目标:OAuth2资源服务器搭建
技术信息
Spring Boot 3.3.0
Spring Security OAuth2 Resource Server 6.3.0
JDK 17
Maven
资源服务器的作用
资源服务器主要就是管理资源的访问,就像一名仓库管理员。
仓库管理员管理仓库中的资源。
资源服务器管理服务器上的资源。
只不过咱们这里所说的资源是网络资源,例如:接口,数据,图片,多媒体等等。
而咱们这边主要以“接口”为例。如:/userinfo
资源服务器工作原理
资源服务器怎么管理资源呢?
它是有一个认证机制的,就像OAuth协议中规定的。
我们设置资源服务器获取授权服务器Meta信息中的公钥,从而构建出Jwt Decoder(解码器)。
Jwt Decoder(解码器) 能对 Jwt Token进行验签。
当客户端带着token去访问接口,资源服务器会进行认证,只有验签没问题才能访问。
所以请求中的token至少要满足两个条件:
- Token是由授权服务器用私钥签署的
- Token有效时间没过期
构建Maven项目
<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 https://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>3.3.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.oauth.resource.demo</groupId>
<artifactId>oauth_resource_demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>17</java.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.build.locales>zh_CN</project.build.locales>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
</dependency>
</dependencies>
</project>
以上是pom的全部信息。建一个simple maven 项目后直接贴就行。
项目整体结构
项目GIT地址 ,大家可以下载到本地运行哦!
关键代码
package com.oauth.resource.demo.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.oauth2.jwt.JwtDecoder;
import org.springframework.security.oauth2.jwt.JwtDecoders;
import org.springframework.security.web.SecurityFilterChain;
/**
* OAuth resource configuration.
*
* @author 3218138121@qq.com
*/
@Configuration
@EnableWebSecurity
public class OAuth2ResourceServerSecurityConfiguration {
@Value("${spring.security.oauth2.resourceserver.jwt.issuer-uri}")
String issuerUri;
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.sessionManagement((sessionManagement)->{
sessionManagement.sessionCreationPolicy(SessionCreationPolicy.STATELESS);
})
.authorizeHttpRequests((authorize) -> {
authorize.requestMatchers(HttpMethod.GET, "/message/*","/user/*")
.hasAuthority("SCOPE_openid")
.anyRequest()
.permitAll();
}).oauth2ResourceServer(resourceServer -> resourceServer.jwt(Customizer.withDefaults()));// 资源服务
return http.build();
}
/**
* 从授权服务器发布的meta地址中获取公钥,构建解码器
* JwtDecoder 用来验证 签名和解码token
* @return
*/
@Bean
public JwtDecoder jwtDecoder() {
return JwtDecoders.fromIssuerLocation(this.issuerUri);
}
}
省下的web包中的Controller都是些简单的接口,可以去git仓库中查看。
而对应server端代码也同样放到了demo_02分支下了。
大家可以去下载到本地运行。
目前还是把jemter当做客户端来用,git仓库中有jemter测试脚本。
下一章讲一下客户端服务的搭建。