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

SpringBoot + SpringSecurity 短信驗(yàn)證碼登錄功能實(shí)現(xiàn)

 更新時(shí)間:2018年06月13日 13:49:58   作者:whyalwaysmea  
這篇文章主要介紹了SpringBoot + SpringSecurity 短信驗(yàn)證碼登錄功能實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

實(shí)現(xiàn)原理

在之前的文章中,我們介紹了普通的帳號(hào)密碼登錄的方式: SpringBoot + Spring Security 基本使用及個(gè)性化登錄配置。 但是現(xiàn)在還有一種常見(jiàn)的方式,就是直接通過(guò)手機(jī)短信驗(yàn)證碼登錄,這里就需要自己來(lái)做一些額外的工作了。

對(duì)SpringSecurity認(rèn)證流程詳解有一定了解的都知道,在帳號(hào)密碼認(rèn)證的過(guò)程中,涉及到了以下幾個(gè)類(lèi):UsernamePasswordAuthenticationFilter(用于請(qǐng)求參數(shù)獲?。?,UsernamePasswordAuthenticationToken(表示用戶登錄信息),ProviderManager(進(jìn)行認(rèn)證校驗(yàn)),

因?yàn)槭峭ㄟ^(guò)的短信驗(yàn)證碼登錄,所以我們需要對(duì)請(qǐng)求的參數(shù),認(rèn)證過(guò)程,用戶登錄Token信息進(jìn)行一定的重寫(xiě)。
當(dāng)然驗(yàn)證碼的過(guò)程我們應(yīng)該放在最前面,如果圖形驗(yàn)證碼的實(shí)現(xiàn)一樣。這樣的做法的好處是:將驗(yàn)證碼認(rèn)證該過(guò)程解耦出來(lái),讓其他接口也可以使用到。

基本實(shí)現(xiàn)

驗(yàn)證碼校驗(yàn)

短信驗(yàn)證碼的功能實(shí)現(xiàn),其實(shí)和圖形驗(yàn)證碼的原理是一樣的。只不過(guò)一個(gè)是返回給前端一個(gè)圖片,一個(gè)是給用戶發(fā)送短消息,這里只需要去調(diào)用一下短信服務(wù)商的接口就好了。更多的原理可以參考 SpringBoot + SpringSecurity 實(shí)現(xiàn)圖形驗(yàn)證碼功能

AuthenticationToken

在使用帳號(hào)密碼登錄的時(shí)候,UsernamePasswordAuthenticationToken里面包含了用戶的帳號(hào),密碼,以及其他的是否可用等狀態(tài)信息。我們是通過(guò)手機(jī)短信來(lái)做登錄,所以就沒(méi)有密碼了,這里我們就直接將UsernamePasswordAuthenticationToken的代碼copy過(guò)來(lái),把密碼相關(guān)的信息去掉就可以了

public class SmsCodeAuthenticationToken extends AbstractAuthenticationToken {

  private static final long serialVersionUID = SpringSecurityCoreVersion.SERIAL_VERSION_UID;

  private final Object principal;

  public SmsCodeAuthenticationToken(String mobile) {
    super(null);
    this.principal = mobile;
    setAuthenticated(false);
  }

  public SmsCodeAuthenticationToken(Object principal,
                   Collection<? extends GrantedAuthority> authorities) {
    super(authorities);
    this.principal = principal;
    super.setAuthenticated(true); // must use super, as we override
  }

  public Object getCredentials() {
    return null;
  }

  public Object getPrincipal() {
    return this.principal;
  }

  public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
    if (isAuthenticated) {
      throw new IllegalArgumentException(
          "Cannot set this token to trusted - use constructor which takes a GrantedAuthority list instead");
    }
    super.setAuthenticated(false);
  }

  @Override
  public void eraseCredentials() {
    super.eraseCredentials();
  }
}

AuthenticationFilter

在帳戶密碼登錄的流程中,默認(rèn)使用的是UsernamePasswordAuthenticationFilter,它的作用是從請(qǐng)求中獲取帳戶、密碼,請(qǐng)求方式校驗(yàn),生成AuthenticationToken。這里我們的參數(shù)是有一定改變的,所以還是老方法,copy過(guò)來(lái)進(jìn)行簡(jiǎn)單的修改

