日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁技術文章
文章詳情頁

Spring Security OAuth 自定義授權方式實現手機驗證碼

瀏覽:16日期:2023-07-24 09:55:23

Spring Security OAuth 默認提供OAuth2.0 的四大基本授權方式(authorization_codeimplicitpasswordclient_credential),除此之外我們也能夠自定義授權方式。

先了解一下Spring Security OAuth提供的兩個默認 Endpoints,一個是AuthorizationEndpoint,這個是僅用于授權碼(authorization_code)和簡化(implicit)模式的。另外一個是TokenEndpoint,用于OAuth2授權時下發Token,根據授予類型(GrantType)的不同而執行不同的驗證方式。

OAuth2協議這里就不做過多介紹了,比較重要的一點是理解認證中各個角色的作用,以及認證的目的(獲取用戶信息或是具備使用API的權限)。例如在authorization_code模式下,用戶(User)在認證服務的網站上進行登錄,網站跳轉回第三方應用(Client),第三方應用通過Secret和Code換取Token后向資源服務請求用戶信息;而在client_credential模式下,第三方應用通過Secret直接獲得Token后可以直接利用其訪問資源API。所以我們應該根據實際的情景選擇適合的認證模式。

對于手機驗證碼的認證模式,我們首先提出短信驗證的通常需求:

每發一次驗證碼只能嘗試驗證5次,防止暴力破解 限制驗證碼發送頻率,單個用戶(這里簡單使用手機號區分)1分鐘1條,24小時x條 限制驗證碼有效期,15分鐘

我們根據業務需求構造出對應的模型:

@Datapublic class SmsVerificationModel { /** * 手機號 */ private String phoneNumber; /** * 驗證碼 */ private String captcha; /** * 本次驗證碼驗證失敗次數,防止暴力嘗試 */ private Integer failCount; /** * 該user當日嘗試次數,防止濫發短信 */ private Integer dailyCount; /** * 限制短信發送頻率和實現驗證碼有效期 */ private Date lastSentTime; /** * 是否驗證成功 */ private Boolean verified = false;}

我們預想的認證流程:

Spring Security OAuth 自定義授權方式實現手機驗證碼

接下來要對Spring Security OAuth進行定制,這里直接仿照一個比較相似的password模式,首先需要編寫一個新的TokenGranter,處理sms類型下的TokenRequest,這個SmsTokenGranter會生成SmsAuthenticationToken,并將AuthenticationToken交由SmsAuthenticationProvider進行驗證,驗證成功后生成通過驗證的SmsAuthenticationToken,完成Token的頒發。

public class SmsTokenGranter extends AbstractTokenGranter { private static final String GRANT_TYPE = 'sms'; private final AuthenticationManager authenticationManager; public SmsTokenGranter(AuthenticationManager authenticationManager, AuthorizationServerTokenServices tokenServices, ClientDetailsService clientDetailsService, OAuth2RequestFactory requestFactory){ super(tokenServices, clientDetailsService, requestFactory, GRANT_TYPE); this.authenticationManager = authenticationManager; } @Override protected OAuth2Authentication getOAuth2Authentication(ClientDetails client, TokenRequest tokenRequest) { Map<String, String> parameters = new LinkedHashMap<>(tokenRequest.getRequestParameters()); String phone = parameters.get('phone'); String code = parameters.get('code'); Authentication userAuth = new SmsAuthenticationToken(phone, code); try { userAuth = authenticationManager.authenticate(userAuth); } catch (AccountStatusException ase) { throw new InvalidGrantException(ase.getMessage()); } catch (BadCredentialsException e) { throw new InvalidGrantException(e.getMessage()); } if (userAuth == null || !userAuth.isAuthenticated()) { throw new InvalidGrantException('Could not authenticate user: ' + username); } OAuth2Request storedOAuth2Request = getRequestFactory().createOAuth2Request(client, tokenRequest); return new OAuth2Authentication(storedOAuth2Request, userAuth); }}

對應的SmsAuthenticationToken,其中一個構造方法是認證后的。

