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

JWT登录校验

1.JWT工具

AuthProperties:配置登录校验需要拦截的路径,因为不是所有的路径都需要登录才能访问

JwtProperties:定义与JWT工具有关的属性,比如秘钥文件位置

SecurityConfig:工具的自动装配

JwtTool:JWT工具,其中包含了校验和解析token的功能

hmall.jks:秘钥文件

AuthPropertiesJwtProperties所需的属性要在application.yaml中配置:

hm:jwt:location: classpath:hmall.jks # 秘钥地址alias: hmall # 秘钥别名password: hmall123 # 秘钥文件密码tokenTTL: 30m # 登录有效期auth:excludePaths: # 无需登录校验的路径- /search/**- /users/login- /items/**

定义一个登录校验的过滤器(AuthGlobalFilter):

package com.hmall.gateway.filter;import com.hmall.common.exception.UnauthorizedException;
import com.hmall.common.utils.CollUtils;
import com.hmall.gateway.config.AuthProperties;
import com.hmall.gateway.util.JwtTool;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
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.util.List;@Component
@RequiredArgsConstructor
@EnableConfigurationProperties(AuthProperties.class)
public class AuthGlobalFilter implements GlobalFilter, Ordered {private final JwtTool jwtTool;private final AuthProperties authProperties;private final AntPathMatcher antPathMatcher = new AntPathMatcher();@Overridepublic Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {// 1.获取RequestServerHttpRequest request = exchange.getRequest();// 2.判断是否不需要拦截if(isExclude(request.getPath().toString())){// 无需拦截,直接放行return chain.filter(exchange);}// 3.获取请求头中的tokenString token = null;List<String> headers = request.getHeaders().get("authorization");if (!CollUtils.isEmpty(headers)) {token = headers.get(0);}// 4.校验并解析tokenLong userId = null;try {userId = jwtTool.parseToken(token);} catch (UnauthorizedException e) {// 如果无效,拦截ServerHttpResponse response = exchange.getResponse();response.setRawStatusCode(401);return response.setComplete();}// TODO 5.如果有效,传递用户信息System.out.println("userId = " + userId);// 6.放行return chain.filter(exchange);}private boolean isExclude(String antPath) {for (String pathPattern : authProperties.getExcludePaths()) {if(antPathMatcher.match(pathPattern, antPath)){return true;}}return false;}@Overridepublic int getOrder() {return 0;}
}

http://www.dtcms.com/a/392962.html

相关文章:

  • 对症下药:电商、B2B、本地服务和内容媒体的GEO定制化策略
  • 分类预测 | Matlab实现GWO-BP灰狼算法优化BP神经网络多特征分类预测
  • pcl封装11 (快速定义)旋转矩阵
  • Windows 系统中如何通过 Docker 调用 CUDA 和 cuDNN 加速大模型推理
  • 从零编写vue3系统--5步教学
  • 嵌入式Linux C语言程序设计三
  • 【记录】初赛复习 Day5 6(2021S第一轮错题,内附深井题目讲解)
  • 【C++】类和对象—(下) 收官之战
  • 人工智能学习:什么是迁移学习
  • 模型进阶与神经网络
  • 微软.NET离线运行库合集 v2025.09.09_Win中文_NET运行库_安装教程
  • Galileo AI-AI驱动的UI界面设计工具
  • 布谷鸟布隆过滤器和计数式布隆过滤器和原始布隆过滤器相比分别解决了什么问题?
  • 大模型介绍
  • 基于Springboot的无人之境智能酒店服务平台
  • ICCV-2025 | 大模型驱动的认知导航框架!CogNav:面向目标导航的大型语言模型驱动的认知过程建模
  • java-异常
  • 网络编程:一个 TCP 服务器的简易实现(epoll 版本)
  • 【MySQL学习】关于MySql语句执行、查询、更新流程原理总结
  • C++语法深度剖析与面试核心详解
  • 【Tomcat】基础总结:类加载机制
  • 127、【OS】【Nuttx】【周边】效果呈现方案解析:比较浮点数(上)
  • 计网协议簇具体协议
  • 电路分析基础笔记
  • 【JVM 常用工具命令大全】
  • 从iload_1 iload_2 iadd字节码角度看jvm字节码执行
  • openssl 启用AES NI加速对AES加密性能影响的测试
  • LeetCode:32.随机链表的复制
  • 基于SpringBoot+Vue的旅游系统【协同过滤推荐算法+可视化统计】
  • 前端实现一个星空特效的效果(实战+讲解)