Springboot3+SpringSecurity6Oauth2+vue3前后端分离认证授权-资源服务
资源服务
- 整体流程
- 后端
- 技术栈
- 项目结构
- 代码
整体流程
后端
技术栈
springboot3
spring security6 oauth2
项目结构
代码
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.example</groupId><artifactId>security</artifactId><version>1.0-SNAPSHOT</version></parent><artifactId>security-resource</artifactId><properties><maven.compiler.source>17</maven.compiler.source><maven.compiler.target>17</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></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-security</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-oauth2-resource-server</artifactId></dependency><dependency><groupId>com.alibaba.fastjson2</groupId><artifactId>fastjson2</artifactId></dependency></dependencies></project>
application.yml
server:port: 8082spring:security:oauth2:resourceserver:jwt:jwk-set-uri: http://localhost:8081/oauth2/jwks
# issuer-uri: http://localhost:8081
SecurityConfig.java
package org.example.resource.config;import com.alibaba.fastjson2.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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.web.SecurityFilterChain;import java.util.HashMap;
import java.util.Map;/*** Spring security配置** @author qiongying.huai* @version 1.0* @date 15:04 2025/6/23*/
@Configuration
@EnableWebSecurity
public class SecurityConfig {private final Logger logger = LoggerFactory.getLogger(getClass());@Beanpublic SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {http.authorizeHttpRequests(authorize -> authorize.anyRequest().authenticated())// 启用 OAuth2 资源服务器功能,并指定使用 JWT 进行验证
// .oauth2ResourceServer(oauth2 -> oauth2.jwt(Customizer.withDefaults()))
// .oauth2ResourceServer(oauth2 -> oauth2.jwt(j -> j.decoder(JwtDecoders.fromIssuerLocation("http://localhost:8081")))).oauth2ResourceServer(oauth2 -> oauth2.jwt(Customizer.withDefaults()).authenticationEntryPoint((request, response, authException) -> {logger.error("request: {}, error: ", request.getRequestURI(), authException);Map<String, Object> responseData = new HashMap<>(4);responseData.put("code", 1001);responseData.put("msg", authException.getMessage());response.setContentType("application/json;charset=utf-8");response.setStatus(200);response.getWriter().write(JSON.toJSONString(responseData));}));return http.build();}
}
ResourceController.java
package org.example.resource.controller;import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.HashMap;
import java.util.Map;/*** 资源接口,返回认证授权信息** @author qiongying.huai* @version 1.0* @date 15:22 2025/6/27*/
@RestController
public class ResourceController {@GetMapping("/authentication")public Map<String, Object> authentication() {Map<String, Object> res = new HashMap<>(4);res.put("code", 200);res.put("data", SecurityContextHolder.getContext().getAuthentication());return res;}
}