public class SmsAuthenticationToken extends AbstractAuthenticationToken { private final Object principal; private Object credentials; public SmsAuthenticationToken(Object principal, Object credentials) { super(null); this.credentials = credentials; this.principal = principal; } public SmsAuthenticationToken(Object principal, Object credentials,Collection<? extends GrantedAuthority> authorities) { super(authorities); this.principal = principal; this.credentials = credentials; // 表示已經認證 super.setAuthenticated(true); } ...}

SmsAuthenticationProvider是仿照AbstractUserDetailsAuthenticationProvider編寫的,這里僅僅列出核心部分。

public class SmsAuthenticationProvider implements AuthenticationProvider { @Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { String username = authentication.getName(); UserDetails user = retrieveUser(username); preAuthenticationChecks.check(user); String phoneNumber = authentication.getPrincipal().toString(); String code = authentication.getCredentials().toString(); // 嘗試從Redis中取出Model SmsVerificationModel verificationModel =Optional.ofNullable( redisService.get(SMS_REDIS_PREFIX + phoneNumber, SmsVerificationModel.class)).orElseThrow(() -> new BusinessException(OAuthError.SMS_VERIFY_BEFORE_SEND)); // 判斷短信驗證次數 Optional.of(verificationModel).filter(x -> x.getFailCount() < SMS_VERIFY_FAIL_MAX_TIMES).orElseThrow(() -> new BusinessException(OAuthError.SMS_VERIFY_COUNT_EXCEED)); Optional.of(verificationModel).map(SmsVerificationModel::getLastSentTime)// 驗證碼發送15分鐘內有效,等價于發送時間加上15分鐘晚于當下.filter(x -> DateUtils.addMinutes(x,15).after(new Date())).orElseThrow(() -> new BusinessException(OAuthError.SMS_CODE_EXPIRED)); verificationModel.setVerified(Objects.equals(code, verificationModel.getCaptcha())); verificationModel.setFailCount(verificationModel.getFailCount() + 1); redisService.set(SMS_REDIS_PREFIX + phoneNumber, verificationModel, 1, TimeUnit.DAYS); if(!verificationModel.getVerified()){ throw new BusinessException(OAuthError.SmsCodeWrong); } postAuthenticationChecks.check(user); return createSuccessAuthentication(user, authentication, user); } ...

接下來要通過配置啟用我們定制的類,首先配置AuthorizationServerEndpointsConfigurer,添加上我們的TokenGranter,然后是AuthenticationManagerBuilder,添加我們的AuthenticationProvider。

@Configuration@EnableAuthorizationServerpublic class OAuth2Config extends AuthorizationServerConfigurerAdapter { @Override public void configure(AuthorizationServerSecurityConfigurer security) throws Exception { security.passwordEncoder(passwordEncoder).checkTokenAccess('isAuthenticated()').tokenKeyAccess('permitAll()')// 允許使用Query字段驗證客戶端,即client_id/client_secret 能夠放在查詢參數中.allowFormAuthenticationForClients(); } @Override public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { endpoints.authenticationManager(authenticationManager).userDetailsService(userDetailsService).tokenStore(tokenStore); List<TokenGranter> tokenGranters = new ArrayList<>(); tokenGranters.add(new AuthorizationCodeTokenGranter(endpoints.getTokenServices(), endpoints.getAuthorizationCodeServices(), clientDetailsService,endpoints.getOAuth2RequestFactory())); ... tokenGranters.add(new SmsTokenGranter(authenticationManager, endpoints.getTokenServices(),clientDetailsService, endpoints.getOAuth2RequestFactory())); endpoints.tokenGranter(new CompositeTokenGranter(tokenGranters)); }}

@EnableWebSecurity@Configurationpublic class SecurityConfig extends WebSecurityConfigurerAdapter { ... @Override protected void configure(AuthenticationManagerBuilder auth) { auth.authenticationProvider(daoAuthenticationProvider()); } @Bean public AuthenticationProvider smsAuthenticationProvider(){ SmsAuthenticationProvider smsAuthenticationProvider = new SmsAuthenticationProvider(); smsAuthenticationProvider.setUserDetailsService(userDetailsService); smsAuthenticationProvider.setSmsAuthService(smsAuthService); return smsAuthenticationProvider; }}

那么短信驗證碼授權的部分就到這里了,最后還有一個發送短信的接口,這里就不展示了。

最后測試一下,curl --location --request POST ’http://localhost:8080/oauth/token?grant_type=sms&client_id=XXX&phone=手機號&code=驗證碼’ ,成功。

{ 'access_token': '39bafa9a-7e5b-4ba4-9eda-e307ac98aad1', 'token_type': 'bearer', 'expires_in': 3599, 'scope': 'ALL'}

到此這篇關于Spring Security OAuth 自定義授權方式實現手機驗證碼的文章就介紹到這了,更多相關Spring Security OAuth手機驗證碼內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Spring
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
成人污污视频| 国产日产一区| 三级在线看中文字幕完整版| 国产欧美日韩一区二区三区在线| 青青国产91久久久久久| 国产情侣一区| 波多野结衣久久精品| 日韩精品欧美| 爽好多水快深点欧美视频| 亚洲激情中文| 日本中文字幕一区二区| 麻豆成人91精品二区三区| 日韩综合精品| 中文视频一区| 欧美aa在线视频| 亚洲不卡av不卡一区二区| 久热精品在线| 国产精品3区| 麻豆理论在线观看| 亚洲免费一区二区| 国产剧情在线观看一区| 人人香蕉久久| 日本一区免费网站| 国产成人精品一区二区三区在线| caoporn视频在线| 中文精品视频| 美女视频免费精品| 国产视频一区在线观看一区免费| 日本不卡一二三区黄网| 精品一二三区| 午夜亚洲一区| 国产精品99久久久久久董美香| 99精品在线观看| 日本午夜精品| 久久精品亚洲人成影院| 日本午夜精品久久久久| 亚洲成人不卡| 青青国产精品| 久久美女性网| 日韩区一区二| 黑丝美女一区二区| 国产视频一区二| 欧美大黑bbbbbbbbb在线| 欧美精品三级在线| 不卡中文字幕| 毛片在线网站| 日本中文字幕一区二区视频 | 视频一区二区欧美| 久久这里只有| 蜜桃av一区二区| 欧美片第1页| 日本欧美一区二区| 九色精品91| 在线中文字幕播放| 日韩精品久久理论片| 亚洲高清激情| 成人在线丰满少妇av| 欧美一级二级视频| 国产午夜久久| 天堂8中文在线最新版在线| 日本欧美在线| 国产模特精品视频久久久久| 国产伦久视频在线观看| 国产精品一区二区av日韩在线| 丝袜a∨在线一区二区三区不卡 | 欧美成人精品一级| 亚洲18在线| 亚洲一区二区网站| 成人啊v在线| 免费视频一区二区三区在线观看 | 欧美精品99| 91亚洲精品视频在线观看| 国产精品日本欧美一区二区三区| 国产超碰精品| 日韩欧美精品综合| 麻豆精品一区二区综合av| 婷婷精品久久久久久久久久不卡| 偷拍欧美精品| 久久久成人网| 麻豆成全视频免费观看在线看| 美女在线视频一区| 国产精品久久久久久久久久白浆| 日韩三级一区| 日本欧美大码aⅴ在线播放| 亚洲精品九九| 深夜福利一区| 日本不卡高清| 国产亚洲欧美日韩精品一区二区三区| 日韩欧美2区| 日韩中文字幕无砖| 亚洲欧美日韩综合国产aⅴ| 在线国产一区二区| 合欧美一区二区三区| 亚洲国产一区二区三区在线播放| 午夜久久一区| 日韩精品一二区| 亚洲一区二区av| 亚洲青青久久| 日本强好片久久久久久aaa| 国产色噜噜噜91在线精品| 日韩激情精品| 国产精品极品在线观看| 久久三级中文| 中文字幕在线看片| 免费在线小视频| 亚洲精品一区三区三区在线观看| 99精品视频精品精品视频| 午夜久久黄色| 日韩国产一二三区| 国产福利资源一区| 日韩av免费大片| 亚洲国产专区| 中文不卡在线| 国产乱码精品一区二区亚洲| 精品国产美女a久久9999| 国产h片在线观看| 精品一区在线| 婷婷综合电影| 国产一区二区视频在线看| 精品三级久久| 999久久久国产精品| 免费不卡在线观看| 国产激情久久| 激情久久五月| 视频一区日韩精品| 久久中文字幕一区二区三区| 久久91导航| 在线国产精品一区| 麻豆精品新av中文字幕| 欧美日韩一区二区综合 | 伊人精品视频| 国产日韩欧美三级| 日韩深夜视频| 免费欧美日韩| 91p九色成人| 日产精品一区| 日韩在线视频一区二区三区| 韩国女主播一区二区三区| 国产精品91一区二区三区| 日本在线视频一区二区| 欧美韩一区二区| 国产精品7m凸凹视频分类| 国产亚洲一区| 国产综合激情| 久久狠狠久久| 国产韩日影视精品| 麻豆视频一区| 激情欧美国产欧美| 欧美中文一区| 欧美日韩一区二区三区视频播放| 日韩综合一区二区| 日本久久成人网| 国产日韩三级| 另类av一区二区| 色婷婷色综合| 日韩精品一区二区三区中文 | 日韩av自拍| 色8久久久久| 999久久久免费精品国产| 国产欧美日韩一区二区三区四区| 亚洲精品va| 精品高清久久| 亚洲资源在线| 亚洲成av人片一区二区密柚| 国产精品a久久久久| 亚洲另类视频| 亚洲精品一区二区妖精| 高清一区二区三区| 久久精品凹凸全集| 蜜臀av国产精品久久久久| 成人日韩在线| 麻豆成人在线观看| 亚洲九九精品| 国产综合色产| 国产一区二区三区不卡视频网站| 日韩欧美四区| 香蕉成人久久| 欧美羞羞视频| 麻豆91小视频| 日本久久一区| 蜜臀精品一区二区三区在线观看 | 日本色综合中文字幕| 国产亚洲欧洲| 欧洲毛片在线视频免费观看| 成人小电影网站| 国产福利资源一区| 亚洲精品系列| 在线视频日韩| 激情欧美亚洲| 亚洲天堂久久| 韩国精品主播一区二区在线观看| 精品国产99| 激情久久99| 久久精品国产在热久久| 国产精品黄色| 毛片不卡一区二区| 精品免费在线| 丁香婷婷久久| 日韩伦理在线一区| 91精品蜜臀一区二区三区在线|