在没有 Spring Security 的情况下持久化 Spring 会话 [英] Persist Spring Session without Spring Security

查看:59
本文介绍了在没有 Spring Security 的情况下持久化 Spring 会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am seeking assistance in configuring Spring Boot to persist a Spring Session without using Spring Security.

我从 HttpSession JDBC Spring Boot 示例应用程序,用于为 Spring Boot 应用程序保留 Spring 会话.但是,这使用了 Spring Security.删除 Spring Security 时,会话将停止保留在 H2 数据库中.XML 配置Java 配置示例应用程序不使用 Spring Security.因此,不是必需的.

I started with the HttpSession JDBC Spring Boot sample app, to persist a Spring Session for a Spring Boot app. However this uses Spring Security. When removing Spring Security, the session ceases to be persisted in the H2 database. The XML config and Java config sample apps do not utilize Spring Security. Thus, not a requirement.

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

The guide states that the springSessionRepositoryFilter will create the Spring Session. However, after removing Spring Security and debugging this filter (SessionRepositoryFilter), the boolean passed to SessionRepositoryFilter$SessionRepositoryRequestWrapper.getSession(boolean) is never set to true. Thus, the Spring Session is never created and persisted.

关于在没有 Spring Security 的情况下为 Spring Boot 应用程序持久化 Spring Session 所需的额外配置有什么建议吗?

Any suggestions on additional configuration required to persist a Spring Session for a Spring Boot app, without Spring Security?

这里是相关的类、pom.xml 和 application.property 文件:

Here are the relevant classes, pom.xml, and application.property file:

@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

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 控制器方法.

Actually, there isn't anything more in particular you need to configure - you just need to initiate the creation of HttpSession and use it according to your needs. For example, the simplest way to achieve this would be to have Spring MVC controller method with HttpSession as an argument.

查看 Github 上的这个 repo,特别是 basic-jdbc 模块.这正是您正在寻找的示例 - 带有 Spring Session 但没有 Spring Security 的 Spring Boot 应用程序.

Check out this repo on Github, in particular basic-jdbc module. It's exactly the sample you're looking for - Spring Boot application with Spring Session but without Spring Security.

这篇关于在没有 Spring Security 的情况下持久化 Spring 会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