欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring Security 構(gòu)建rest服務(wù)實(shí)現(xiàn)rememberme 記住我功能

 更新時(shí)間:2018年03月06日 09:54:19   作者:奮斗的羊仔  
這篇文章主要介紹了Spring Security 構(gòu)建rest服務(wù)實(shí)現(xiàn)rememberme 記住我功能,需要的朋友可以參考下

Spring security記住我基本原理:

登錄的時(shí)候,請(qǐng)求發(fā)送給過濾器UsernamePasswordAuthenticationFilter,當(dāng)該過濾器認(rèn)證成功后,會(huì)調(diào)用RememberMeService,會(huì)生成一個(gè)token,將token寫入到瀏覽器cookie,同時(shí)RememberMeService里邊還有個(gè)TokenRepository,將token和用戶信息寫入到數(shù)據(jù)庫中。這樣當(dāng)用戶再次訪問系統(tǒng),訪問某一個(gè)接口時(shí),會(huì)經(jīng)過一個(gè)RememberMeAuthenticationFilter的過濾器,他會(huì)讀取cookie中的token,交給RememberService,RememberService會(huì)用TokenRepository根據(jù)token從數(shù)據(jù)庫中查是否有記錄,如果有記錄會(huì)把用戶名取出來,再調(diào)用UserDetailService根據(jù)用戶名獲取用戶信息,然后放在SecurityContext里。

 RememberMeAuthenticationFilter在Spring Security中認(rèn)證過濾器鏈的倒數(shù)第二個(gè)過濾器位置,當(dāng)其他認(rèn)證過濾器都沒法認(rèn)證成功的時(shí)候,就會(huì)調(diào)用RememberMeAuthenticationFilter嘗試認(rèn)證。

實(shí)現(xiàn):

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

 2,根據(jù)上邊的原理圖可知,要配置TokenRepository,把生成的token存進(jìn)數(shù)據(jù)庫,這是一個(gè)配置bean的配置,放在了BrowserSecurityConfig里

3,在configure里配置

4,在BrowserProperties里加上自動(dòng)登錄時(shí)間,把記住我時(shí)間做成可配置的

//記住我秒數(shù)配置
private int rememberMeSeconds = 10;齊活
package com.imooc.s@Configuration //這是一個(gè)配置
public class BrowserSecurityConfig extends WebSecurityConfigurerAdapter{
  //讀取用戶配置的登錄頁配置
  @Autowired
  private SecurityProperties securityProperties;
  //自定義的登錄成功后的處理器
  @Autowired
  private AuthenticationSuccessHandler imoocAuthenticationSuccessHandler;
  //自定義的認(rèn)證失敗后的處理器
  @Autowired
  private AuthenticationFailureHandler imoocAuthenticationFailureHandler;
  //數(shù)據(jù)源
  @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配置,在登錄成功后執(zhí)行
   * 登錄成功后往數(shù)據(jù)庫存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);
    //啟動(dòng)時(shí)自動(dòng)生成相應(yīng)表,可以在JdbcTokenRepositoryImpl里自己執(zhí)行CREATE_TABLE_SQL腳本生成表
    jdbcTokenRepository.setCreateTableOnStartup(true);
    return jdbcTokenRepository;
  }
  //版本二:可配置的登錄頁
  @Override
  protected void configure(HttpSecurity http) throws Exception {
    //驗(yàn)證碼過濾器
    ValidateCodeFilter validateCodeFilter = new ValidateCodeFilter();
    //驗(yàn)證碼過濾器中使用自己的錯(cuò)誤處理
    validateCodeFilter.setAuthenticationFailureHandler(imoocAuthenticationFailureHandler);
    //配置的驗(yàn)證碼過濾url
    validateCodeFilter.setSecurityProperties(securityProperties);
    validateCodeFilter.afterPropertiesSet();
    //實(shí)現(xiàn)需要認(rèn)證的接口跳轉(zhuǎn)表單登錄,安全=認(rèn)證+授權(quán)
    //http.httpBasic() //這個(gè)就是默認(rèn)的彈框認(rèn)證
    //
    http //把驗(yàn)證碼過濾器加載登錄過濾器前邊
      .addFilterBefore(validateCodeFilter, UsernamePasswordAuthenticationFilter.class)
      //表單認(rèn)證相關(guān)配置
      .formLogin() 
        .loginPage("/authentication/require") //處理用戶認(rèn)證BrowserSecurityController
        //登錄過濾器UsernamePasswordAuthenticationFilter默認(rèn)登錄的url是"/login",在這能改
        .loginProcessingUrl("/authentication/form") 
        .successHandler(imoocAuthenticationSuccessHandler)//自定義的認(rèn)證后處理器
        .failureHandler(imoocAuthenticationFailureHandler) //登錄失敗后的處理
      .and()
      //記住我相關(guān)配置  
      .rememberMe()
        .tokenRepository(persistentTokenRepository())//TokenRepository,登錄成功后往數(shù)據(jù)庫存token的
        .tokenValiditySeconds(securityProperties.getBrowser().getRememberMeSeconds())//記住我秒數(shù)
        .userDetailsService(userDetailsService) //記住我成功后,調(diào)用userDetailsService查詢用戶信息
      .and()
      //授權(quán)相關(guān)的配置 
      .authorizeRequests() 
        // /authentication/require:處理登錄,securityProperties.getBrowser().getLoginPage():用戶配置的登錄頁
        .antMatchers("/authentication/require",
        securityProperties.getBrowser().getLoginPage(),//放過登錄頁不過濾,否則報(bào)錯(cuò)
        "/verifycode/image").permitAll() //驗(yàn)證碼
        .anyRequest()    //任何請(qǐng)求
        .authenticated()  //都需要身份認(rèn)證
      .and()
        .csrf().disable() //關(guān)閉csrf防護(hù)
      ;  
  }
}ecurity.browser;

其中由于要和數(shù)據(jù)庫打交道,所以需要注入一個(gè)數(shù)據(jù)源: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

啟動(dòng)應(yīng)用,訪問 localhost:8080/user,需要登錄

登錄成功:

數(shù)據(jù)庫:生成一個(gè)persistent_logins表,存進(jìn)去了一條數(shù)據(jù)

停止服務(wù),從新啟動(dòng)(注釋掉生成保存token表的jdbcTokenRepository.setCreateTableOnStartup(true);)因?yàn)槲覀兊挠脩舻卿浶畔⒍即嬖诹藄ession中,所以重啟服務(wù)后,再訪問localhost:8080/user,本應(yīng)該重新引導(dǎo)到登錄頁,但是由于配置了記住我,所以能夠直接訪問,拿到了接口數(shù)據(jù)

請(qǐng)求頭:

至此基本的rememberMe已做好

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

總結(jié)

以上所述是小編給大家介紹的Spring Security 構(gòu)建rest服務(wù)實(shí)現(xiàn)rememberme 記住我功能,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論