【发布时间】:2017-11-15 09:39:47
【问题描述】:
我有一个 Web 应用程序,我的后端使用 Spring Security。我设法实现了正常登录,但现在我想扩展它,以便用户不必在每次 sessionid cookie 过期时登录。我也不希望延长上述 cookie 的有效期。相反,我想把这个选择留给用户。谁想保持登录状态可以这样做,其他用户应该在一定时间后自动注销。我想借助登录表单中的复选框来实现这一点。这怎么可能实现?我设法找到了有关记住我 cookie 的信息,但据我所知,这具有全局影响,使所有用户保持登录状态。
这是我的 WebConfig:
@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
提前致谢!
【问题讨论】:
标签: java spring cookies login spring-security