public class SmsCodeAuthenticationFilter extends AbstractAuthenticationProcessingFilter {
  // 請(qǐng)求參數(shù)key
  private String mobileParameter = SecurityConstants.DEFAULT_PARAMETER_NAME_MOBILE;
  // 是否只支持POST
  private boolean postOnly = true;

  public SmsCodeAuthenticationFilter() {
    // 請(qǐng)求接口的url
    super(new AntPathRequestMatcher(SecurityConstants.DEFAULT_LOGIN_PROCESSING_URL_MOBILE, "POST"));
  }

  public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
      throws AuthenticationException {
    if (postOnly && !request.getMethod().equals("POST")) {
      throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod());
    }
    // 根據(jù)請(qǐng)求參數(shù)名,獲取請(qǐng)求value
    String mobile = obtainMobile(request);
    if (mobile == null) {
      mobile = "";
    }
    mobile = mobile.trim();

    // 生成對(duì)應(yīng)的AuthenticationToken
    SmsCodeAuthenticationToken authRequest = new SmsCodeAuthenticationToken(mobile);

    setDetails(request, authRequest);

    return this.getAuthenticationManager().authenticate(authRequest);
  }

  /**
   * 獲取手機(jī)號(hào)
   */
  protected String obtainMobile(HttpServletRequest request) {
    return request.getParameter(mobileParameter);
  }
  // 省略不相關(guān)代碼
}

Provider

在帳號(hào)密碼登錄的過(guò)程中,密碼的正確性以及帳號(hào)是否可用是通過(guò)DaoAuthenticationProvider來(lái)校驗(yàn)的。我們也應(yīng)該自己實(shí)現(xiàn)一個(gè)Provier

public class SmsCodeAuthenticationProvider implements AuthenticationProvider {

  private UserDetailsService userDetailsService;

  /**
   * 身份邏輯驗(yàn)證
   * @param authentication
   * @return
   * @throws AuthenticationException
   */
  @Override
  public Authentication authenticate(Authentication authentication) throws AuthenticationException {

    SmsCodeAuthenticationToken authenticationToken = (SmsCodeAuthenticationToken) authentication;

    UserDetails user = userDetailsService.loadUserByUsername((String) authenticationToken.getPrincipal());

    if (user == null) {
      throw new InternalAuthenticationServiceException("無(wú)法獲取用戶信息");
    }

    SmsCodeAuthenticationToken authenticationResult = new SmsCodeAuthenticationToken(user, user.getAuthorities());

    authenticationResult.setDetails(authenticationToken.getDetails());

    return authenticationResult;
  }

  @Override
  public boolean supports(Class<?> authentication) {
    return SmsCodeAuthenticationToken.class.isAssignableFrom(authentication);
  }

  public UserDetailsService getUserDetailsService() {
    return userDetailsService;
  }

  public void setUserDetailsService(UserDetailsService userDetailsService) {
    this.userDetailsService = userDetailsService;
  }
}

配置

主要的認(rèn)證流程就是通過(guò)以上四個(gè)過(guò)程實(shí)現(xiàn)的, 這里我們?cè)俳邓鼈兣渲靡幌戮涂梢粤?/p>

@Component
public class SmsCodeAuthenticationSecurityConfig extends SecurityConfigurerAdapter<DefaultSecurityFilterChain, HttpSecurity> {

  @Autowired
  private AuthenticationSuccessHandler myAuthenticationSuccessHandler;

  @Autowired
  private AuthenticationFailureHandler myAuthenticationFailureHandler;

  @Autowired
  private UserDetailsService userDetailsService;

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

    SmsCodeAuthenticationFilter smsCodeAuthenticationFilter = new SmsCodeAuthenticationFilter();
    smsCodeAuthenticationFilter.setAuthenticationManager(http.getSharedObject(AuthenticationManager.class));
    smsCodeAuthenticationFilter.setAuthenticationSuccessHandler(myAuthenticationSuccessHandler);
    smsCodeAuthenticationFilter.setAuthenticationFailureHandler(myAuthenticationFailureHandler);

    SmsCodeAuthenticationProvider smsCodeAuthenticationProvider = new SmsCodeAuthenticationProvider();
    smsCodeAuthenticationProvider.setUserDetailsService(userDetailsService);

