通过springcloud gateway优雅的进行springcloud oauth2认证和权限控制

server/2024/9/22 2:55:30/

代码地址

如果对你有帮助请给个start,本项目会持续更新,目标是做一个可用的快速微服务开发平台,成为接私活,毕设的开发神器, 欢迎大神们多提意见和建议

使用的都是spring官方最新的版本,版本如下:

  <dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>2.2.6.RELEASE</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Hoxton.SR3</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-dependencies</artifactId><version>2.2.0.RELEASE</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement>

springcloud gateway进行token校验时使用框架封装好的,不在需要通过自定义过滤器进行认证
引入依赖:

<dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId></dependency><dependency><groupId>org.springframework.security</groupId><artifactId>spring-security-config</artifactId></dependency><dependency><groupId>org.springframework.security</groupId><artifactId>spring-security-oauth2-resource-server</artifactId></dependency><dependency><groupId>org.springframework.security</groupId><artifactId>spring-security-oauth2-jose</artifactId></dependency></dependencies>

修改配置文件

spring:security:oauth2:resourceserver:jwt:jwk-set-uri: http://localhost:8000/public/key.json

增加一个配置文件

package com.digierp.gateway.config;import com.digierp.gateway.component.PermissionAuthorizationManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
import org.springframework.security.config.web.server.ServerHttpSecurity;
import org.springframework.security.web.server.SecurityWebFilterChain;/*** @author liuhao* @date 2020/4/10*/
@EnableWebFluxSecurity
public class GateWayWebSecurityConfig {@Autowiredprivate PermissionAuthorizationManager permissionAuthorizationManager;@Beanpublic SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {http.authorizeExchange().pathMatchers("/security/**").permitAll().anyExchange().access(permissionAuthorizationManager);http.oauth2ResourceServer().jwt();http.csrf().disable();return http.build();}}

需要在网关做权限控制添加ReactiveAuthorizationManager<AuthorizationContext>接口的实现, 如果不需要,请忽略

package com.digierp.gateway.component;import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.security.authorization.AuthorizationDecision;
import org.springframework.security.authorization.ReactiveAuthorizationManager;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.jwt.Jwt;
import org.springframework.security.web.server.authorization.AuthorizationContext;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;/*** @author liuhao* @date 2020/4/11*/
@Slf4j
@Component
public class PermissionAuthorizationManager implements ReactiveAuthorizationManager<AuthorizationContext> {/*** 实现权限验证判断*/@Overridepublic Mono<AuthorizationDecision> check(Mono<Authentication> authenticationMono, AuthorizationContext authorizationContext) {ServerWebExchange exchange = authorizationContext.getExchange();//请求资源String requestPath = exchange.getRequest().getURI().getPath();return authenticationMono.map(auth -> {new AuthorizationDecision(checkAuthorities(exchange, auth, requestPath));}).defaultIfEmpty(new AuthorizationDecision(false));}//权限校验private boolean checkAuthorities(ServerWebExchange exchange, Authentication auth, String requestPath) {Jwt principal = (Jwt) auth.getPrincipal();log.info("访问的URL是:{}用户信息:{}",requestPath, principal.getClaims().get("user_name"));return RandomUtils.nextInt() % 2 == 0 ;}}
image.png


喜欢的朋友记得点赞、收藏、关注哦!!!


http://www.ppmy.cn/server/120087.html

相关文章

windows C++-并行编程-并行算法(三)-分区工作

并行模式库 (PPL) 提供了对数据集合并行地执行工作的算法。这些算法类似于 C 标准库提供的算法。并行算法由并发运行时中的现有功能组成。 若要对数据源操作进行并行化&#xff0c;一个必要步骤是将源分区为可由多个线程同时访问的多个部分。 分区程序将指定并行算法应如何在线…

【Day14-单例设计模式动态代理】

单例设计模式 什么是设计模式&#xff08;Design pattern&#xff09; ? 一个问题通常有n种解法&#xff0c;其中肯定有一种解法是最优的&#xff0c;这个最优的解法被人总结出来了&#xff0c;称之为设计模式。设计模式有20多种&#xff0c;对应20多种软件开发中会遇到的问题…

智慧安防监控EasyCVR视频汇聚管理平台如何修改视频流分辨率?

智慧安防监控EasyCVR视频管理平台能在复杂的网络环境中&#xff0c;将前端监控设备进行统一集中接入与汇聚管理。EasyCVR平台支持H.264/H.265视频压缩技术&#xff0c;可在4G/5G/WIFI/宽带等网络环境下&#xff0c;传输720P/1080P/2K/4K高清视频。视频流经平台处理后&#xff0…

十三、SOA(企业服务总线ESB架构实现)

**企业服务总线&#xff08;Enterprise Service Bus&#xff0c;ESB&#xff09;**是SOA架构中的核心组成部分&#xff0c;主要用于促进企业内部异构系统和应用程序之间的集成与通信。ESB提供了一个统一的服务集成平台&#xff0c;通过使用消息路由、协议转换、服务编排等功能&…

Gnu Radio抓取WiFi信号,流程图中模块功能

模块流程如图所示&#xff1a; GNURadio中抓取WiFi信号的流程图中各个模块的功能&#xff1a; UHD: USRP Source&#xff1a; 使用此模块配置USRP硬件进行信号采集。设置频率、增益、采样率等参数。Complex to Mag^2&#xff1a; 将复数IQ数据转换为幅度的平方。Delay&#xf…

为写论文头疼?推荐4款ai写毕业论文初稿的软件

写论文对于许多学生来说是一项既重要又具挑战性的任务。为了帮助大家更高效地完成这一过程&#xff0c;我将推荐四款优秀的AI写毕业论文初稿的软件&#xff0c;并详细介绍它们的功能和优势。 传送门&#xff1a;https://www.aipaperpass.com?piclLGw 千笔-AIPassPaper是一款功…

代码随想录打卡Day34

前两题自己AC的&#xff0c;后两题确实有点难&#xff0c;看讲解才AC的&#xff0c;中规中矩吧。 62.不同路径 这个主要是需要先把第0行和第0列初始化&#xff0c;全都赋值为1&#xff0c;然后从坐标&#xff08;1&#xff0c;1&#xff09;开始遍历&#xff0c;每一个格子的…

记录一下,Vcenter清理/storage/archive空间

一、根因 vpostgres&#xff1a;这个目录可能包含与 vCenter Server 使用的 PostgreSQL 数据库相关的归档文件过多&#xff0c;导致空间被占用。 二、处理过程 1、SSH登陆到Vcenter. 2、df -Th **图中可以看到 /storage/archive 使用占比很高。 /storage/archive 目录通常用…