当前位置: 首页 > web >正文

Springboot3+SpringSecurity6Oauth2+vue3前后端分离认证授权-资源服务

资源服务

  • 整体流程
  • 后端
    • 技术栈
    • 项目结构
    • 代码

整体流程

客户端前端客户端后端授权服务前端授权服务后端资源服务后端请求/hello接口无权限返回code=1001跳转到登录页请求登录/login接口返回授权服务获取授权码页面地址跳转到获取授权码页面请求获取授权码/oauth2/authorize接口无权限返回code=1001跳转到登录页请求登录/login接口验证用户密码登录成功返回token跳转回获取授权码页面带token请求获取授权码/oauth2/authorize接口返回授权码和客户端回调地址(带token)跳转到客户端回调地址(带token)请求回调/callback接口带token请求获取access_token的/oauth2/token接口返回access_token返回access_token跳转回最初始地址/带access_token请求/hello接口带access_token请求/authentication接口返回认证授权信息Authentication带Authentication走接下来流程返回/hello接口结果客户端前端客户端后端授权服务前端授权服务后端资源服务后端

后端

技术栈

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;}
}
http://www.xdnf.cn/news/19957.html

相关文章:

  • 单片机键盘接口程序设计(汇编语言)
  • 血缘元数据采集开放标准:OpenLineage Guides 在 Airflow 中使用 OpenLineage Proxy
  • 快速在RK3588上部署运行DeepSeek-R1-Distill-Qwen-1.5B模型并进行板端推理调用流程记录
  • 重生之IOday4————多进程通信
  • Python学习笔记--使用Django修改和删除数据
  • Python学习笔记--使用Django查询数据
  • 网络协议之https?
  • 智能开发新突破:大模型驱动的QAC与TESSY助手实战分享
  • 【工具变量】上市公司绿色供应链管理示范企业DID数据(2010-2024年)
  • phpstorm 操作git 另外的操作在 我的收藏
  • Maven动态控制版本号秘籍:高效发包部署,版本管理不再头疼!
  • Top 10 Kali Linux Tools for Hacking 2025.2
  • 《WINDOWS 环境下32位汇编语言程序设计》第11章 动态链接库和钩子
  • nano banana官方最强Prompt模板来了!六大场景模板详解
  • GEM5学习(4): 运行全系统模式的ARM系统
  • 如何构建企业级RAG知识库?实战方法、关键细节与平台选型
  • 只会刷App?大学生学透Android开发,直接开挂!
  • 【沉浸式解决问题】浮点数计算精度误差,round后值错误,0.1+0.2不等于0.3?
  • Ai Qwen3解答epochs多少为最佳 仅共参考
  • 机器视觉opencv总结
  • NuttX编译流程与config.h生成解析
  • 插入排序及希尔排序
  • AR智慧运维系统介绍
  • 【机器学习】实战:市场增长点分析挖掘项目
  • 算法模板(Java版)_链表(单链表、双链表)、栈和队列
  • HarmonyOS Stage 模型深度解析:构建现代化、高性能应用
  • IotDB批量数据脱敏DEMO
  • wpf 自定义控件,只能输入小数点,并且能控制小数点位数
  • 微服务多级缓存:从问题到实战(小白也能看懂的亿级流量方案)
  • FastJson