OAuth2 集成 - 構(gòu)造資源服務(wù)器
1. 前言
上一節(jié)中,我們使用了 Spring Security 構(gòu)造了自己的認證服務(wù)器,本節(jié)中我們將繼續(xù)討論如何將構(gòu)建資源服務(wù)器。
資源服務(wù)器負責(zé)提供受保護的資源,客戶端訪問這些受保護的資源時需要現(xiàn)在 OAuth2.0 認證中心核驗身份及權(quán)限,核驗的依據(jù)是用戶認證通過后獲得的 token,本節(jié)我們討論資源服務(wù)器的實現(xiàn)方法。
本小節(jié)實例開發(fā)環(huán)境:
本小節(jié)所使用的實例代碼是基于 Spring 官網(wǎng)中提供的最小化 HelloWorld 模板創(chuàng)建,請點此下載完整的 HelloWorld 模板壓縮包。
2. 過程實現(xiàn)
在前面章節(jié),我們討論了如何快速建立一個 Spring Security 的認證服務(wù)器,此處我們將在上一例的認證服務(wù)器基礎(chǔ)上增加資源服務(wù)器。
2.1 創(chuàng)建 Spring Boot web 服務(wù)端應(yīng)用
工程目錄結(jié)構(gòu)如下:
? OAuth2ResourceServer/
? src/
? main/
? java/imooc/springsecurity/oauth2/server/
? config/
OAuth2ResourceServerController.java # 配置控制器,用來扮演資源
OAuth2ResourceServerSecurityConfiguration.java # 資源服務(wù)器相關(guān)配置均在此處
OAuth2ResourceServerApplication.java # 啟動類
? resources/
application.yml # 配置 OAuth2.0 認證服務(wù)器的地址等信息
? test/
pom.xml
在 pom.xml 文件中增加依賴項,相比「用戶名密碼認證實例」,此處注意添加了 OAuth2 自動配置的相關(guān)依賴。spring-security-oauth2-autoconfigure
和 spring-security-oauth2-resource-server
。完整 pom.xml 文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.example</groupId>
<artifactId>OAuth2ResourceServer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-oauth2-resource-server</artifactId>
<version>5.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security.oauth.boot</groupId>
<artifactId>spring-security-oauth2-autoconfigure</artifactId>
<version>2.2.5.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
創(chuàng)建 SpringSecurity OAuth2 資源服務(wù)器配置類,src/main/java/imooc/springsecurity/oauth2/server/OAuth2ResourceServerSecurityConfiguration.java
。
- 使其繼承
org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter
類,并其增加@EnableResourceServer
標(biāo)簽,以聲明此類作為 OAuth2 資源服務(wù)器的配置依據(jù); - 在
configure(HttpSecurity http)
方法中配置其資源的訪問權(quán)限,本例中默認所有資源需要認證用戶才能訪問;
完整代碼如下:
package imooc.springsecurity.oauth2.server.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
@Configuration
@EnableResourceServer
public class OAuth2ResourceServerSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests(authorizeRequests ->
authorizeRequests.anyRequest().authenticated()
)
.csrf().disable();
}
}
在 application.yml 文件中,需要將 OAuth2.0 認證服務(wù)器的信息配置進去。
server:
port: 8081
security:
oauth2:
client:
client-id: reader # 客戶端標(biāo)識,與認證服務(wù)器中的寫法相同
client-secret: secret # 客戶端秘鑰,與認證服務(wù)器中的寫法相同
user-authorization-uri: http://localhost:8080/oauth/authorize # 客戶端鑒權(quán)地址
access-token-uri: http://localhost:8080/oauth/token # 客戶端獲取 Token 地址
resource:
id: reader # 資源服務(wù)器標(biāo)識,這里可以根據(jù)業(yè)務(wù)情況填寫
token-info-uri: http://localhost:8080/oauth/check_token # 驗證 Token 的地址
至此,資源服務(wù)器的核心內(nèi)容均配置完成。
2.2 運行及測試
我們用 curl 工具測試 OAuth2.0 資源服務(wù)器。
測試流程如下:
- 利用前一小節(jié)的指令向認證服務(wù)器獲取票據(jù) Token;
curl reader:secret@localhost:8080/oauth/token -d grant_type=password -d username=admin -d password=123456
獲得結(jié)果:
{
"access_token": "8b7a6968-cf6e-40d0-a988-f3260f7836a6",
"token_type": "bearer",
"expires_in": 599995027,
"scope": "message:read"
}
- 使用 Token 作為參數(shù),請求資源服務(wù)器上的內(nèi)容,此時需要在請求中增加認證頭信息「Authorization」。
curl --location --request GET 'http://localhost:8081/' \
--header 'Authorization: Bearer 8b7a6968-cf6e-40d0-a988-f3260f7836a6'
如果驗證成功,返回資源服務(wù)器內(nèi)對應(yīng)內(nèi)容;
如果驗證失敗,返回 401 錯誤信息,提示 token 驗證失敗。
{
"error":"invalid_token",
"error_description":"8b7a6968-cf6e-40d0-a988-f3260f7836a6"
}
3. 小結(jié)
本節(jié)我們討論了 OAuth2.0 規(guī)范下資源服務(wù)器的構(gòu)建方式,主要知識點有:
- Spring Security 支持通過注解形式構(gòu)造資源服務(wù)器;
- 資源服務(wù)器接收客戶端傳來的 Token,并交由認證服務(wù)器檢查 Token 的有效性。
至此,關(guān)于 Spring Security 如何集成 OAuth2.0 的內(nèi)容就告一段落了,下節(jié)我們將討論一種在大企業(yè)中常被用作統(tǒng)一身份解決方案的認證框架:「SAML2.0」。