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

    记住我 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;
        }
    }
    
    
  • 相关阅读:
    iDirect IQ200 Evolution Option Config
    直播卫星接收设备参数
    QCA9531方案双通道嵌入式无线AP模块应用选型参考
    FanControl 机箱风扇控制软件
    硬盘基本知识(磁头、磁道、扇区、柱面)
    Registry keys for configuring Automatic Updates & WSUS
    CentOS6.5下简单的MySQL数据库操作
    临时搞两天VC,在VC里如何获取当前程序的名字和路径以及如何分割字符串
    COBOL中USAGE的用法
    sqlplus 帮助无法显示问题的解决
  • 原文地址:https://www.cnblogs.com/fly-book/p/12239989.html
Copyright © 2011-2022 走看看