Spring Boot整合Security系列步骤及问题排查(九)—— 记住我功能

本文详细介绍了如何在Spring Boot项目中结合Spring Security实现"记住我"功能,包括BrowserProperties的超时属性配置、WebSecurityConfig的更新、登录页面记住我复选框的添加,以及启动后的测试和可能出现的问题排查过程。
摘要由CSDN通过智能技术生成

1.BrowserProperties增加超时属性配置:

/**
 * 记住我过期时间(默认1小时)
 */
private int rememberMeSeconds = 3600;

2.更新WebSecurityConfig:

/**
 * SpringSecurity配置
 *
 * @author zhaohaibin
 */
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private SecurityProperties securityProperties;

    /**
     * 自定义成功处理逻辑
     */
    @Autowired
    private DemoAuthenticationSuccessHandler demoAuthenticationSuccessHandler;
    /**
     * 自定义失败处理逻辑
     */
    @Autowired
    private DemoAuthenticationFailureHandler demoAuthenticationFailureHandler;

    /**
     * 记住我功能引入数据源配置
     */
    @Autowired
    private DataSource dataSource;

    /**
     * 对UserDetailsService的自定义实现
     */
    @Autowired
    private ISystemUserService userDetailsService;

    /**
     * 加密解密官方实现
     *
     * @return
     */
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
    
    /**
     * 记住我功能token存储配置
     *
     * @return
     */
    @Bean
    public PersistentTokenRepository persistentTokenRepository() {
        JdbcTokenRepositoryImpl tokenRepository = new JdbcTokenRepositoryImpl();
        tokenRepository.setDataSource(dataSource);
        // 启动创建表,创建后记得注释掉
//        tokenRepository.setCreateTableOnStartup(true);
        return tokenRepository;
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        ValidateCodeFilter validateCodeFilter = new ValidateCodeFilter();
        // 传入自定义失败处理
        validateCodeFilter.setAuthenticationFailureHandler(demoAuthenticationFailureHandler);
        // 传入自定义路径配置
        validateCodeFilter.setSecurityProperties(securityProperties);
        // 执行分隔操作
        validateCodeFilter.afterPropertiesSet();

        // 默认/表单登录方式
//        http.httpBasic()
        // 在默认UsernamePasswordAuthenticationFilter前增加自定义验证码校验拦截,并传入自定义失败对象
        http.addFilterBefore(validateCodeFilter, UsernamePasswordAuthenticationFilter.class)
                .formLogin()
                // 自定义登录页面
                .loginPage("/authentication/require")
                .loginProcessingUrl("/authentication/form")
                .successHandler(demoAuthenticationSuccessHandler)
                .failureHandler(demoAuthenticationFailureHandler)
                .and()
                // 记住我相关配置
                .rememberMe()
                .tokenRepository(persistentTokenRepository())
                .tokenValiditySeconds(securityProperties.getBrowser().getRememberMeSeconds())
                .userDetailsService(userDetailsService)
                .and()
                // 对任何请求授权
                .authorizeRequests()
                // 匹配页面授权所有权限
                .antMatchers(
                        // API
                        "/swagger-ui.html",
                        // 默认登录页
                        "/authentication/require",
                        // 自定义登录页(demoLogin)
                        securityProperties.getBrowser().getLoginPage(),
                        "/code/image").permitAll()
                // 任何请求
                .anyRequest()
                // 都需要被认证
                .authenticated()
                .and()
                // 请求伪造防护功能关闭
                .csrf().disable();

    }

}

3.更新登录页,增加记住我复选框:

    <div><input type="checkbox" name="remember-me" value="true"/><label>记住我</label></div>

4.启动测试:

第一次启动,数据库自动新建persistent_logins表,登录后自动新建一条数据,登录成功,接口访问成功;
第二次启动(记得注释生成表代码),接口访问成功;

问题排查:
暂无

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值