Spring Security 自定義資源服務器實踐過程
前言
在前面我們使用最小化配置的方式搭建了自己的授權服務器,現(xiàn)在我們依舊用最小化的方式配置自己的資源服務器。
資源服務器負責scope的鑒權、authorities的鑒權、基于用戶角色的鑒權等。
最小化配置
安裝資源服務器
1、 新建一個Spring Boot項目,命名為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>
其中與授權服務器依賴不同的是,資源服務器有spring boot版本,版本號會有spring boot進行管理,不需要顯示聲明。
配置資源服務器
1、配置application.yml 文件
spring: security: oauth2: resourceserver: jwt: issuer-uri: http://localhost:9000
該配置用于指定授權服務器地址,資源服務器將從該地址獲取JWT令牌,并根據(jù)JWT中的屬性進一步自我配置,發(fā)現(xià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()
定義為資源服務器jwt()
使用JWT令牌
3、 創(chuàng)建一個資源接口/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
調整后配置如下,其他部分跟原來是一樣的。
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返回的屬性名中存在
整流程體驗
在如上三部分配置完成后,就可以體驗了,啟動spring-security-resource-server
、spring-security-authorization-server
、spring-security-oauth2-client
瀏覽器訪問地址:http://127.0.0.1:8080/hello,在授權完成后,即跳轉回并顯示結果。
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 ] ************************************************************
總結
到此,我們通過自己搭建的授權服務器和資源服務器,完整體驗了OAuth2流程,再來體會下第一篇文章中說明的交互流程。
在整個流程中,我們使用的是最嚴密的授權碼模式,它將用戶引導到授權服務器進行身份驗證,授權服務器將發(fā)放的訪問令牌傳遞給客戶端,目前主流都是使用該模式,因此特別重要,要好好體會。
源代碼地址:https://github.com/jujunchen/21Study
到此這篇關于Spring Security 自定義資源服務器實踐過程的文章就介紹到這了,更多相關Spring Security 自定義資源服務器內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java創(chuàng)建型設計模式之抽象工廠模式(Abstract?Factory)
當系統(tǒng)所提供的工廠所需生產的具體產品并不是一個簡單的對象,而是多個位于不同產品等級結構中屬于不同類型的具體產品時需要使用抽象工廠模式,抽象工廠模式是所有形式的工廠模式中最為抽象和最具一般性的一種形態(tài)2022-09-09mybatis?plus新增(insert)數(shù)據(jù)獲取主鍵id的問題
這篇文章主要介紹了mybatis?plus新增(insert)數(shù)據(jù)獲取主鍵id的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03Java的idea連接mongodb數(shù)據(jù)庫的詳細教程
這篇文章主要介紹了Java的idea連接mongodb數(shù)據(jù)庫的詳細教程,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11JavaWeb中使用JavaMail實現(xiàn)發(fā)送郵件功能實例詳解
這篇文章主要介紹了JavaWeb中使用JavaMail實現(xiàn)發(fā)送郵件功能的實例代碼,非常不錯具有參考借鑒價值,感興趣的朋友一起看看吧2016-05-05