本篇文章讓我?guī)阏J(rèn)識(shí)一下什么是oauth協(xié)議
什么是 oauth協(xié)議 ?
百度百科上解釋:允許用戶提供一個(gè)令牌,而不是用戶名和密碼來(lái)訪問(wèn)他們存放在特定服務(wù)提供者的數(shù)據(jù)。每一個(gè)令牌授權(quán)一個(gè)特定的網(wǎng)站(例如,視頻編輯網(wǎng)站)在特定的時(shí)段(例如,接下來(lái)的2小時(shí)內(nèi))內(nèi)訪問(wèn)特定的資源(例如僅僅是某一相冊(cè)中的視頻)。這樣,OAuth允許用戶授權(quán)第三方網(wǎng)站訪問(wèn)他們存儲(chǔ)在另外的服務(wù)提供者上的信息,而不需要分享他們的訪問(wèn)許可或他們數(shù)據(jù)的所有內(nèi)容。
簡(jiǎn)單的來(lái)講就是一個(gè)令牌,這個(gè)令牌可以有一定的權(quán)限,在不知道用戶密碼的情況下也可以進(jìn)行部分的功功能操作。用一個(gè)例子幫助理解:一個(gè)甲方公司,公司里面使用一卡通,這張卡可以門(mén)禁,食堂,等等,公司招了外包進(jìn)來(lái),外包只能開(kāi)通門(mén)禁就行,其他的權(quán)限是沒(méi)有的。這時(shí)候公司就不會(huì)給一張正式員工的卡,而是外包的卡,正編員工卡就是用戶的賬號(hào)密碼,這個(gè)外包卡就相當(dāng)于是一個(gè)令牌,并且可能公司會(huì)給你的外包卡設(shè)置一個(gè)有效期,等有效期快到的時(shí)候有需要申請(qǐng)延期。這個(gè)就相當(dāng)于是oauth協(xié)議模式。用戶不會(huì)給你用戶名和密碼,那樣相當(dāng)于放開(kāi)了所有的權(quán)限,而會(huì)給你提供一個(gè)令牌,在有效期類(lèi)可以訪問(wèn)特定的功能服務(wù) 使用令牌的優(yōu)點(diǎn):
- 令牌是短期的,到期會(huì)自動(dòng)失效,用戶自己無(wú)法修改。密碼一般長(zhǎng)期有效,用戶不修改,就不會(huì)發(fā)生變化。
- 令牌可以被數(shù)據(jù)所有者撤銷(xiāo),會(huì)立即失效。
- 令牌有權(quán)限范圍(scope),對(duì)于網(wǎng)絡(luò)服務(wù)來(lái)說(shuō),只讀令牌就比讀寫(xiě)令牌更安全。密碼一般是完整權(quán)限
oauth協(xié)議有四種模式;
1、授權(quán)碼模式 2、隱藏式 3、密碼式 4、客戶端憑證 其中授權(quán)碼方式是最常用的流程,安全性也最高,今天我們就來(lái)著重講一下授權(quán)碼模式。 授權(quán)碼模式: 指的是第三方應(yīng)用先申請(qǐng)一個(gè)授權(quán)碼,然后再用該碼獲取令牌,授權(quán)碼通過(guò)前端傳送,令牌則是儲(chǔ)存在后端,而且所有與資源服務(wù)器的通信都在后端完成。這樣的前后端分離,可以避免令牌泄漏。
授權(quán)碼模式主要分為幾個(gè)步驟
以甲方公司員工卡系統(tǒng)(SelfResearch.com)和外包公司員工卡系統(tǒng)(outsource.com)為例,下面是授權(quán)碼模式的流程
請(qǐng)求授權(quán)碼
首先,甲方公司給外包員工方提供一個(gè)授權(quán)鏈接,外包員工點(diǎn)擊連接,申請(qǐng)授權(quán)碼,連接參數(shù)如下所示
https://SelfResearch.com/oauth/authorize?response_type=code&client_id=CLIENT_ID&redirect_uri=CALLBACK_URL&scope=read
response_type參數(shù)表示要求返回授權(quán)碼(code), client_id參數(shù)讓 甲方 知道是誰(shuí)在請(qǐng)求, redirect_uri參數(shù)是 甲方系統(tǒng) 接受或拒絕請(qǐng)求后的跳轉(zhuǎn)網(wǎng)址, scope參數(shù)表示要求的授權(quán)范圍(這里是只讀)。
返回授權(quán)碼
用戶跳轉(zhuǎn)到這個(gè)之后,甲方公司會(huì)先要求用戶登錄,然后請(qǐng)求授權(quán),如果同意就跳轉(zhuǎn)到redirect_uri這個(gè)參數(shù)的地址,并返回授權(quán)碼
https://SelfResearch.com/callback?code=AUTHORIZATION_CODE
code就是表示授權(quán)碼
請(qǐng)求令牌
外包員工拿著授權(quán)碼去請(qǐng)求令牌
https://SelfResearch.com/oauth/token?client_id=CLIENT_ID&client_secret=CLIENT_SECRET&grant_type=authorization_code&code=AUTHORIZATION_CODE&redirect_uri=CALLBACK_URL
- client_id參數(shù)和client_secret參數(shù)用來(lái)讓 甲方公司 確認(rèn) 外包員工 的身份(client_secret參數(shù)是保密的,因此只能在后端發(fā)請(qǐng)求)。
- grant_type參數(shù)的值是AUTHORIZATION_CODE,表示采用的授權(quán)方式是授權(quán)碼。
- code參數(shù)是上一步拿到的授權(quán)碼。
- redirect_uri參數(shù)是令牌頒發(fā)后的回調(diào)網(wǎng)址。
返回令牌 外包員工訪問(wèn)redirect_uri會(huì)得到一串json數(shù)據(jù):
{
"access_token": "67c4051b-36c8-11ec-af33-00163e0808bf",
"token_type": "bearer",
"refresh_token": "71975ccc-36c8-11ec-af33-cfd2826915e5",
"expires_in": 3249,
"scope": "read"
}
access_token就是令牌了,用戶需要訪問(wèn)其他接口就需要帶上這個(gè)token去訪問(wèn)了 refresh_token這個(gè)是刷新令牌,如果需要使用新的token,就需要通過(guò)這個(gè)刷新令牌來(lái)獲取最新的access_token。
實(shí)現(xiàn) oauth2,可以分為三個(gè)步驟
- 認(rèn)證服務(wù)
- 資源服務(wù)
- 第三方服務(wù)
現(xiàn)在第三方用戶(test9527)想去訪問(wèn)資源中的部分功能(接口),但是改功能只有User角色才能訪問(wèn),需要先通過(guò)認(rèn)證服務(wù)器獲取user的授權(quán)碼,然后拿著這個(gè)code和自己賬號(hào)信息去獲取token,并校驗(yàn)通過(guò)之后才能訪問(wèn)到資源服務(wù)器,也就是第三方用戶通過(guò) test9527 可以訪問(wèn)本來(lái)需要user權(quán)限才能訪問(wèn)的資源功能(接口)
實(shí)現(xiàn)步驟:
創(chuàng)建三個(gè)服務(wù)oauth-server(認(rèn)證服務(wù))、resource-server(資源服務(wù))、third-server(第三方服務(wù)
maven依賴
< !-- oauth2 -- >
< dependency >
< groupId >org.springframework.security.oauth< /groupId >
< artifactId >spring-security-oauth2< /artifactId >
< version >2.1.3.RELEASE< /version >
< /dependency >
< !-- spring security-- >
< dependency >
< groupId >org.springframework.boot< /groupId >
< artifactId >spring-boot-starter-security< /artifactId >
< /dependency >
oauth-server 服務(wù)配置文件
/**
* 模擬第三方授權(quán)配置
*/
@EnableAuthorizationServer
@Configuration
public class AuthConfig extends AuthorizationServerConfigurerAdapter {
@Resource
ClientDetailsService clientDetailsService;
/**
* 資源服務(wù)器校驗(yàn)Token
*/
@Override
public void configure(AuthorizationServerSecurityConfigurer security) {
security.checkTokenAccess("permitAll()").allowFormAuthenticationForClients();
}
/**
* 第三方客戶端請(qǐng)求配置,和資源服務(wù)訪問(wèn)的配置,不設(shè)置默認(rèn)都可以訪問(wèn),提供默認(rèn)回調(diào)地址
*/
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
//第三方用戶
.withClient("test9527")
.secret(new BCryptPasswordEncoder().encode("test9527"))
.resourceIds("resource")
//認(rèn)證模式
.authorizedGrantTypes("authorization_code","refresh_token")
.scopes("all")
//回調(diào)地址
.redirectUris("http://localhost:8082/notify.html");
}
/**
* 配置訪問(wèn)端點(diǎn)
*/
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) {
endpoints.authorizationCodeServices(authorizationCodeServices()).tokenServices(tokenServices());
}
/**
* 內(nèi)存管理
*/
@Bean
AuthorizationCodeServices authorizationCodeServices() {
return new InMemoryAuthorizationCodeServices();
}
/**
* Token管理規(guī)則
*/
@Bean
AuthorizationServerTokenServices tokenServices() {
DefaultTokenServices services = new DefaultTokenServices();
services.setClientDetailsService(clientDetailsService);
services.setSupportRefreshToken(true);
services.setTokenStore(tokenStore());
services.setAccessTokenValiditySeconds(3600);
services.setRefreshTokenValiditySeconds(3600*7);
return services;
}
@Bean
TokenStore tokenStore() {
return new InMemoryTokenStore();
}
配置spring security
/**
* 模擬本地用戶配置
*/
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
/**
* 密碼加密方式
*/
@Bean
public PasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}
/**
* 內(nèi)存中虛擬用戶和角色
*/
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("user")
.password(new BCryptPasswordEncoder().encode("123456"))
.roles("user");
}
/**
* 表單登錄
*/
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable().formLogin();
}
}
resource-server配置
/**
* 資源服務(wù)管理配置
*/
@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
/**
* Token令牌校驗(yàn)
*/
@Bean
RemoteTokenServices tokenServices() {
RemoteTokenServices services = new RemoteTokenServices();
services.setCheckTokenEndpointUrl("http://localhost:8080/oauth/check_token");
services.setClientId("test9527");
services.setClientSecret("test9527");
return services;
}
/**
* 服務(wù)資源ID配置
*/
@Override
public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
resources.resourceId("resource").tokenServices(tokenServices());
}
/**
* 模擬用戶權(quán)限規(guī)則
*/
@Override
public void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
//訪問(wèn)user下的路徑需要user角色
.antMatchers("/user/**").hasRole("user")
.anyRequest().authenticated();
}
resource下的功能(接口)
第三方服務(wù)首先認(rèn)證,獲取授權(quán)碼code http://localhost:8080/oauth/authorize?client_id=test9527&response_type=code&scope=all&redirect_uri=http://localhost:8082/notify.html
此時(shí)會(huì)到認(rèn)證服務(wù)器中的user認(rèn)證
此時(shí)需要user用戶同意授權(quán),當(dāng)認(rèn)證服務(wù)中的user用戶同意之后,到如下頁(yè)面,此時(shí)看接口可以知道已經(jīng)拿到code授權(quán)碼,并跳轉(zhuǎn)到我們需要跳轉(zhuǎn)的地址
redirect_uri=http://localhost:8082/notify.html
拿到授權(quán)碼之后訪問(wèn)回調(diào)地址 http://localhost:8082/notify.html?code=Rs067L
然后通過(guò)code,client_id,client_secret 等參數(shù)訪問(wèn)獲取令牌地址 https://SelfResearch.com/oauth/token?client_id=CLIENT_ID&client_secret=CLIENT_SECRET&grant_type=authorization_code&code=SMD5nj&redirect_uri=redirect_uri=http://localhost:8082/notify.html
成功之后會(huì)返回token,刷新token,以及token有效時(shí)間,如下
拿到token之后,我們?cè)偃ピL問(wèn)資源服務(wù)器,此時(shí)就能順利訪問(wèn)到功能(接口)了
最終效果,最終第三方用戶test9527訪問(wèn)當(dāng)了資源服務(wù)器功
-
數(shù)據(jù)
+關(guān)注
關(guān)注
8文章
7168瀏覽量
89692 -
參數(shù)
+關(guān)注
關(guān)注
11文章
1860瀏覽量
32446
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
linxu網(wǎng)絡(luò)協(xié)議分析:IP協(xié)議、TCP協(xié)議、UDP協(xié)議
![linxu網(wǎng)絡(luò)<b class='flag-5'>協(xié)議</b>分析:IP<b class='flag-5'>協(xié)議</b>、TCP<b class='flag-5'>協(xié)議</b>、UDP<b class='flag-5'>協(xié)議</b>](https://file.elecfans.com/web1/M00/CC/FE/pIYBAF-ZLjSACk-gAAC4nlN26AQ246.png)
![](https://file1.elecfans.com/web2/M00/83/9D/wKgZomRl4J2AU25_AAEaAmDMGwg534.png)
#硬聲創(chuàng)作季 06-JAVAEE實(shí)戰(zhàn)項(xiàng)目課程—OAUTH2認(rèn)證-OAuth2協(xié)議流程
跟著小狂玩天貓精靈智能設(shè)備對(duì)接--服務(wù)器篇(二)
K8s 從懵圈到熟練 – 鏡像拉取這件小事
協(xié)議是什么 協(xié)議棧又是什么
如何使用ESP8266作為簡(jiǎn)單頁(yè)面的網(wǎng)絡(luò)服務(wù)器來(lái)生成OAuth代碼以批準(zhǔn)設(shè)備并生成刷新令牌和訪問(wèn)令牌?
OSPF協(xié)議,OSPF協(xié)議是什么意思
SIP協(xié)議,什么是SIP協(xié)議
V-CHARGE的V2I通訊系統(tǒng)由德國(guó)TU Braunschweig大學(xué)完成設(shè)計(jì)
![V-CHARGE的V2I通訊系統(tǒng)由德國(guó)TU Braunschweig大學(xué)完成設(shè)計(jì)](https://file.elecfans.com/web1/M00/58/F3/pIYBAFtgHS6AUTA5AAAYoATfBks848.png)
OAuth2-Server OAuth 2.0授權(quán)服務(wù)器的實(shí)現(xiàn)
![<b class='flag-5'>OAuth</b>2-Server <b class='flag-5'>OAuth</b> 2.0授權(quán)服務(wù)器的實(shí)現(xiàn)](https://file.elecfans.com/web1/M00/D9/4E/pIYBAF_1ac2Ac0EEAABDkS1IP1s689.png)
六款開(kāi)源項(xiàng)目推薦
SSO單點(diǎn)登錄和OAuth2.0的區(qū)別和理解
什么是單點(diǎn)登錄?解讀單點(diǎn)登錄兩個(gè)協(xié)議:SAML、OAuth2
Spring Boot 3.1 中如何整合Spring Security和Keycloak
![Spring Boot 3.1 中如何整合Spring Security和Keycloak](https://file1.elecfans.com/web2/M00/89/5A/wKgaomSBe3-AAraCAAAulKC7sBM847.png)
評(píng)論