激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務器之家 - 編程語言 - Java教程 - SpringBoot + SpringSecurity 短信驗證碼登錄功能實現

SpringBoot + SpringSecurity 短信驗證碼登錄功能實現

2021-05-09 12:27whyalwaysmea Java教程

這篇文章主要介紹了SpringBoot + SpringSecurity 短信驗證碼登錄功能實現,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

實現原理

在之前的文章中,我們介紹了普通的帳號密碼登錄的方式: SpringBoot + Spring Security 基本使用及個性化登錄配置。 但是現在還有一種常見的方式,就是直接通過手機短信驗證碼登錄,這里就需要自己來做一些額外的工作了。

SpringSecurity認證流程詳解有一定了解的都知道,在帳號密碼認證的過程中,涉及到了以下幾個類:UsernamePasswordAuthenticationFilter(用于請求參數獲取),UsernamePasswordAuthenticationToken(表示用戶登錄信息),ProviderManager(進行認證校驗),

因為是通過的短信驗證碼登錄,所以我們需要對請求的參數,認證過程,用戶登錄Token信息進行一定的重寫。
當然驗證碼的過程我們應該放在最前面,如果圖形驗證碼的實現一樣。這樣的做法的好處是:將驗證碼認證該過程解耦出來,讓其他接口也可以使用到。

基本實現

驗證碼校驗

短信驗證碼的功能實現,其實和圖形驗證碼的原理是一樣的。只不過一個是返回給前端一個圖片,一個是給用戶發送短消息,這里只需要去調用一下短信服務商的接口就好了。更多的原理可以參考 SpringBoot + SpringSecurity 實現圖形驗證碼功能

AuthenticationToken

在使用帳號密碼登錄的時候,UsernamePasswordAuthenticationToken里面包含了用戶的帳號,密碼,以及其他的是否可用等狀態信息。我們是通過手機短信來做登錄,所以就沒有密碼了,這里我們就直接將UsernamePasswordAuthenticationToken的代碼copy過來,把密碼相關的信息去掉就可以了

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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

在帳戶密碼登錄的流程中,默認使用的是UsernamePasswordAuthenticationFilter,它的作用是從請求中獲取帳戶、密碼,請求方式校驗,生成AuthenticationToken。這里我們的參數是有一定改變的,所以還是老方法,copy過來進行簡單的修改

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
public class SmsCodeAuthenticationFilter extends AbstractAuthenticationProcessingFilter {
  // 請求參數key
  private String mobileParameter = SecurityConstants.DEFAULT_PARAMETER_NAME_MOBILE;
  // 是否只支持POST
  private boolean postOnly = true;
 
  public SmsCodeAuthenticationFilter() {
    // 請求接口的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());
    }
    // 根據請求參數名,獲取請求value
    String mobile = obtainMobile(request);
    if (mobile == null) {
      mobile = "";
    }
    mobile = mobile.trim();
 
    // 生成對應的AuthenticationToken
    SmsCodeAuthenticationToken authRequest = new SmsCodeAuthenticationToken(mobile);
 
    setDetails(request, authRequest);
 
    return this.getAuthenticationManager().authenticate(authRequest);
  }
 
  /**
   * 獲取手機號
   */
  protected String obtainMobile(HttpServletRequest request) {
    return request.getParameter(mobileParameter);
  }
  // 省略不相關代碼
}

Provider

在帳號密碼登錄的過程中,密碼的正確性以及帳號是否可用是通過DaoAuthenticationProvider來校驗的。我們也應該自己實現一個Provier

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
public class SmsCodeAuthenticationProvider implements AuthenticationProvider {
 
  private UserDetailsService userDetailsService;
 
  /**
   * 身份邏輯驗證
   * @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("無法獲取用戶信息");
    }
 
    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;
  }
}

配置

主要的認證流程就是通過以上四個過程實現的, 這里我們再降它們配置一下就可以了

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
@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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://blog.csdn.net/u013435893/article/details/79684027

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 天天草天天干天天 | 999久久国精品免费观看网站 | 日韩在线播放中文字幕 | 精品国产91久久久久 | 91九色蝌蚪国产 | 99在线精品视频免费观看20 | 色就色 综合偷拍区91网 | 国产一级毛片视频在线! | 精品一区二区三区免费爱 | 草操视频 | 成人毛片视频在线观看 | 国产一级在线观看视频 | 精品国产一区二区三区四区在线 | 国产精品伊人久久 | 国产一国产精品一级毛片 | 日韩大片在线永久观看视频网站免费 | 一区小视频 | 在线免费av观看 | 欧美一级毛片大片免费播放 | 黄在线观看在线播放720p | 日本一区二区不卡在线观看 | 特级黄色一级毛片 | 毛片久久 | 中文字幕网址 | 亚洲国产精久久久久久久 | 羞羞羞网站 | 毛片哪里看 | 欧美一级毛片一级毛片 | 欧美a∨一区二区三区久久黄 | 精品国产乱码久久久久久久 | av懂色| av免费提供 | a黄在线观看 | 国产一级做a爱片在线看免 日日草夜夜 | 久久亚洲美女视频 | 一区二区三区欧美在线观看 | 午夜小视频免费观看 | 12av电影 | 激情网站免费观看 | 国产九色视频在线观看 | 一级做人爱c黑人影片 |