成人在线亚洲_国产日韩视频一区二区三区_久久久国产精品_99国内精品久久久久久久

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

Spring Security OAuth 自定義授權方式實現(xiàn)手機驗證碼

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

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

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

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

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

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

我們根據(jù)業(yè)務需求構造出對應的模型:

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

我們預想的認證流程:

Spring Security OAuth 自定義授權方式實現(xiàn)手機驗證碼

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

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; // 表示已經(jīng)認證 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)); // 判斷短信驗證次數(shù) 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)// 驗證碼發(fā)送15分鐘內(nèi)有效,等價于發(fā)送時間加上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 能夠放在查詢參數(shù)中.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; }}

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

最后測試一下,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 自定義授權方式實現(xiàn)手機驗證碼的文章就介紹到這了,更多相關Spring Security OAuth手機驗證碼內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持好吧啦網(wǎng)!

標簽: Spring
相關文章:
成人在线亚洲_国产日韩视频一区二区三区_久久久国产精品_99国内精品久久久久久久
91在线视频在线| 国产在线国偷精品免费看| 天天免费综合色| 亚洲欧洲一区二区天堂久久| 国产精品网站在线播放| 99re这里只有精品首页| 日韩午夜三级在线| 国产精品亚洲午夜一区二区三区| 欧美日韩一区二区三区四区| 日韩高清在线不卡| 色综合久久久久综合体桃花网| 亚洲一区二区精品3399| 最新亚洲激情| 亚洲欧美在线另类| 欧美午夜精品理论片a级大开眼界 欧美午夜精品久久久久免费视 | 99re成人精品视频| 久久夜色精品国产欧美乱极品| 成人黄色在线网站| 精品久久久三级丝袜| 成人18精品视频| 久久嫩草精品久久久精品一| 99精品国产99久久久久久白柏| 精品免费国产二区三区| 成人av网站免费| 欧美www视频| 成人黄页毛片网站| 国产日韩成人精品| 亚洲一级特黄| 亚洲精品高清在线观看| 亚洲免费一区二区| 日韩激情一区二区| 欧美怡红院视频| 精品在线播放午夜| 日韩欧美自拍偷拍| 91在线观看视频| 中文字幕av一区二区三区高 | 在线观看www91| 国精品**一区二区三区在线蜜桃| 欧美一区二区视频观看视频| 高清beeg欧美| 久久精品日产第一区二区三区高清版| 欧美日韩天堂| 亚洲精品日韩综合观看成人91| 国产精品一区视频| 蜜臀av性久久久久av蜜臀妖精| 欧美日本在线看| 成人av网在线| 亚洲欧美在线高清| 色欧美乱欧美15图片| 国产黄色91视频| 国产人久久人人人人爽| 亚洲高清二区| 美日韩黄色大片| 精品国产一区二区三区不卡| 欧美视频官网| 亚洲成人精品一区二区| 欧美三级在线看| 成人午夜视频福利| 国产精品久久久久久亚洲毛片| 亚洲一区二区三区高清不卡| 久久国产夜色精品鲁鲁99| 精品久久久久香蕉网| 亚洲福利av| 久草这里只有精品视频| 久久久综合视频| 国产视频亚洲| 美女尤物国产一区| 26uuu精品一区二区| 亚洲激情av| 蜜桃视频第一区免费观看| 欧美成va人片在线观看| 黄色在线成人| 日韩激情一区二区| 精品三级在线观看| 亚洲激情一区二区三区| 久久精品久久99精品久久| 精品乱人伦小说| 亚洲日本精品国产第一区| 奇米亚洲午夜久久精品| 欧美成人精品3d动漫h| 影音先锋中文字幕一区| 午夜视频一区在线观看| 日韩一区二区电影网| 国内精品久久久久久久影视蜜臀| 五月婷婷色综合| 精品国产一区a| 国产欧美日韩在线播放| 黄色日韩三级电影| 国产精品久久国产精麻豆99网站| 一本色道久久综合精品竹菊| youjizz久久| 亚洲18女电影在线观看| 欧美一级二级三级乱码| 亚洲国产美女| 国产精品99久久久久久久vr| 1024成人网| 欧美日韩综合一区| 欧美一区二区三区在线播放| 日韩精品一卡二卡三卡四卡无卡| 亚洲精品一线二线三线无人区| 亚洲一区日韩在线| 成人h精品动漫一区二区三区| 亚洲色大成网站www久久九九| 欧美高清视频一二三区 | 亚洲视频免费在线| 678五月天丁香亚洲综合网| 在线播放一区| 国产99久久久精品| 亚洲第一福利视频在线| 久久久久久久久久看片| 91成人国产精品| 国内成+人亚洲| 国产精品自拍毛片| 亚洲一区二区三区四区在线观看| 日韩小视频在线观看专区| 国产精品久久亚洲7777| av不卡在线播放| 青青草国产精品97视觉盛宴| 国产精品色婷婷| 欧美日韩一本到| 日韩亚洲国产精品| 99精品视频在线播放观看| 欧美a级理论片| 亚洲欧美日韩在线不卡| 久久久久久久久一| 欧美日本一区二区| 欧美亚洲一区| 欧美日韩a区| 国产ts人妖一区二区| 午夜视黄欧洲亚洲| 国产精品久久久爽爽爽麻豆色哟哟| 3d动漫精品啪啪一区二区竹菊| 国产一区二区你懂的| 91麻豆免费观看| 久久电影网站中文字幕| 亚洲精品菠萝久久久久久久| 久久日一线二线三线suv| 欧美日韩国产片| 老**午夜毛片一区二区三区| 亚洲午夜精品久久| 成人精品免费网站| 久久91精品国产91久久小草| 亚洲成a人片在线不卡一二三区| 国产精品久久久久久久久免费樱桃| 精品美女一区二区三区| 欧美日本韩国一区| 色综合久久久久综合体| 亚洲精品日本| 国产精品a级| a在线播放不卡| 另类人妖一区二区av| 亚洲国产精品精华液网站| 国产精品久久久久久久裸模| 亚洲精品一区在线观看| 日韩午夜av电影| 欧美理论电影在线| 一本一道综合狠狠老| 亚洲一区免费| 国产精品久久久免费| 日韩视频不卡| 在线观看福利一区| 欧美黄色一级视频| 成人av集中营| 高清不卡一区二区在线| 国产电影一区在线| 毛片一区二区三区| 久久精品国产77777蜜臀| 蜜臀av性久久久久蜜臀av麻豆| 五月天激情综合| 亚洲成人7777| 午夜伦欧美伦电影理论片| 亚洲综合成人在线视频| 亚洲丝袜精品丝袜在线| 国产欧美一区二区精品婷婷| 欧美成人精品二区三区99精品| 日韩你懂的电影在线观看| 91精品国产综合久久久久久久久久| 欧美无人高清视频在线观看| 在线观看日韩高清av| 欧美性高清videossexo| 乱人伦精品视频在线观看| 国产乱码精品| 性久久久久久| 久久久久久网| 在线观看成人小视频| 欧美日韩视频在线一区二区| 欧美日韩国产在线观看| 欧美喷潮久久久xxxxx| 欧美日韩国产成人在线免费| 欧美狂野另类xxxxoooo| 88在线观看91蜜桃国自产| 欧美一级专区免费大片| 精品免费视频.| 国产欧美综合在线| 成人欧美一区二区三区黑人麻豆| 国产精品久久久久7777按摩| 亚洲伦理在线免费看| 亚洲国产一区二区三区青草影视 | 日本成人中文字幕在线视频 | 99精品免费视频|