亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 開發 > Java > 正文

Spring Security Oauth2.0 實現短信驗證碼登錄示例

2024-07-13 10:16:31
字體:
來源:轉載
供稿:網友

本文介紹了Spring Security Oauth2.0 實現短信驗證碼登錄示例,分享給大家,具體如下:

Spring,Security,Oauth2.0,登錄

定義手機號登錄令牌

/** * @author lengleng * @date 2018/1/9 * 手機號登錄令牌 */public class MobileAuthenticationToken extends AbstractAuthenticationToken {  private static final long serialVersionUID = SpringSecurityCoreVersion.SERIAL_VERSION_UID;  private final Object principal;  public MobileAuthenticationToken(String mobile) {    super(null);    this.principal = mobile;    setAuthenticated(false);  }  public MobileAuthenticationToken(Object principal,                   Collection<? extends GrantedAuthority> authorities) {    super(authorities);    this.principal = principal;    super.setAuthenticated(true);  }  public Object getPrincipal() {    return this.principal;  }  @Override  public Object getCredentials() {    return null;  }  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();  }}

手機號登錄校驗邏輯

/** * @author lengleng * @date 2018/1/9 * 手機號登錄校驗邏輯 */public class MobileAuthenticationProvider implements AuthenticationProvider {  private UserService userService;  @Override  public Authentication authenticate(Authentication authentication) throws AuthenticationException {    MobileAuthenticationToken mobileAuthenticationToken = (MobileAuthenticationToken) authentication;    UserVo userVo = userService.findUserByMobile((String) mobileAuthenticationToken.getPrincipal());    UserDetailsImpl userDetails = buildUserDeatils(userVo);    if (userDetails == null) {      throw new InternalAuthenticationServiceException("手機號不存在:" + mobileAuthenticationToken.getPrincipal());    }    MobileAuthenticationToken authenticationToken = new MobileAuthenticationToken(userDetails, userDetails.getAuthorities());    authenticationToken.setDetails(mobileAuthenticationToken.getDetails());    return authenticationToken;  }  private UserDetailsImpl buildUserDeatils(UserVo userVo) {    return new UserDetailsImpl(userVo);  }  @Override  public boolean supports(Class<?> authentication) {    return MobileAuthenticationToken.class.isAssignableFrom(authentication);  }  public UserService getUserService() {    return userService;  }  public void setUserService(UserService userService) {    this.userService = userService;  }}

登錄過程filter處理

/** * @author lengleng * @date 2018/1/9 * 手機號登錄驗證filter */public class MobileAuthenticationFilter extends AbstractAuthenticationProcessingFilter {  public static final String SPRING_SECURITY_FORM_MOBILE_KEY = "mobile";  private String mobileParameter = SPRING_SECURITY_FORM_MOBILE_KEY;  private boolean postOnly = true;  public MobileAuthenticationFilter() {    super(new AntPathRequestMatcher(SecurityConstants.MOBILE_TOKEN_URL, "POST"));  }  public Authentication attemptAuthentication(HttpServletRequest request,                        HttpServletResponse response) throws AuthenticationException {    if (postOnly && !request.getMethod().equals(HttpMethod.POST.name())) {      throw new AuthenticationServiceException(          "Authentication method not supported: " + request.getMethod());    }    String mobile = obtainMobile(request);    if (mobile == null) {      mobile = "";    }    mobile = mobile.trim();    MobileAuthenticationToken mobileAuthenticationToken = new MobileAuthenticationToken(mobile);    setDetails(request, mobileAuthenticationToken);    return this.getAuthenticationManager().authenticate(mobileAuthenticationToken);  }  protected String obtainMobile(HttpServletRequest request) {    return request.getParameter(mobileParameter);  }  protected void setDetails(HttpServletRequest request,               MobileAuthenticationToken authRequest) {    authRequest.setDetails(authenticationDetailsSource.buildDetails(request));  }  public void setPostOnly(boolean postOnly) {    this.postOnly = postOnly;  }  public String getMobileParameter() {    return mobileParameter;  }  public void setMobileParameter(String mobileParameter) {    this.mobileParameter = mobileParameter;  }  public boolean isPostOnly() {    return postOnly;  }}

生產token 位置

