SpringSecurity oAuth2.0的四種模式(小結(jié))
1.1. 授權(quán)碼授權(quán)模式(Authorization code Grant)
1.1.1. 流程圖
1.1.2. 授權(quán)服務(wù)器配置配置
授權(quán)服務(wù)器中 client,secret,redirectUri,授權(quán)模式,權(quán)限配置
//授權(quán)服務(wù)器配置 @Configuration @EnableAuthorizationServer public class OAuth2AuthorizationServer extends AuthorizationServerConfigurerAdapter { @Override public void configure(ClientDetailsServiceConfigurer clients) throws Exception { clients.inMemory() .withClient("clientapp") .secret("112233") .redirectUris("http://localhost:9001/callback") // 授權(quán)碼模式 .authorizedGrantTypes("authorization_code") .scopes("read_userinfo", "read_contacts"); } }
1.1.3. 資源服務(wù)器配置
配置需要資源授權(quán)的接口地址
//資源服務(wù)配置 @Configuration @EnableResourceServer public class OAuth2ResourceServer extends ResourceServerConfigurerAdapter { @Override public void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .anyRequest() .authenticated() .and() .requestMatchers() .antMatchers("/api/**"); } }
1.1.4. 操作步驟
瀏覽器請(qǐng)求下列地址,獲取授權(quán)code,請(qǐng)求參數(shù)client_id,redirect_uri回調(diào)地址,response_type響應(yīng)類型,scope權(quán)限
http://localhost:8080/oauth/authorize?client_id=clientapp&redirect_uri=http://localhost:9001/callback&response_type=code&scope=read_userinfo
輸入用戶名密碼,該密碼為Spring Security的登路密碼,application.properties里配置
# Spring Security Setting security.user.name=bobo security.user.password=xyz
登陸后顯示
選擇Approve,點(diǎn)擊Authorize,會(huì)調(diào)用回調(diào)地址并返回code參數(shù)
在獲得授權(quán)碼后,接下去獲取訪問(wèn)令牌,訪問(wèn)
http://localhost:8080/oauth/token?code=ghN0hF&grant_type=authorization_code&redirect_uri=http://localhost:9001/callback&scope=read_userinfo
注意:需要在headers里添加認(rèn)證
認(rèn)證參數(shù)就是授權(quán)服務(wù)器配置的client和secret
獲取token后訪問(wèn)
http://localhost:8080/api/userinfo?access_token=f4345f3a-34a3-4887-bc02-e95150c54bf4
如果token錯(cuò)誤,則
1.1.5. 使用場(chǎng)景
授權(quán)碼模式是最常見(jiàn)的一種授權(quán)模式,在oauth2.0內(nèi)是最安全和最完善的。適用于所有有Server端的應(yīng)用,如Web站點(diǎn)、有Server端的手機(jī)客戶端??梢缘玫捷^長(zhǎng)期限授權(quán)。
1.2. 隱式授權(quán)模式(Implicit Grant)
1.2.1. 流程圖
1.2.2. 改動(dòng) authorizedGrantTypes
@Configuration @EnableAuthorizationServer public class OAuth2AuthoriationServer extends AuthorizationServerConfigurerAdapter{ @Override public void configure(ClientDetailsServiceConfigurer clients) throws Exception { clients.inMemory() .withClient("clientapp") .secret("112233") .accessTokenValiditySeconds(60) .redirectUris("http://localhost:9001/callback") .authorizedGrantTypes("implicit") .scopes("admin", "visitor"); } }
1.2.3. 操作步驟
申請(qǐng)授權(quán)token,參數(shù)和申請(qǐng)授權(quán)碼類似,client_id,redirect_uri回調(diào)地址,response_type有變動(dòng),改為直接獲取token,scope權(quán)限,state用于認(rèn)證標(biāo)記,傳過(guò)去什么回調(diào)時(shí)傳回來(lái)什么
http://localhost:8080/oauth/authorize?client_id=clientapp&redirect_uri=http://localhost:9001/callback&response_type=token&scope=admin&state=abc
操作同上,輸入密碼跳轉(zhuǎn)認(rèn)證確認(rèn),選Approve后點(diǎn)Authorize,跳轉(zhuǎn)
3. 可以看到直接返回了access_token,state也是原樣返回
4. 之后按授權(quán)碼模式第六步操作,把a(bǔ)ccess_token參數(shù)帶上,進(jìn)行接口調(diào)用就可以了
1.2.4. 使用場(chǎng)景
- 適用于所有無(wú)Server端配合的應(yīng)用
- 如手機(jī)/桌面客戶端程序、瀏覽器插件。
- 基于JavaScript等腳本客戶端腳本語(yǔ)言實(shí)現(xiàn)的應(yīng)用。
注意:因?yàn)锳ccess token是附著在 redirect_uri 上面被返回的,所以這個(gè) Access token就可能會(huì)暴露給資源所有者或者設(shè)置內(nèi)的其它方(對(duì)資源所有者來(lái)說(shuō),可以看到redirect_uri,對(duì)其它方來(lái)說(shuō),可以通過(guò)監(jiān)測(cè)瀏覽器的地址變化來(lái)得到 Access token)。
1.3. 密碼模式(Resource Owner Password Credentials Grant)
1.3.1. 流程圖
1.3.2. 改動(dòng)
授權(quán)服務(wù)器配置,需要添加用戶認(rèn)證管理端點(diǎn)authenticationManager,修改模式authorizedGrantTypes為password
// 授權(quán)服務(wù)器配置 @Configuration @EnableAuthorizationServer public class OAuth2AuthoriationServer extends AuthorizationServerConfigurerAdapter{ @Autowired private AuthenticationManager authenticationManager; @Override public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { endpoints.authenticationManager(authenticationManager); } @Override public void configure(ClientDetailsServiceConfigurer clients) throws Exception { clients.inMemory() .withClient("clientapp") .secret("112233") .accessTokenValiditySeconds(60) .redirectUris("http://localhost:9001/callback") .authorizedGrantTypes("password") .scopes("admin", "visitor"); } }
1.3.3. 操作步驟
調(diào)用以下鏈接,向客戶端和服務(wù)器提供用戶名密碼
http://localhost:8080/oauth/token?password=123456&grant_type=password&username=lll&scope=admin
注意:和授權(quán)碼模式一樣,需要在headers里添加認(rèn)證
結(jié)果:
獲取token后,步驟同1.1和1.2模式
1.3.4. 使用場(chǎng)景
- 這種模式適用于用戶對(duì)應(yīng)用程序高度信任的情況。比如是用戶操作系統(tǒng)的一部分。
- 認(rèn)證服務(wù)器只有在其他授權(quán)模式無(wú)法執(zhí)行的情況下,才能考慮使用這種模式。
1.4. 客戶端憑證模式(Client Credentials Grant)
1.4.1. 流程圖
1.4.2. 改動(dòng)
只需修改授權(quán)服務(wù)器,authorizedGrantTypes類型client_credentials
1.4.3. 操作步驟
http://localhost:8080/oauth/token?grant_type=client_credentials&scope=admin
可以看到客戶端憑證模式也需要在header里添加認(rèn)證賬戶密碼
獲得token后操作同上
1.4.4. 使用場(chǎng)景
- 客戶端模式應(yīng)用于應(yīng)用程序想要以自己的名義與授權(quán)服務(wù)器以及資源服務(wù)器進(jìn)行互動(dòng)。
- 例如使用了第三方的靜態(tài)文件服務(wù)
1.5. 刷新TOKEN
1.5.1. 流程圖
1.5.2. 改動(dòng)
1.5.3. 操作步驟
以授權(quán)碼模式為例,步驟同授權(quán)碼模式,取得授權(quán)碼后,去取token時(shí),返回
在token過(guò)期后,調(diào)用
http://localhost:8080/oauth/token?grant_type=refresh_token&refresh_token=ad3941d1-c6dd-4a2e-a9c8-eac6a9a59dd2
返回
就可以拿新的access_token繼續(xù)調(diào)用了建議將access_token和refresh_token的過(guò)期時(shí)間保存下來(lái),每次調(diào)用平臺(tái)方的業(yè)務(wù)api前先對(duì)access_token和refresh_token進(jìn)行一下時(shí)間判斷,如果過(guò)期則執(zhí)行刷新access_token或重新授權(quán)操作。refersh_token如果過(guò)期就只能讓用戶重新授權(quán)。
參考 https://www.cnblogs.com/maoxiaolv/p/5838680.html
代碼學(xué)習(xí)地址 https://github.com/spring2go/oauth2lab
到此這篇關(guān)于SpringSecurity oAuth2.0的四種模式(小結(jié))的文章就介紹到這了,更多相關(guān)SpringSecurity oAuth2.0模式內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
在同一個(gè)類中調(diào)用帶有@Transactional注解的方法示例
這篇文章主要為大家介紹了在同一個(gè)類中調(diào)用帶有@Transactional注解的方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04java版簡(jiǎn)單的猜數(shù)字游戲?qū)嵗a
猜數(shù)字游戲是一款經(jīng)典的游戲,該游戲說(shuō)簡(jiǎn)單也很簡(jiǎn)單,說(shuō)不簡(jiǎn)單確實(shí)也很難,那么下面這篇文章主要給大家介紹了java版簡(jiǎn)單的猜數(shù)字游戲的相關(guān)資料,文中給出了詳細(xì)的實(shí)現(xiàn)分析和示例代碼供大家參考學(xué)習(xí),需要的朋友們下面來(lái)一起看看吧。2017-05-05Java getRealPath("/")與getContextPath()區(qū)別詳細(xì)分析
這篇文章主要介紹了Java getRealPath("/")與getContextPath()區(qū)別詳細(xì)分析,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08詳解微信開(kāi)發(fā)之a(chǎn)ccess_token之坑
access_token分類一是普通access_token,二是網(wǎng)頁(yè)授權(quán)access_token。這篇文章主要介紹了詳解微信開(kāi)發(fā)之a(chǎn)ccess_token之坑,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10IntelliJ IDEA基于Scala實(shí)現(xiàn)Git檢查工具
這篇文章主要介紹了如何使用Scala實(shí)現(xiàn)自定義的Git檢查工具,大家可以基于本文的示例進(jìn)行擴(kuò)展與實(shí)現(xiàn),也可以進(jìn)行其他應(yīng)用方向的嘗試,感興趣的可以了解下2023-08-08