SpringBoot+SpringSecurity OAuth2 认证服务搭建实战 (四)资源服务器搭建

目标: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至少要满足两个条件:

  1. Token是由授权服务器用私钥签署的
  2. 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测试脚本。

下一章讲一下客户端服务的搭建。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

觉自性本然

您的鼓励与支持是我最大的动力~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值