/** * @author lengleng * @date 2018/1/8 * 手機號登錄成功,返回oauth token */@Componentpublic class MobileLoginSuccessHandler implements org.springframework.security.web.authentication.AuthenticationSuccessHandler {  private Logger logger = LoggerFactory.getLogger(getClass());  @Autowired  private ObjectMapper objectMapper;  @Autowired  private ClientDetailsService clientDetailsService;  @Autowired  private AuthorizationServerTokenServices authorizationServerTokenServices;  @Override  public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {    String header = request.getHeader("Authorization");    if (header == null || !header.startsWith("Basic ")) {      throw new UnapprovedClientAuthenticationException("請求頭中client信息為空");    }    try {      String[] tokens = extractAndDecodeHeader(header);      assert tokens.length == 2;      String clientId = tokens[0];      String clientSecret = tokens[1];      JSONObject params = new JSONObject();      params.put("clientId", clientId);      params.put("clientSecret", clientSecret);      params.put("authentication", authentication);      ClientDetails clientDetails = clientDetailsService.loadClientByClientId(clientId);      TokenRequest tokenRequest = new TokenRequest(MapUtil.newHashMap(), clientId, clientDetails.getScope(), "mobile");      OAuth2Request oAuth2Request = tokenRequest.createOAuth2Request(clientDetails);      OAuth2Authentication oAuth2Authentication = new OAuth2Authentication(oAuth2Request, authentication);      OAuth2AccessToken oAuth2AccessToken = authorizationServerTokenServices.createAccessToken(oAuth2Authentication);      logger.info("獲取token 成功:{}", oAuth2AccessToken.getValue());      response.setCharacterEncoding(CommonConstant.UTF8);      response.setContentType(CommonConstant.CONTENT_TYPE);      PrintWriter printWriter = response.getWriter();      printWriter.append(objectMapper.writeValueAsString(oAuth2AccessToken));    } catch (IOException e) {      throw new BadCredentialsException(          "Failed to decode basic authentication token");    }  }  /**   * Decodes the header into a username and password.   *   * @throws BadCredentialsException if the Basic header is not present or is not valid   *                 Base64   */  private String[] extractAndDecodeHeader(String header)      throws IOException {    byte[] base64Token = header.substring(6).getBytes("UTF-8");    byte[] decoded;    try {      decoded = Base64.decode(base64Token);    } catch (IllegalArgumentException e) {      throw new BadCredentialsException(          "Failed to decode basic authentication token");    }    String token = new String(decoded, CommonConstant.UTF8);    int delim = token.indexOf(":");    if (delim == -1) {      throw new BadCredentialsException("Invalid basic authentication token");    }    return new String[]{token.substring(0, delim), token.substring(delim + 1)};  }}

配置以上自定義

//** * @author lengleng * @date 2018/1/9 * 手機號登錄配置入口 */@Componentpublic class MobileSecurityConfigurer extends SecurityConfigurerAdapter<DefaultSecurityFilterChain, HttpSecurity> {  @Autowired  private MobileLoginSuccessHandler mobileLoginSuccessHandler;  @Autowired  private UserService userService;  @Override  public void configure(HttpSecurity http) throws Exception {    MobileAuthenticationFilter mobileAuthenticationFilter = new MobileAuthenticationFilter();    mobileAuthenticationFilter.setAuthenticationManager(http.getSharedObject(AuthenticationManager.class));    mobileAuthenticationFilter.setAuthenticationSuccessHandler(mobileLoginSuccessHandler);    MobileAuthenticationProvider mobileAuthenticationProvider = new MobileAuthenticationProvider();    mobileAuthenticationProvider.setUserService(userService);    http.authenticationProvider(mobileAuthenticationProvider)        .addFilterAfter(mobileAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);  }}

在spring security 配置 上邊定一個的那個聚合配置

/** * @author lengleng * @date 2018年01月09日14:01:25 * 認證服務器開放接口配置 */@Configuration@EnableResourceServerpublic class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {  @Autowired  private FilterUrlsPropertiesConifg filterUrlsPropertiesConifg;  @Autowired  private MobileSecurityConfigurer mobileSecurityConfigurer;  @Override  public void configure(HttpSecurity http) throws Exception {    registry        .antMatchers("/mobile/token").permissionAll()        .anyRequest().authenticated()        .and()        .csrf().disable();    http.apply(mobileSecurityConfigurer);  }}

使用

 

復制代碼 代碼如下:

curl -H "Authorization:Basic cGlnOnBpZw==" -d "grant_type=mobile&scope=server&mobile=17034642119&code=" http://localhost:9999/auth/mobile/token

 

源碼

請參考gitee.com/log4j/

基于Spring Cloud、Spring Security Oauth2.0開發企業級認證與授權,提供常見服務監控、鏈路追蹤、日志分析、緩存管理、任務調度等實現

整個邏輯是參考spring security 自身的 usernamepassword 登錄模式實現,可以參考其源碼。

驗證碼的發放、校驗邏輯比較簡單,方法后通過全局fiter 判斷請求中code 是否和 手機號匹配集合,重點邏輯是令牌的參數

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。


