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

SpringCloud Gateway网关 全局过滤器[AntPathMatcher 某些路径url禁止访问] 实现用户鉴权

前提:先保证Gateway网关项目 和 Nacos注册中心 等可以正常访问和调用,搭建方法可查看博文
SpringCloud Gateway网关 项目创建 及 整合Nacos开发_spring gateway如何设置工程名称-CSDN博客


类似的全局鉴权方案,参考如下: 
SpringCloud Gateway网关 全局过滤器[header token] 实现用户鉴权_gateway添加鉴权过滤器-CSDN博客

核心代码如下: 

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;import java.nio.charset.StandardCharsets;@Component
public class GlobalAuthFilter implements GlobalFilter, Ordered {private AntPathMatcher antPathMatcher = new AntPathMatcher();@Overridepublic Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {ServerHttpRequest serverHttpRequest = exchange.getRequest();String path = serverHttpRequest.getURI().getPath();// 判断路径中是否包含 system,如果包含system,则不允许访问if (antPathMatcher.match("/**/system/**", path)) {ServerHttpResponse response = exchange.getResponse();response.getHeaders().add("Content-Type", "application/json;charset=UTF-8");response.setStatusCode(HttpStatus.FORBIDDEN);DataBufferFactory dataBufferFactory = response.bufferFactory();DataBuffer dataBuffer = dataBufferFactory.wrap("无权限".getBytes(StandardCharsets.UTF_8));return response.writeWith(Mono.just(dataBuffer));//设置状态码 未授权401//exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);//个人理解,终止输出访问//return exchange.getResponse().setComplete();}return chain.filter(exchange);}/*** 优先级提到最高** @return*/@Overridepublic int getOrder() {return 0;}
}

http://www.lryc.cn/news/304942.html

相关文章:

  • ELK介绍以及搭建
  • Spring中的ApplicationContext.publishEvent
  • jackson、gson、fastjson和json-lib四种主流json解析框架对比
  • 已解决:IDEA中@Autowired自动注入MyBatis Mapper报红警告的几种解决方法
  • 在jar里限制指定的包名才可调用(白名单)。
  • python 提取PDF文字
  • 电气机械5G智能工厂数字孪生可视化平台,推进电气机械行业数字化转型
  • C# (WebApi)整合 Swagger
  • 导入excel某些数值是0
  • wo-gradient-card是一款采用uniapp实现的透明辉光动画卡片
  • Spark: a little summary
  • 018—pandas 生成笛卡尔积排列组合合并多列字符串数据
  • 【算法与数据结构】链表、哈希表、栈和队列、二叉树(笔记二)
  • bugku3
  • 相机的白平衡
  • 刷题日记-Day2- Leedcode-977. 有序数组的平方,209. 长度最小的子数组,59. 螺旋矩阵 II-Python实现
  • Linux命令-chcon命令(修改对象(文件)的安全上下文)
  • 【漏洞复现】大华DSS视频管理系统信息泄露漏洞
  • websocket了解下
  • docker install private registry 【docker 安装 registry 仅证书认证】
  • JavaWeb——004Maven SpringBootWeb入门
  • 数据结构与算法-常用排序算法
  • 链表之“无头单向非循环链表”
  • 一休哥助手网页版如何使用
  • 个人博客系统测试
  • 智慧应急的未来:物联网技术引领智慧应急发展新趋势
  • 字符串摘要(C语言)
  • Linux进一步研究权限-----------ACL使用
  • 剪辑视频调色软件有哪些 剪辑视频软件哪个最好 剪辑视频怎么学 剪辑视频的方法和步骤 会声会影2024 会声会影视频制作教程
  • 【Linux进阶之路】Socket —— “UDP“ “TCP“