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

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

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

瀏覽:13日期: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)在認證服務的網站上進行登錄,網站跳轉回第三方應用(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; // 表示已經認證 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分鐘內有效,等價于發(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手機驗證碼內容請搜索好吧啦網以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Spring
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
国产精品久久久亚洲一区| 麻豆精品视频在线观看| 精品成av人一区二区三区| 国产午夜精品一区在线观看| 视频一区二区三区中文字幕| 好吊一区二区三区| 久久国产精品毛片| 中文无码久久精品| 亚洲精品第一| 国产亚洲电影| 精品视频一区二区三区四区五区 | 热久久久久久久| 午夜精品婷婷| 鲁大师影院一区二区三区| 日av在线不卡| 欧美在线精品一区| 欧美1区二区| 一区二区三区四区日本视频| 新版的欧美在线视频| 成人久久一区| 久久亚洲图片| 日本午夜精品久久久| 国产精品videossex| 电影91久久久| 久久久水蜜桃av免费网站| 999久久久免费精品国产| 欧美精品自拍| 水蜜桃久久夜色精品一区的特点| 亚洲男人在线| 国产精品xxx在线观看| 久久精品国产亚洲aⅴ| 欧美日韩免费观看视频| 国产亚洲福利| 日本h片久久| 国精品产品一区| 亚洲香蕉网站| 亚洲美女久久| 精品国产欧美日韩| 国产字幕视频一区二区| 亚洲一区欧美| 毛片不卡一区二区| 欧美色图一区| 麻豆亚洲精品| 国产精品伊人| 秋霞影视一区二区三区| 亚洲天堂免费| 欧美激情亚洲| 国产综合精品一区| 国产日韩欧美三区| 91精品在线观看国产| 中文字幕一区二区三区在线视频| 国产精品九九| 午夜免费一区| 国产精品sm| 黑丝一区二区| 国产欧美日本| 欧美91福利在线观看| 视频一区免费在线观看| 久久精品二区亚洲w码| 91久久午夜| 国产乱码精品一区二区亚洲| 日韩在线不卡| 日韩视频一二区| 丝袜美腿诱惑一区二区三区| 综合一区av| 欧美二三四区| 88久久精品| 久久精品亚洲人成影院 | 国产精品一国产精品| 亚洲最新无码中文字幕久久| 日本一区二区中文字幕| 欧美aa一级| 国产精品一线天粉嫩av| 亚洲经典在线| 超碰99在线| 欧美久久久网站| 悠悠资源网久久精品| 久久中文精品| 最新国产精品久久久| 视频福利一区| 美女视频免费精品| 亚洲三级网站| 成人欧美一区二区三区的电影| 日韩和欧美一区二区| 91精品99| а√天堂8资源在线| 欧美午夜网站| 国产精品美女久久久| 日韩成人亚洲| 卡一精品卡二卡三网站乱码| 亚洲精品系列| 麻豆91精品| 性欧美69xoxoxoxo| 日韩和的一区二在线| 免费在线成人| 欧美性www| 日韩一区二区三区四区五区| 99tv成人| 欧美日韩尤物久久| 精品日韩在线| 国产精品videossex久久发布| 亚洲精品成a人ⅴ香蕉片| 丝袜国产日韩另类美女| 久久国产精品成人免费观看的软件| 精品视频一区二区三区在线观看| 国产欧美日韩在线观看视频| 婷婷五月色综合香五月| 国产女优一区| 久久青草久久| 亚洲www啪成人一区二区| 91综合网人人| 日韩综合一区| 精品国产aⅴ| 欧美韩一区二区| 欧美精品国产白浆久久久久| 一区二区三区四区日韩| 噜噜噜躁狠狠躁狠狠精品视频 | 国产精品原创| 狂野欧美性猛交xxxx| 美腿丝袜在线亚洲一区| 国产精品毛片视频| 国产亚洲欧美日韩精品一区二区三区| 午夜精品影视国产一区在线麻豆| 免费在线成人网| 三级欧美在线一区| 男女男精品视频网| 99在线|亚洲一区二区| 日韩亚洲在线| 午夜在线精品偷拍| 国产videos久久| 成人国产综合| 黄毛片在线观看| 91中文字幕精品永久在线| 中文在线а√天堂| 性欧美videohd高精| av资源中文在线天堂| 日韩中文在线电影| 日韩精品一区二区三区免费观影| 久久精品亚洲人成影院 | 亚洲精品国产偷自在线观看| 亚洲婷婷免费| 在线亚洲观看| 中文字幕日韩高清在线| 热久久久久久| 久久亚洲人体| 亚洲综合电影| 欧美精品九九| 亚欧洲精品视频在线观看| 日本成人在线一区| 精品国产欧美日韩| 婷婷国产精品| 亚洲精品韩国| 国产极品一区| 日韩欧美一区二区三区免费看| 日韩欧美在线中字| 国产视频一区三区| 日韩av网站在线观看| 欧美韩一区二区| 免费在线小视频| 国产精品美女久久久| 日韩高清在线不卡| 精品亚洲免a| 美女网站一区| 亚洲精选91| 国产一区二区三区成人欧美日韩在线观看| 综合日韩av| 日韩在线观看一区二区| 国产精品对白久久久久粗| 国产亚洲一区二区手机在线观看| 亚洲精品午夜av福利久久蜜桃| 中文字幕日韩亚洲| 麻豆视频一区二区| 九九久久电影| 久久国产三级| 欧美日韩免费观看视频| 亚洲三级精品| 高清日韩中文字幕| 欧美中文字幕| 欧美激情日韩| 免费久久精品| 国产日韩欧美一区二区三区在线观看| 伊人久久av| 日韩激情一区二区| 亚洲91精品| 日本欧美韩国一区三区| 亚洲91精品| 奇米狠狠一区二区三区| 亚洲黄色网址| 亚洲精品亚洲人成在线观看| 日产午夜精品一线二线三线| 蜜桃久久久久久| 在线天堂资源www在线污| 亚洲最大av| 吉吉日韩欧美| 日本在线视频一区二区| 日韩免费小视频| 国产免费久久| 99视频在线精品国自产拍免费观看| 欧美激情三区| 香蕉久久国产|