【问题标题】:Spring security "Remember Me" CheckboxSpring security“记住我”复选框
【发布时间】: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


    【解决方案1】:

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

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

    以下是登录页面,

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

    【讨论】:

    • 你能解释一下这个方法吗?
    • 另外我不想要有限的时间
    猜你喜欢
    • 2015-05-10
    • 2012-01-21
    • 2014-09-04
    • 2018-12-02
    • 2014-02-04
    • 2011-02-15
    • 2012-03-13
    • 2016-01-31
    • 1970-01-01
    相关资源
    最近更新 更多