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

news/2024/9/22 19:40:13/

代码地址

如果对你有帮助请给个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/news/1528982.html

相关文章

phpstudy 建站使用 php8版本打开 phpMyAdmin后台出现网页提示致命错误:(phpMyAdmin这是版本问题导致的)

报错提示&#xff1a; 解决方法&#xff1a;官网下载phpmyadmin 5.2.1版本。 下载地址&#xff1a;phpMyAdmin 将网站根目录phpMyAdmin4.8.5里面的文件换成 官网下载的5.2.1版本即可。 重启网站&#xff0c;打开phpMyAdmin后台即可&#xff08;若打不开更改 mysql密码即可&am…

Apache ZooKeeper 及 Curator 使用总结

1. 下载 官网地址&#xff1a;Apache ZooKeeper 点击下载按钮 选择对应的版本进行下载 2. 使用 1、解压 tar -zxf apache-zookeeper-3.9.2-bin.tar.gz2、复制配置文件&#xff0c;有一个示例配置文件 conf/zoo_sample.cfg&#xff0c;此文件不能生效&#xff0c;需要名称为…

C语言6大常用标准库 -- 4.<math.h>

目录 引言 4. C标准库--math.h 4.1 简介 4.2 库变量 4.3 库宏 4.4 库函数 4.5 常用的数学常量 &#x1f308;你好呀&#xff01;我是 程序猿 &#x1f30c; 2024感谢你的陪伴与支持 ~ &#x1f680; 欢迎一起踏上探险之旅&#xff0c;挖掘无限可能&#xff0c;共同成长&…

数据结构:堆排序

更完堆&#xff0c;再更一期堆排序 利用容器实现堆排序 在C等高级语言中&#xff0c;基本上都有堆或者优先队列等容器&#xff0c;借助这些容器很容易实现堆排序。 将数组里面的元素先插入到容器中&#xff0c;建好堆&#xff0c; 接着将容器中的元素&#xff0c;按照升序或降…

C++ boost——时间与日期

文章目录 timerprogress_timerdate_time创建日期访问日期日期输出转换C结构日期长度日期运算日期区间日期区间运算日期迭代器其他功能综合运用 timer 是一个小型计时器&#xff0c;提供毫秒级的精度 适用于要求不高的程序计时任务 class timer { public :timer()(_start_time…

2024年9月16日--9月22日,(工作日源码抄写+周末ue5肉鸽独立游戏)

继续进行&#xff0c;按照周一到周五每天晚上在公司抄3小时源码gpu精粹催眠&#xff0c;周末独立游戏的方式&#xff0c;也就是说&#xff0c;要把独立游戏在周末做下去较长的一段时间&#xff0c;看看这条路能不能走通。 具体执行情况&#xff1a; 周一&#xff1a; 15&#…

SQL优化之深度分页优化

深度分页是指在分页查询场景下&#xff0c;当数量很大时&#xff0c;随着页数的增大&#xff0c;查询会变得越慢&#xff0c;数据库在梳理分页查询时需要跳过大量的数据&#xff0c;降低查询效率。 //查询第 1 到第 20 条商品 select * from products limit 20 offset 0; sele…

Python 二级考试

易错点 电脑基础知识 定义学生关系模式如下&#xff1a;Student &#xff08;S#&#xff0c; Sn&#xff0c; Ssex&#xff0c;class&#xff0c;monitorS#&#xff09;&#xff08;其属性分别为学号、学生名、性别、班级和班长学号&#xff09; 在关系模式中&#xff0c;如果…