Spring Security構建Rest服務-0900-rememberMe記住我


Spring security記住我基本原理:

登錄的時候,請求發送給過濾器UsernamePasswordAuthenticationFilter,當該過濾器認證成功后,會調用RememberMeService,會生成一個token,將token寫入到瀏覽器cookie,同時RememberMeService里邊還有個TokenRepository,將token和用戶信息寫入到數據庫中。這樣當用戶再次訪問系統,訪問某一個接口時,會經過一個RememberMeAuthenticationFilter的過濾器,他會讀取cookie中的token,交給RememberService,RememberService會用TokenRepository根據token從數據庫中查是否有記錄,如果有記錄會把用戶名取出來,再調用UserDetailService根據用戶名獲取用戶信息,然后放在SecurityContext里。

 RememberMeAuthenticationFilter在Spring Security中認證過濾器鏈的倒數第二個過濾器位置,當其他認證過濾器都沒法認證成功的時候,就會調用RememberMeAuthenticationFilter嘗試認證。

實現:

 1,登錄表單加上<input type="checkbox" name="remember-me" value="true"/>,SpringSecurity在SpringSessionRememberMeServices類里定義了一個常量,默認值就是remember-me

 2,根據上邊的原理圖可知,要配置TokenRepository,把生成的token存進數據庫,這是一個配置bean的配置,放在了BrowserSecurityConfig里

3,在configure里配置

4,在BrowserProperties里加上自動登錄時間,把記住我時間做成可配置的

//記住我秒數配置
private int rememberMeSeconds = 10;齊活

package com.imooc.security.browser;

@Configuration //這是一個配置
public class BrowserSecurityConfig extends WebSecurityConfigurerAdapter{
    
    //讀取用戶配置的登錄頁配置
    @Autowired
    private SecurityProperties securityProperties;
    
    //自定義的登錄成功后的處理器
    @Autowired
    private AuthenticationSuccessHandler imoocAuthenticationSuccessHandler;
    
    //自定義的認證失敗后的處理器
    @Autowired
    private AuthenticationFailureHandler imoocAuthenticationFailureHandler;
    
    //數據源
    @Autowired
    private DataSource dataSource;
    
    
    @Autowired
    private UserDetailsService userDetailsService;

    //注意是org.springframework.security.crypto.password.PasswordEncoder
    @Bean
    public PasswordEncoder passwordencoder(){
        //BCryptPasswordEncoder implements PasswordEncoder
        return new BCryptPasswordEncoder();
    }
    
    
    /**
     * 記住我TokenRepository配置,在登錄成功后執行
     * 登錄成功后往數據庫存token的
     * @Description: 記住我TokenRepository配置
     * @param @return   JdbcTokenRepositoryImpl
     * @return PersistentTokenRepository  
     * @throws
     * @author lihaoyang
     * @date 2018年3月5日
     */
    @Bean
    public PersistentTokenRepository persistentTokenRepository(){
        JdbcTokenRepositoryImpl jdbcTokenRepository = new JdbcTokenRepositoryImpl();
        jdbcTokenRepository.setDataSource(dataSource);
        //啟動時自動生成相應表,可以在JdbcTokenRepositoryImpl里自己執行CREATE_TABLE_SQL腳本生成表
        jdbcTokenRepository.setCreateTableOnStartup(true);
        return jdbcTokenRepository;
    }
    
    
   //版本二:可配置的登錄頁
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //驗證碼過濾器
        ValidateCodeFilter validateCodeFilter = new ValidateCodeFilter();
        //驗證碼過濾器中使用自己的錯誤處理
        validateCodeFilter.setAuthenticationFailureHandler(imoocAuthenticationFailureHandler);
        //配置的驗證碼過濾url
        validateCodeFilter.setSecurityProperties(securityProperties);
        validateCodeFilter.afterPropertiesSet();
        
        
        //實現需要認證的接口跳轉表單登錄,安全=認證+授權
        //http.httpBasic() //這個就是默認的彈框認證
        //
        http //把驗證碼過濾器加載登錄過濾器前邊
            .addFilterBefore(validateCodeFilter, UsernamePasswordAuthenticationFilter.class)
            //表單認證相關配置
            .formLogin() 
                .loginPage("/authentication/require") //處理用戶認證BrowserSecurityController
                //登錄過濾器UsernamePasswordAuthenticationFilter默認登錄的url是"/login",在這能改
                .loginProcessingUrl("/authentication/form") 
                .successHandler(imoocAuthenticationSuccessHandler)//自定義的認證后處理器
                .failureHandler(imoocAuthenticationFailureHandler) //登錄失敗后的處理
            .and()
            //記住我相關配置    
 .rememberMe()
                .tokenRepository(persistentTokenRepository())//TokenRepository,登錄成功后往數據庫存token的
                .tokenValiditySeconds(securityProperties.getBrowser().getRememberMeSeconds())//記住我秒數
                .userDetailsService(userDetailsService) //記住我成功后,調用userDetailsService查詢用戶信息
            .and()
            //授權相關的配置 
            .authorizeRequests() 
                // /authentication/require:處理登錄,securityProperties.getBrowser().getLoginPage():用戶配置的登錄頁
                .antMatchers("/authentication/require",
                securityProperties.getBrowser().getLoginPage(),//放過登錄頁不過濾,否則報錯
                "/verifycode/image").permitAll() //驗證碼
                .anyRequest()        //任何請求
                .authenticated()    //都需要身份認證
            .and()
                .csrf().disable() //關閉csrf防護
            ;    
    }
}

其中由於要和數據庫打交道,所以需要注入一個數據源:application.properties

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/imooc-demo
spring.datasource.username=root
spring.datasource.password=root

啟動應用,訪問 localhost:8080/user,需要登錄

登錄成功:

數據庫:生成一個persistent_logins表,存進去了一條數據

停止服務,從新啟動(注釋掉生成保存token表的jdbcTokenRepository.setCreateTableOnStartup(true);)因為我們的用戶登錄信息都存在了session中,所以重啟服務后,再訪問localhost:8080/user,本應該重新引導到登錄頁,但是由於配置了記住我,所以能夠直接訪問,拿到了接口數據

請求頭:

至此基本的rememberMe已做好

 

 

完整代碼放在了github:https://github.com/lhy1234/spring-security

 

打個廣告

最近在玩今日頭條頭條號,錄一些北京打工生活,想看的請搜索“北漂小陽”點擊關注


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM