目前我正在体验新的 Spring 反应式堆栈,并希望在 Spring Session 2.0 中使用反应式功能。
在传统的 Servlet 方法中,Spring Session 提供了一个HttpSessionStrategy来检测 cookie 或请求标头中的会话。它很容易用于为 RESTful APIHeaderHttpSessionStrategy实现类似身份验证的令牌(默认名称为X-AUTH-TOKEN)。
Spring 5核心提供了一个WebSessionIdResolver为Reactive环境做同样的事情。
但是当它与 Spring Security 一起使用并希望它以传统方式工作时,我无法让它工作。
会话配置文件。
@EnableSpringWebSession
public class SessionConfig {
    @Bean
    public ReactorSessionRepository sessionRepository() {
        return new MapReactorSessionRepository(new ConcurrentHashMap<>());
    }
    @Bean
    public WebSessionIdResolver headerWebSessionIdResolver() {
        HeaderWebSessionIdResolver resolver = new HeaderWebSessionIdResolver();
        resolver.setHeaderName("X-SESSION-ID");
        return resolver;
    }
}
部分SecurityConfig。
@EnableWebFluxSecurity
class SecurityConfig {
    @Bean
    SecurityWebFilterChain springWebFilterChain(HttpSecurity http) throws Exception {
        return http
            .authorizeExchange()
            .pathMatchers(HttpMethod.GET, "/posts/**").permitAll()
            .pathMatchers(HttpMethod.DELETE, "/posts/**").hasRole("ADMIN") …spring spring-security reactive-programming spring-session spring-webflux
我正在使用 Spring Boot 2.0.x、Hibernate 和 Spring Session Jdbc 以及 Mysql 5.7。我在开发环境下工作,因此 Hibernate 配置为每次生成模式:
spring.jpa.hibernate.ddl-auto=create-drop
它工作正常,但我的 Spring Session 有问题......我尝试了 set initialize-schema,但它不起作用。
spring.session.jdbc.initialize-schema=always
是否可以自动生成完整模式(所有实体和 SPRING_SESSION)?
它不适用于 MySQL 和 H2 (我尝试过embedded选项)
我正在 Spring 创建一个简单的酒店预订 Web 应用程序。目前,我正在构建预订功能的实际实现,使用 HttpSession 来存储对服务器的请求之间的数据。
请参阅以下预订流程:
第 1 步:url:“/”(索引页)- 用户选择位置、入住和退房日期
第 2 步:url:“/reservation/roomselection” - 用户从三种可用房间类型中选择一种
第 3 步:url:“/auth/guest/reservation/details” - 用户添加附加信息并确认预订
重要提示 在进入步骤 3 之前,用户需要在登录页面(url:“/login”)上进行身份验证,因此他会自动重定向到登录页面,并且在身份验证成功后重定向到步骤 3 的 url。
整个会话工作正常(应用程序记住了所有信息,直到第三步),直到我在步骤 3 之前引入了身份验证。登录后,所有信息都将丢失,并且 JSESSIONID cookie 将被重新创建(前一个信息将丢失)。
我确实了解该问题的原因 - 我的登录映射位于不同的控制器中,我想将其保留在那里。
我想问是否有任何简单的解决方案可以让我保留相同的SESSIONID和所有信息直到步骤3(登录页面之后)?
我的愿望如下:
我附上了 ReservationController、ReservationDto 以及 HomeController(带有登录页面)的代码。
感谢您的所有回答!
@Controller
@RequestMapping("/")
@SessionAttributes("reservationDto")
public class ReservationController {
    private Logger LOG = LoggerFactory.getLogger(getClass());
    private final HotelService hotelService;
    public ReservationController(HotelService hotelService) {
        this.hotelService = hotelService;
    }
    @GetMapping …我正在使用 Spring Session Redis Data(使用自动配置进行配置,到目前为止没有自定义),它默认用作FindByIndexNameSessionRepository实现SessionRepository。
但是,有时在 Redis 中(在会话已过期但未执行注销之后),前缀为 的密钥的spring:session:index:org.springframework.session.FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME:TTL 仍为 -1,这意味着 Redis 不会使该密钥过期。
为什么 Spring Session(Redis 数据)在会话过期时不清理这个键?我在文档中也找不到提及。
我已按照文档中所述将带有主体名称的属性存储到会话中,但这并不能解决此问题。有关详细信息,请参阅https://docs.spring.io/spring-session/reference/api.html#api-findbyindexnamesessionrepository。
使用的Redis版本:6.2.6(bitnami/redis的docker镜像:6.2.6)
相关依赖:
我不希望索引持久化的原因是,如果有很多用户至少登录过一次并且有一个密钥持久化到Redis(其作用类似于索引),那么Redis将不得不存储可能无法访问的数据很长一段时间(或根本没有)。
spring-security spring-data-redis spring-boot spring-session
Spring Session文档描述了其使用优势之一,如下所示.
允许单个浏览器以透明方式同时具有多个会话.例如,许多开发人员希望允许用户使用多个帐户进行身份验证,并在它们之间切换,类似于gmail中的方式.
从技术上讲,如何利用这一优势,如何实施?
我正在尝试按照本教程在 Spring 中实现外部会话处理。不过,我在添加正确的过滤器时遇到了一些麻烦。Spring Boot 似乎定义了正确的 bean/过滤器,但我的项目不是 Spring Boot,所以它找不到FilterRegistrationBean. 在 Spring 的非 Boot 版本中是否有某种等价于这个类?我也试过org.springframework.web.context.embedded.FilterRegistrationBean,但无法正确导入(看起来这个文档指的是 SNAPSHOT 版本,所以也许这个包从来都不是正确版本的一部分)。
spring spring-mvc servlet-filters spring-bean spring-session
我正在使用spring boot,spring secuirity和spring session(redis)构建一个Spring REST Web应用程序.我正在使用spring cloud和zuul proxy建立一个遵循网关模式的云应用程序.在这种模式中,我使用spring会话来管理redis中的HttpSesssion并使用它来授权我的资源服务器上的请求.当执行一个改变会话权限的操作时,我想更新该对象,以便用户不必注销以反映更新.有人有解决方案吗?
我尝试运行此示例,但不使用Redis,而是使用我的本地MySQL服务器.
我编辑了这个春季启动应用程序,如下所示:
摇篮:
buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:$springBootVersion")
    }
}
apply plugin: 'spring-boot'
apply from: JAVA_GRADLE
//this 'if' statement is because I was getting error: Execution failed for task ':samples:findbyusername:findMainClass'.
//> Could not find property 'main' on task ':samples:findbyusername:run'.
if (!hasProperty('mainClass')) {
    ext.mainClass = 'sample.FindByUsernameApplication'
}
tasks.findByPath("artifactoryPublish")?.enabled = false
group = 'samples'
dependencies {
    compile("org.springframework.boot:spring-boot-starter-jdbc:$springBootVersion") 
    compile group: 'mysql', name: 'mysql-connector-java', version: '6.0.2'
    compile group: 'org.springframework.session', name: 'spring-session', version: '1.2.0.RELEASE'
    compile project(':spring-session'),
            "org.springframework.boot:spring-boot-starter-web",
            "org.springframework.boot:spring-boot-starter-thymeleaf",
            "nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect", …使用 gradle 在 Spring 会话示例下构建时:https ://docs.spring.io/spring-session/docs/current/reference/html5/guides/boot-findbyusername.html
我遇到了关于 java.annotation 模块的错误,有人知道如何解决吗?
/spring-session/spring-session-core/src/main/java/org/springframework/session/config/annotation/web/http/SpringHttpSessionConfiguration.java:22: error: package javax.annotation is not visible
import javax.annotation.PostConstruct;
            ^
  (package javax.annotation is declared in module java.xml.ws.annotation, which is not in the module graph)
warning: unknown enum constant When.MAYBE
  reason: class file for javax.annotation.meta.When not found
1 error
1 warning
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':spring-session-core:compileJava'.
> Compilation failed; see the compiler error output for details.
我尝试在 build.gradle 中添加以下配置,但问题仍然存在。
tasks.withType(AbstractCompile) { …在spring-session-data-redis演示spring-boot项目中进行配置后,bootRun任务失败,并显示以下消息:
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of method redisTemplate in org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration required a bean of type 'org.springframework.data.redis.connection.RedisConnectionFactory' that could not be found.
    - Bean method 'redisConnectionFactory' not loaded because @ConditionalOnClass did not find required classes 'org.apache.commons.pool2.impl.GenericObjectPool', 'redis.clients.jedis.Jedis'
    - Bean method 'redisConnectionFactory' not loaded because @ConditionalOnClass did not find required class 'io.lettuce.core.RedisClient'
Action:
Consider revisiting the entries above or defining a bean of type 'org.springframework.data.redis.connection.RedisConnectionFactory' in your configuration.
我已经完成的工作(又称重现步骤):
 
1.使用Spring Initializr创建了一个[带有Java和Spring …
spring-session ×10
spring ×6
spring-boot ×4
java ×3
spring-mvc ×2
gradle ×1
hibernate ×1
jdbc ×1
mysql ×1
redis ×1
spring-bean ×1
spring-web ×1