SpringSecurity oAuth2.0的四種模式(小結(jié))
1.1. 授權(quán)碼授權(quán)模式(Authorization code Grant)
1.1.1. 流程圖

1.1.2. 授權(quán)服務器配置配置
授權(quán)服務器中 client,secret,redirectUri,授權(quán)模式,權(quán)限配置
//授權(quán)服務器配置
@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. 資源服務器配置
配置需要資源授權(quán)的接口地址
//資源服務配置
@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. 操作步驟
瀏覽器請求下列地址,獲取授權(quán)code,請求參數(shù)client_id,redirect_uri回調(diào)地址,response_type響應類型,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,點擊Authorize,會調(diào)用回調(diào)地址并返回code參數(shù)

在獲得授權(quán)碼后,接下去獲取訪問令牌,訪問
http://localhost:8080/oauth/token?code=ghN0hF&grant_type=authorization_code&redirect_uri=http://localhost:9001/callback&scope=read_userinfo

注意:需要在headers里添加認證

認證參數(shù)就是授權(quán)服務器配置的client和secret

獲取token后訪問
http://localhost:8080/api/userinfo?access_token=f4345f3a-34a3-4887-bc02-e95150c54bf4

如果token錯誤,則

1.1.5. 使用場景
授權(quán)碼模式是最常見的一種授權(quán)模式,在oauth2.0內(nèi)是最安全和最完善的。適用于所有有Server端的應用,如Web站點、有Server端的手機客戶端??梢缘玫捷^長期限授權(quán)。
1.2. 隱式授權(quán)模式(Implicit Grant)
1.2.1. 流程圖

1.2.2. 改動 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. 操作步驟
申請授權(quán)token,參數(shù)和申請授權(quán)碼類似,client_id,redirect_uri回調(diào)地址,response_type有變動,改為直接獲取token,scope權(quán)限,state用于認證標記,傳過去什么回調(diào)時傳回來什么
http://localhost:8080/oauth/authorize?client_id=clientapp&redirect_uri=http://localhost:9001/callback&response_type=token&scope=admin&state=abc

操作同上,輸入密碼跳轉(zhuǎn)認證確認,選Approve后點Authorize,跳轉(zhuǎn)

3. 可以看到直接返回了access_token,state也是原樣返回
4. 之后按授權(quán)碼模式第六步操作,把access_token參數(shù)帶上,進行接口調(diào)用就可以了

1.2.4. 使用場景
- 適用于所有無Server端配合的應用
- 如手機/桌面客戶端程序、瀏覽器插件。
- 基于JavaScript等腳本客戶端腳本語言實現(xiàn)的應用。
注意:因為Access token是附著在 redirect_uri 上面被返回的,所以這個 Access token就可能會暴露給資源所有者或者設置內(nèi)的其它方(對資源所有者來說,可以看到redirect_uri,對其它方來說,可以通過監(jiān)測瀏覽器的地址變化來得到 Access token)。
1.3. 密碼模式(Resource Owner Password Credentials Grant)
1.3.1. 流程圖

1.3.2. 改動
授權(quán)服務器配置,需要添加用戶認證管理端點authenticationManager,修改模式authorizedGrantTypes為password
// 授權(quán)服務器配置
@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)用以下鏈接,向客戶端和服務器提供用戶名密碼
http://localhost:8080/oauth/token?password=123456&grant_type=password&username=lll&scope=admin
注意:和授權(quán)碼模式一樣,需要在headers里添加認證
結(jié)果:

獲取token后,步驟同1.1和1.2模式
1.3.4. 使用場景
- 這種模式適用于用戶對應用程序高度信任的情況。比如是用戶操作系統(tǒng)的一部分。
- 認證服務器只有在其他授權(quán)模式無法執(zhí)行的情況下,才能考慮使用這種模式。
1.4. 客戶端憑證模式(Client Credentials Grant)
1.4.1. 流程圖

1.4.2. 改動
只需修改授權(quán)服務器,authorizedGrantTypes類型client_credentials
1.4.3. 操作步驟
http://localhost:8080/oauth/token?grant_type=client_credentials&scope=admin

可以看到客戶端憑證模式也需要在header里添加認證賬戶密碼
獲得token后操作同上
1.4.4. 使用場景
- 客戶端模式應用于應用程序想要以自己的名義與授權(quán)服務器以及資源服務器進行互動。
- 例如使用了第三方的靜態(tài)文件服務
1.5. 刷新TOKEN
1.5.1. 流程圖

1.5.2. 改動
1.5.3. 操作步驟
以授權(quán)碼模式為例,步驟同授權(quán)碼模式,取得授權(quán)碼后,去取token時,返回

在token過期后,調(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的過期時間保存下來,每次調(diào)用平臺方的業(yè)務api前先對access_token和refresh_token進行一下時間判斷,如果過期則執(zhí)行刷新access_token或重新授權(quán)操作。refersh_token如果過期就只能讓用戶重新授權(quán)。
參考 https://www.cnblogs.com/maoxiaolv/p/5838680.html
代碼學習地址 https://github.com/spring2go/oauth2lab
到此這篇關于SpringSecurity oAuth2.0的四種模式(小結(jié))的文章就介紹到這了,更多相關SpringSecurity oAuth2.0模式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
在同一個類中調(diào)用帶有@Transactional注解的方法示例
這篇文章主要為大家介紹了在同一個類中調(diào)用帶有@Transactional注解的方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-04-04
Java getRealPath("/")與getContextPath()區(qū)別詳細分析
這篇文章主要介紹了Java getRealPath("/")與getContextPath()區(qū)別詳細分析,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-08-08
詳解微信開發(fā)之a(chǎn)ccess_token之坑
access_token分類一是普通access_token,二是網(wǎng)頁授權(quán)access_token。這篇文章主要介紹了詳解微信開發(fā)之a(chǎn)ccess_token之坑,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-10-10
IntelliJ IDEA基于Scala實現(xiàn)Git檢查工具
這篇文章主要介紹了如何使用Scala實現(xiàn)自定義的Git檢查工具,大家可以基于本文的示例進行擴展與實現(xiàn),也可以進行其他應用方向的嘗試,感興趣的可以了解下2023-08-08

