詳解使用Spring Security OAuth 實(shí)現(xiàn)OAuth 2.0 授權(quán)
OAuth 2.0 是一種工業(yè)級的授權(quán)協(xié)議。OAuth 2.0是從創(chuàng)建于2006年的OAuth 1.0繼承而來的。OAuth 2.0致力于幫助開發(fā)者簡化授權(quán)并為web應(yīng)用、桌面應(yīng)用、移動應(yīng)用、嵌入式應(yīng)用提供具體的授權(quán)流程。
OAuth 2.0 is the industry-standard protocol for authorization. OAuth 2.0 supersedes the work done on the original OAuth protocol created in 2006. OAuth 2.0 focuses on client developer simplicity while providing specific authorization flows for web applications, desktop applications, mobile phones, and living room devices.
OAuth 2.0的四個角色
為了方便理解,以常用的 使用微信登錄 為例
Resource Owner
資源擁有者,對應(yīng)微信的每個用戶微信上設(shè)置的個人信息是屬于每個用戶的,不屬于騰訊。
Resource Server
資源服務(wù)器,一般就是用戶數(shù)據(jù)的一些操作(增刪改查)的REST API,比如微信的獲取用戶基本信息的接口。
Client Application
第三方客戶端,對比微信中就是各種微信公眾號開發(fā)的應(yīng)用,第三方應(yīng)用經(jīng)過 認(rèn)證服務(wù)器 授權(quán)后即可訪問 資源服務(wù)器 的REST API來獲取用戶的頭像、性別、地區(qū)等基本信息。
Authorization Server
認(rèn)證服務(wù)器,驗(yàn)證第三方客戶端是否合法。如果合法就給客戶端頒布token,第三方通過token來調(diào)用資源服務(wù)器的API。
四種授權(quán)方式(Grant Type)
anthorization_code
授權(quán)碼類型,適用于Web Server Application。模式為:客戶端先調(diào)用 /oauth/authorize/ 進(jìn)到用戶授權(quán)界面,用戶授權(quán)后返回 code ,客戶端然后根據(jù)code和 appSecret 獲取 access token 。
implicit簡化類型,相對于授權(quán)碼類型少了授權(quán)碼獲取的步驟??蛻舳藨?yīng)用授權(quán)后認(rèn)證服務(wù)器會直接將access token放在客戶端的url。客戶端解析url獲取token。這種方式其實(shí)是不太安全的,可以通過 https安全通道 和 縮短access token的有效時間 來較少風(fēng)險(xiǎn)。
password
密碼類型,客戶端應(yīng)用通過用戶的username和password獲access token。適用于資源服務(wù)器、認(rèn)證服務(wù)器與客戶端具有完全的信任關(guān)系,因?yàn)橐獙⒂脩粢獙⒂脩舻挠脩裘艽a直接發(fā)送給客戶端應(yīng)用,客戶端應(yīng)用通過用戶發(fā)送過來的用戶名密碼獲取token,然后訪問資源服務(wù)器資源。比如支付寶就可以直接用淘寶用戶名和密碼登錄,因?yàn)樗鼈儗儆谕患夜?,彼?充分信任 。
client_credentials
客戶端類型,是不需要用戶參與的一種方式,用于不同服務(wù)之間的對接。比如自己開發(fā)的應(yīng)用程序要調(diào)用短信驗(yàn)證碼服務(wù)商的服務(wù),調(diào)用地圖服務(wù)商的服務(wù)、調(diào)用手機(jī)消息推送服務(wù)商的服務(wù)。當(dāng)需要調(diào)用服務(wù)是可以直接使用服務(wù)商給的 appID 和 appSecret 來獲取token,得到token之后就可以直接調(diào)用服務(wù)。
其他概念
- scope :訪問資源服務(wù)器的哪些作用域。
- refresh token :當(dāng)access token 過期后,可以通過refresh token重新獲取access token。
實(shí)現(xiàn)
有的時候資源服務(wù)器和認(rèn)證服務(wù)器是兩個不同的應(yīng)用,有的時候資源服務(wù)器和認(rèn)證服務(wù)器在通一個應(yīng)用中,不同之處在于資源服務(wù)器是否需要檢查token的有效性,前者需要檢查,后者不需要。這里實(shí)現(xiàn)后者。
Application的安全配置
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.formLogin()
.and().csrf().disable()
.authorizeRequests().anyRequest().authenticated();
}
@Override
public void configure(WebSecurity web) throws Exception {
super.configure(web);
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("lyt").password("lyt").authorities("ROLE_USER")
.and().withUser("admin").password("admin").authorities("ROLE_ADMIN");
}
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
}
認(rèn)證服務(wù)器配置
@EnableAuthorizationServer
@Configuration
public class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory().withClient("client")
.scopes("read","write")
.secret("secret")
.authorizedGrantTypes("authorization_code","password","implicit","client_credentials");}
@Override
public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
super.configure(security);
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.authenticationManager(authenticationManager);
}
@Autowired
@Qualifier("authenticationManagerBean")
private AuthenticationManager authenticationManager;
}
資源服務(wù)器配置
@EnableGlobalMethodSecurity(prePostEnabled = true)
@EnableResourceServer
@Configuration
public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http.antMatcher("/oauth2/api/**").authorizeRequests()
.antMatchers(HttpMethod.GET, "/read/**").access("#oauth2.hasScope('read')")
.antMatchers(HttpMethod.POST, "/write/**").access("#oauth2.hasScope('write')")
.antMatchers(HttpMethod.PUT, "/write/**").access("#oauth2.hasScope('write')")
.antMatchers(HttpMethod.DELETE, "/write/**").access("#oauth2.hasScope('write')");
}
}
資源服務(wù)器 filter-order 設(shè)置
需要在 application.yml 中將filter-order設(shè)置成3,具體原因參考鏈接
防止cookie沖突
為了避免認(rèn)證服務(wù)器的cookie和客戶端的cookie沖突,出現(xiàn)錯誤,最好修改 cookie name 或者設(shè)置 contextPath 。
測試
postman 中提供OAuth 2.0的認(rèn)證方式,可以獲取到token之后再把認(rèn)證加入http請求中,即可請求資源服務(wù)器的REST API
客戶端信息

