我有一个 Web 应用程序,我的后端使用 spring security。我设法实现了正常登录,但现在我想扩展它,以便用户不必在每次 sessionid cookie 过期时都登录。我也不希望延长上述 cookie 的有效期。相反,我想把这个选择留给用户。想保持登录状态的用户可以这样做,其他用户应在一定时间后自动注销。我想借助登录表单中的复选框来实现这一点。这是如何实现的?我设法找到了关于记住我 cookie 的信息,但据我所知,这具有全局影响,使所有用户都保持登录状态。
这是我的网络配置:
@Override
protected void configure(HttpSecurity http) throws Exception {
http.addFilterBefore(new CORSFilter(), ChannelProcessingFilter.class).authorizeRequests()
// ...Long series of .antMatchers...
.antMatchers("/login", "/getActiveUser", "/logout")
.permitAll()
.anyRequest().authenticated()
.and().authenticationProvider(authenticationProvider())
.exceptionHandling()
.authenticationEntryPoint(authenticationEntryPoint)
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.successHandler(authSuccessHandler)
.failureHandler(authFailureHandler)
.and()
.logout()
.permitAll()
.and()
.csrf().disable();
http.formLogin().defaultSuccessUrl("http://localhost:8100", true);
}
还有我的 application.properties 文件:
spring.jpa.open-in-view=false
spring.datasource.url=jdbc:mysql://localhost:3307/projectdb?serverTimezone=UTC
spring.datasource.username=***
spring.datasource.password=***
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jackson.serialization.write_dates_as_timestamps=false
server.session.cookie.max-age=600
提前致谢!
最佳答案
将以下配置添加到http对象,
http.rememberMe().key("mySecretKey").tokenValiditySeconds(86400);
和下面的登录页面,
<input type="checkbox" name="remember-me" />
关于java - Spring 安全 "Remember Me"复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44524388/