zoukankan      html  css  js  c++  java
  • 记住我 token保存到数据库

    这里使用jpa+mysql

         <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
            </dependency>
    
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    spring.datasource.url=jdbc:mysql://127.0.0.1:3306/fly-demo?serverTimezone=UTC&useUnicode=yes&characterEncoding=UTF-8&useSSL=false
    spring.datasource.username=root
    spring.datasource.password=123456
    

    设置配置rememberme

    import javax.sql.DataSource;
    
    @Configuration
    public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
        @Autowired
        private AuthenticationSuccessHandler flyAuthenticationSuccessHandler;
        @Autowired
        private AuthenticationFailureHandler flyAuthenticationFailureHandler;
    
        @Autowired
        private SecurityProperties securityProperties;
    
        @Autowired
        private PasswordEncoder passwordEncoder;
        @Bean
        public PasswordEncoder setPasswordEncoder() {
            return new BCryptPasswordEncoder();
        }
    
        @Autowired
        private DataSource dataSource;
    
        @Bean
        public PersistentTokenRepository persistentTokenRepository(){
            JdbcTokenRepositoryImpl tokenRepository = new JdbcTokenRepositoryImpl();
    //        tokenRepository.setCreateTableOnStartup(true);
            tokenRepository.setDataSource(dataSource);
            return tokenRepository;
        }
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            ValidateCodeFilter codeFilter = new ValidateCodeFilter(flyAuthenticationFailureHandler);
                http
                    .addFilterBefore(codeFilter, UsernamePasswordAuthenticationFilter.class)
                    .formLogin()
                    .loginPage("/authentication/request")
                    .loginProcessingUrl("/authentication/form")
                    .successHandler(flyAuthenticationSuccessHandler)
                    .failureHandler(flyAuthenticationFailureHandler)
                    .and()
                    .rememberMe()
                        .tokenRepository(persistentTokenRepository())
                        .tokenValiditySeconds(securityProperties.getBrowser().getRememberMe())
                        .userDetailsService(userDetails())
                    .and()
                    .authorizeRequests()
                    .antMatchers("/authentication/request",
                            securityProperties.getBrowser().getLoginPage(),
                            "/captcha")
                    .permitAll()
                    .anyRequest().authenticated()
                    .and().csrf().disable();
        }
        @Bean
        public UserDetailsService userDetails(){
            InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
            manager.createUser(User.withUsername("user").password(passwordEncoder.encode("123")).roles("USER").build());
            return manager;
        }
    }
    
    
     
  • 相关阅读:
    Oracle删除约束和主键的语句
    Tomcat启动时SecureRandom超级慢的问题
    MySQL innodb引擎下根据.frm和.ibd文件恢复表结构和数据
    ORA-00604: 递归 SQL 级别 1 出现错误 ORA-01653: 表 SYS.AUD$ 无法通过 8192 (在表空间 SYSTEM 中) 扩展
    Linux 使用pwgen命令创建随机密码
    nginx 之 proxy_pass详解
    给nginx生成自签名证书
    常见排序算法(java实现)
    浅析变量的作用域和生存周期的差别
    filter过滤器
  • 原文地址:https://www.cnblogs.com/exmyth/p/13602704.html
Copyright © 2011-2022 走看看