授權(quán)

獲取的token

訪問資源服務(wù)器API

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Springsecurity Oauth2如何設(shè)置token的過期時間
- Spring Security OAuth2實(shí)現(xiàn)使用JWT的示例代碼
- 基于Spring Security的Oauth2授權(quán)實(shí)現(xiàn)方法
- Spring Security整合Oauth2實(shí)現(xiàn)流程詳解
- SpringSecurityOAuth2 如何自定義token信息
- SpringSecurity OAuth2單點(diǎn)登錄和登出的實(shí)現(xiàn)
- SpringBoot的Security和OAuth2的使用示例小結(jié)
- Spring Security OAuth過期的解決方法
- Spring Security OAuth2認(rèn)證授權(quán)示例詳解
- Spring Security OAuth2.0登出的實(shí)現(xiàn)
相關(guān)文章
Spring?Security權(quán)限管理小結(jié)
SpringSecurity是一個權(quán)限管理框架,核心是認(rèn)證和授權(quán),前面已經(jīng)系統(tǒng)的給大家介紹過了認(rèn)證的實(shí)現(xiàn)和源碼分析,本文重點(diǎn)來介紹下權(quán)限管理,需要的朋友可以參考下2022-08-08
MyBatis查詢數(shù)據(jù),賦值給List集合時,數(shù)據(jù)缺少的問題及解決
這篇文章主要介紹了MyBatis查詢數(shù)據(jù),賦值給List集合時,數(shù)據(jù)缺少的問題及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01
詳解Spring中Bean的生命周期和作用域及實(shí)現(xiàn)方式
這篇文章主要給大家介紹了Spring中Bean的生命周期和作用域及實(shí)現(xiàn)方式的相關(guān)資料,文中介紹的非常詳細(xì),對大家具有一定的參考價(jià)值,需要的朋友們下面來一起看看吧。2017-03-03

