相關(guān)文章:
- OAuth2的定義和運(yùn)行流程
- Spring Security OAuth實(shí)現(xiàn)Gitee快捷登錄
- Spring Security OAuth實(shí)現(xiàn)GitHub快捷登錄
- Spring Security的過濾器鏈機(jī)制
- Spring Security OAuth Client配置加載源碼分析
- Spring Security內(nèi)置過濾器詳解
- 為什么加載了兩個(gè)OAuth2AuthorizationRequestRedirectFilter分析
- Spring Security 自定義授權(quán)服務(wù)器實(shí)踐
前言
在前面我們使用最小化配置的方式搭建了自己的授權(quán)服務(wù)器,現(xiàn)在我們依舊用最小化的方式配置自己的資源服務(wù)器。
資源服務(wù)器負(fù)責(zé)scope的鑒權(quán)、authorities的鑒權(quán)、基于用戶角色的鑒權(quán)等。
最小化配置
安裝資源服務(wù)器
1、 新建一個(gè)Spring Boot項(xiàng)目,命名為spring-security-resource-server
2、引入pom.xml依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
其中與授權(quán)服務(wù)器依賴不同的是,資源服務(wù)器有spring boot版本,版本號會有spring boot進(jìn)行管理,不需要顯示聲明。
配置資源服務(wù)器
1、配置application.yml 文件
spring:
security:
oauth2:
resourceserver:
jwt:
issuer-uri: http://localhost:9000
該配置用于指定授權(quán)服務(wù)器地址,資源服務(wù)器將從該地址獲取JWT令牌,并根據(jù)JWT中的屬性進(jìn)一步自我配置,發(fā)現(xiàn)授權(quán)服務(wù)器的公鑰、驗(yàn)證JWT令牌。
2、創(chuàng)建配置類
@EnableWebSecurity(debug = true)
public class ResoruceServerConfig {
@Bean
SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.mvcMatchers("/userinfo/**").hasAuthority("SCOPE_userinfo")
.and()
.oauth2ResourceServer()
.jwt();
return http.build();
}
}
.mvcMatchers("/userinfo/**").hasAuthority("SCOPE_userinfo")
匹配/userinfo/**
地址,允許訪問范圍是SCOPE_userinfo
oauth2ResourceServer()
定義為資源服務(wù)器jwt()
使用JWT令牌
3、 創(chuàng)建一個(gè)資源接口/userinfo/
用來獲取資源所有者基本信息
@Data
public class UserInfoRes {
private String username;
}
創(chuàng)建Rest接口
@RestController
public class UserInfoController {
@GetMapping("/userinfo")
public UserInfoRes getUserInfo() {
UserInfoRes userInfoRes = new UserInfoRes();
userInfoRes.setUsername("阿提說說");
return userInfoRes;
}
}
配置客戶端
目前我們客戶端的配置是這樣的:
spring:
security:
oauth2:
client:
registration:
gitee:
client-id: gitee_clientId
client-secret: gitee_secret
authorization-grant-type: authorization_code
redirect-uri: '{baseUrl}/login/oauth2/code/{registrationId}'
client-name: Gitee
github:
client-id: github_clientId
client-secret: github_secret
# 自定義
customize:
client-id: testClientId
client-secret: testClientSecret
authorization-grant-type: authorization_code
redirect-uri: '{baseUrl}/login/oauth2/code/{registrationId}'
client-name: Customize
scope:
- userinfo
provider:
gitee:
authorization-uri: https://gitee.com/oauth/authorize
token-uri: https://gitee.com/oauth/token
user-info-uri: https://gitee.com/api/v5/user
user-name-attribute: name
# 自定義
customize:
authorization-uri: http://localhost:9000/oauth2/authorize
token-uri: http://localhost:9000/oauth2/token
user-info-uri: http://localhost:9000/userinfo
user-name-attribute: username
這里我們只需要修改customize
部分的user-info-uri
和 user-name-attribute
調(diào)整后配置如下,其他部分跟原來是一樣的。
customize:
authorization-uri: http://localhost:9000/oauth2/authorize
token-uri: http://localhost:9000/oauth2/token
user-info-uri: http://localhost:8090/userinfo
user-name-attribute: username
? user-name-attribute的名字,必須在user-info-uri返回的屬性名中存在
整流程體驗(yàn)
在如上三部分配置完成后,就可以體驗(yàn)了,啟動(dòng)spring-security-resource-server
、spring-security-authorization-server
、spring-security-oauth2-client
瀏覽器訪問地址:http://127.0.0.1:8080/hello,在授權(quán)完成后,即跳轉(zhuǎn)回并顯示結(jié)果。
ResourceServer下能看到帶著token的/userinfo
請求日志。
************************************************************
Request received for GET '/userinfo':
org.apache.catalina.connector.RequestFacade@3418bfc9
servletPath:/userinfo
pathInfo:null
headers:
accept: application/json
authorization: Bearer eyJraWQiOiI5YjZjZWMzNi05ZDYyLTRkMWMtOWRiNi0wMWM1ODQzMDc1N2UiLCJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJ1c2VyIiwiYXVkIjoidGVzdENsaWVudElkIiwibmJmIjoxNjYwOTU1ODQyLCJzY29wZSI6WyJ1c2VyaW5mbyJdLCJpc3MiOiJodHRwOlwvXC9sb2NhbGhvc3Q6OTAwMCIsImV4cCI6MTY2MDk1NjE0MiwiaWF0IjoxNjYwOTU1ODQyfQ.gVWwwfzB-xNuWWUBpgGokIOy5xwV9Wkd05k3rqpk1h92b-TWENB4ZArEL--zpngSyE8iuml0vG3veCv647FDx_EY56ClM-UxH-3Wq0D2f3b6WTgFO5RpCCwRLCHahBlV5g9plr7hWYY5uX2cQ4MsC4-ltZSR6wga5LSLDB-bIK46ZmJ3DOaQFwTTCpWB4OgOuq1j59i9XkgDUc_I8WUsHB4eEDEbBJeOmdimDn5O1Ux6nDhPgLMLcpnrt3lHLmXDTk8Q7hX7YBynO2VBm6wkTeYP4a2rfinfhW-LtF1o3hm8QAY0hn1QKSEeWU5K5qiIOVeSJ5FqrYJ_VQPadT1qAQ
user-agent: Java/11
host: localhost:8090
connection: keep-alive
Security filter chain: [
DisableEncodeUrlFilter
WebAsyncManagerIntegrationFilter
SecurityContextPersistenceFilter
HeaderWriterFilter
CsrfFilter
LogoutFilter
BearerTokenAuthenticationFilter
RequestCacheAwareFilter
SecurityContextHolderAwareRequestFilter
AnonymousAuthenticationFilter
SessionManagementFilter
ExceptionTranslationFilter
FilterSecurityInterceptor
]
************************************************************
總結(jié)
到此,我們通過自己搭建的授權(quán)服務(wù)器和資源服務(wù)器,完整體驗(yàn)了OAuth2流程,再來體會下第一篇文章中說明的交互流程。
在整個(gè)流程中,我們使用的是最嚴(yán)密的授權(quán)碼模式,它將用戶引導(dǎo)到授權(quán)服務(wù)器進(jìn)行身份驗(yàn)證,授權(quán)服務(wù)器將發(fā)放的訪問令牌傳遞給客戶端,目前主流都是使用該模式,因此特別重要,要好好體會。
文章來源:http://www.zghlxwxcb.cn/news/detail-816204.html
源代碼地址:https://github.com/jujunchen/21Study文章來源地址http://www.zghlxwxcb.cn/news/detail-816204.html
到了這里,關(guān)于Spring Security 自定義資源服務(wù)器實(shí)踐的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!