繁体   English   中英

Spring Security“记住我”复选框

[英]Spring security “Remember Me” Checkbox

我有一个Web应用程序,后端使用spring安全性。 我设法实现了普通登录,但是现在我想扩展它,以便用户不必在每次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

提前致谢!

将以下配置添加到http对象,

http.rememberMe().key("mySecretKey").tokenValiditySeconds(86400);

和下面的登录页面,

<input type="checkbox" name="remember-me" />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM