spring-boot – 在没有Spring Security的情况下坚持Spring会话

我正在寻求帮助配置
Spring Boot以在不使用Spring Security的情况下持久保存Spring会话.

我从HttpSession JDBC Spring Boot sample app开始,坚持Spring会话为Spring Boot应用程序.但是这使用了Spring Security.删除Spring Security时,会话将不再保留在H2数据库中. XML configJava config示例应用程序不使用Spring Security.因此,不是要求.

guide声明springSessionRepositoryFilter将创建Spring Session.但是,在删除Spring Security并调试此过滤器(SessionRepositoryFilter)之后,传递给SessionRepositoryFilter $SessionRepositoryRequestWrapper.getSession(boolean)的布尔值永远不会设置为true.因此,Spring Session永远不会被创建和持久化.

关于在没有Spring Security的情况下为Spring Boot应用程序保留Spring会话所需的其他配置的任何建议?

以下是相关的类,pom.xml和application.property文件:

@SpringBootApplication
public class SbWebSessionJdbcApplication {

    public static void main(String[] args) {
        SpringApplication.run(SbWebSessionJdbcApplication.class, args);
    }
}

@EnableJdbcHttpSession 
public class HttpSessionConfig {
}

的pom.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.5.RELEASE</version>
    <relativePath/>
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.session</groupId>
        <artifactId>spring-session-jdbc</artifactId>
        <version>1.2.0.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.session</groupId>
        <artifactId>spring-session</artifactId>
        <version>1.2.0.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

application.properties

spring.thymeleaf.cache=false
spring.template.cache=false
spring.datasource.schema=classpath:org/springframework/session/jdbc/schema-h2.sql
spring.h2.console.enabled=true
logging.level.org.springframework.web=DEBUG

最佳答案 实际上,您不需要配置任何更多 – 您只需要启动HttpSession的创建并根据您的需要使用它.例如,实现这一点的最简单方法是使用HttpSession作为参数的Spring MVC控制器方法.

查看this repo on Github,特别是basic-jdbc模块.这正是您正在寻找的样本 – 使用Spring Session但没有Spring Security的Spring Boot应用程序.

点赞