    http.authenticationProvider(smsCodeAuthenticationProvider)
        .addFilterAfter(smsCodeAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);

  }
}
 
// BrowerSecurityConfig.java
@Override
protected void configure(HttpSecurity http) throws Exception {
  http.apply(smsCodeAuthenticationSecurityConfig);
}

代碼下載

Spring-Security

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 將本地SpringBoot項(xiàng)目發(fā)布到云服務(wù)器的方法

    將本地SpringBoot項(xiàng)目發(fā)布到云服務(wù)器的方法

    這篇文章主要介紹了如何將本地SpringBoot項(xiàng)目發(fā)布到云服務(wù)器,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-12-12
  • Java中類(lèi)的初始化和實(shí)例化區(qū)別詳解

    Java中類(lèi)的初始化和實(shí)例化區(qū)別詳解

    這篇文章主要介紹了Java中類(lèi)的初始化和實(shí)例化區(qū)別詳解,類(lèi)的初始化<BR>是完成程序執(zhí)行前的準(zhǔn)備工作,類(lèi)的實(shí)例化(實(shí)例化對(duì)象)是指創(chuàng)建一個(gè)對(duì)象的過(guò)程,需要的朋友可以參考下
    2023-08-08
  • Java使用Lua實(shí)現(xiàn)動(dòng)態(tài)擴(kuò)展和腳本自動(dòng)升級(jí)

    Java使用Lua實(shí)現(xiàn)動(dòng)態(tài)擴(kuò)展和腳本自動(dòng)升級(jí)

    Lua是一種輕量級(jí)的腳本語(yǔ)言,常用于游戲開(kāi)發(fā)和嵌入式系統(tǒng)中,這篇文章主要介紹了Java如何調(diào)用Lua實(shí)現(xiàn)動(dòng)態(tài)擴(kuò)展和腳本自動(dòng)升級(jí),感興趣的可以學(xué)習(xí)下
    2023-08-08
  • 一文教會(huì)你使用jmap和MAT進(jìn)行堆內(nèi)存溢出分析

    一文教會(huì)你使用jmap和MAT進(jìn)行堆內(nèi)存溢出分析

    本文介紹關(guān)于jmap和MAT的使用來(lái)進(jìn)行堆內(nèi)存溢出分析,因?yàn)檫@個(gè)內(nèi)存溢出是我們手動(dòng)構(gòu)造出來(lái)的,查找比較簡(jiǎn)單,真的到了生產(chǎn)上面需要我們仔細(xì)排除
    2021-09-09
  • Java?枚舉的常用技巧匯總

    Java?枚舉的常用技巧匯總

    在Java中,枚舉類(lèi)型是一種特殊的數(shù)據(jù)類(lèi)型,允許定義一組固定的常量,默認(rèn)情況下,toString方法返回枚舉常量的名稱(chēng),本文提供了一個(gè)完整的代碼示例,展示了如何在Java中通過(guò)重寫(xiě)枚舉的toString方法來(lái)展示枚舉實(shí)例的字段信息,感興趣的朋友一起看看吧
    2025-01-01
  • C++字符串的處理詳解

    C++字符串的處理詳解

    這篇文章主要介紹了C++ string字符串類(lèi),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-08-08
  • SpringCloud負(fù)載均衡spring-cloud-starter-loadbalancer解讀

    SpringCloud負(fù)載均衡spring-cloud-starter-loadbalancer解讀

    這篇文章主要介紹了SpringCloud負(fù)載均衡spring-cloud-starter-loadbalancer使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-03-03
  • springboot集成本地緩存Caffeine的三種使用方式(小結(jié))

    springboot集成本地緩存Caffeine的三種使用方式(小結(jié))

    本文主要介紹了springboot集成本地緩存Caffeine的三種使用方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • Java流程控制語(yǔ)句之If選擇結(jié)構(gòu)

    Java流程控制語(yǔ)句之If選擇結(jié)構(gòu)

    今天繼續(xù)帶大家復(fù)習(xí)Java流程控制語(yǔ)句的相關(guān)知識(shí),本文對(duì)If選擇結(jié)構(gòu)作了非常詳細(xì)的介紹及代碼示例,對(duì)正在學(xué)習(xí)的小伙伴們很有幫助,需要的朋友可以參考下
    2021-06-06

最新評(píng)論