注:相關教程知識閱讀請移步到JAVA教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
国产一区二区三区直播精品电影| xxxxx91麻豆| 中日韩午夜理伦电影免费| 国产精品wwwwww| 68精品国产免费久久久久久婷婷| 亚洲国产91精品在线观看| 亚洲精品国产精品国自产观看浪潮| 国产剧情久久久久久| 国产一区二区三区三区在线观看| 欧美怡春院一区二区三区| 国产精品自产拍在线观| 97免费中文视频在线观看| 国产精品成人一区二区| 亚洲性视频网站| 国产精品久久久久久久久粉嫩av| 亚洲日韩欧美视频一区| 精品人伦一区二区三区蜜桃免费| 精品国产欧美一区二区三区成人| 国产精品第3页| 欧美高清不卡在线| 亚洲欧美日本伦理| 国产97免费视| 精品在线欧美视频| 97超级碰碰碰| 精品亚洲一区二区三区| 日韩中文在线不卡| 欧美最猛性xxxxx亚洲精品| 国产精品九九久久久久久久| 欧美成人黄色小视频| 欧美大学生性色视频| 久久视频在线看| 欧美影院久久久| 国产精品免费在线免费| 欧美乱大交xxxxx另类电影| 成人黄色片在线| 日本高清不卡的在线| 欧美日韩精品在线观看| 精品久久久国产| 亚洲电影中文字幕| 亚洲女人天堂成人av在线| 97精品伊人久久久大香线蕉| 亚洲图片欧洲图片av| 国产精品久久久久久久一区探花| 亚洲电影av在线| 欧美性猛交xxxx免费看| 在线视频日韩精品| 国产一区二中文字幕在线看| 欧美日韩人人澡狠狠躁视频| 亚洲一区中文字幕| 亚洲精品久久久久久久久| 国产成人91久久精品| 欧美在线激情网| 欧美最近摘花xxxx摘花| 成人免费福利视频| 欧美成人四级hd版| 亚洲第一综合天堂另类专| 久久精品影视伊人网| 精品国产一区二区三区久久狼5月| 97超级碰在线看视频免费在线看| 97精品国产97久久久久久春色| 97精品欧美一区二区三区| 久久精品欧美视频| 国产精品视频中文字幕91| 久久精品夜夜夜夜夜久久| 精品久久久久久久久久久久| 热99在线视频| 国产精品久久中文| 日韩亚洲一区二区| 欧美性视频在线| 精品性高朝久久久久久久| 国产午夜精品一区二区三区| 在线观看欧美www| 日韩二区三区在线| 久久精品视频网站| 欧美性生交大片免费| 亚洲伊人第一页| 黑人精品xxx一区| 91亚洲一区精品| 色老头一区二区三区在线观看| 久久久亚洲福利精品午夜| 久久99精品久久久久久青青91| 日本久久久久久久| 亚洲欧美日韩国产成人| 韩曰欧美视频免费观看| 国内精品美女av在线播放| 日本国产欧美一区二区三区| 亚洲精品久久久久久下一站| 日韩电影免费观看在线| 欧美成人精品一区二区| 亚洲欧美在线一区| 正在播放亚洲1区| 中文字幕欧美日韩在线| 国产精品扒开腿做爽爽爽的视频| 中文字幕九色91在线| 午夜免费在线观看精品视频| 欧美性猛交xxxx乱大交| 久久久久久久国产精品| 欧美日韩色婷婷| 久久精品国产亚洲一区二区| 亚洲一区二区在线| 国产91在线播放精品91| 亚洲人成啪啪网站| 成人精品久久久| 狠狠综合久久av一区二区小说| 亚洲人a成www在线影院| 国产在线精品一区免费香蕉| 日韩精品在线免费播放| 国产视频自拍一区| yw.139尤物在线精品视频| 国产美女搞久久| 欧美日韩国产中文精品字幕自在自线| 高清亚洲成在人网站天堂| 国产亚洲人成网站在线观看| 亚洲黄色www| 国产精品久久久久免费a∨大胸| 欧美激情亚洲激情| 91精品在线影院| 伊人一区二区三区久久精品| 国产精品吊钟奶在线| 国产欧美精品日韩精品| 久久久久久久亚洲精品| 黄色成人在线免费| 久久艳片www.17c.com| 日韩精品中文字幕视频在线| xxxx欧美18另类的高清| 欧美日韩一区二区三区| 91久久在线观看| 亚洲精品欧美一区二区三区| 日韩有码在线播放| 狠狠做深爱婷婷久久综合一区| 国产91色在线|免| 日韩精品亚洲精品| 国产丝袜一区二区三区免费视频| 久久精品国产一区二区电影| 永久免费毛片在线播放不卡| 日韩中文字幕第一页| 中日韩美女免费视频网址在线观看| 国产小视频国产精品| 成人久久18免费网站图片| 亚洲丝袜一区在线| 26uuu另类亚洲欧美日本老年| 亚洲国产欧美一区二区三区同亚洲| 亚洲国产日韩欧美在线99| 热久久视久久精品18亚洲精品| 国产日韩亚洲欧美| 欧美午夜无遮挡| 亚洲国产私拍精品国模在线观看| 日韩暖暖在线视频| 国产主播在线一区| 久久久视频在线| 日本视频久久久| 亚洲国产高清自拍| 奇米影视亚洲狠狠色| 亚洲国产成人久久| 亚洲国产精品悠悠久久琪琪| 亚洲欧美日韩第一区| 91爱爱小视频k| 国产精品久久国产精品99gif| 欧美日韩中国免费专区在线看| 久久精品国产清自在天天线| 国产日韩综合一区二区性色av| 中文字幕日韩精品在线观看| 91精品视频免费观看| 国产午夜精品久久久|