这篇文章主要介绍了springcloud获取网关封装的头部信息,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
后台系统如何获取请求头里的数据
1. 网关层封装数据到head头
@Component
@Slf4j
public class LoginGatewayFilterFactory extends AbstractGatewayFilterFactory {
@Override
public GatewayFilter apply(Object config) {
return (exchange, chain) -> {
// 获取 request
ServerHttpRequest req = exchange.getRequest();
URI uri = req.getURI();
// 日志
log.info(StringUtils.join("【", req.getMethod(), "】", uri.getHost(), ":", uri.getPort(), ", 【path】", uri.getPath()));
// 检查请求 uri 是否需要鉴权
String path = uri.getPath();
if (StringUtils.equalsAny(path, "/sysLogin")) {
return chain.filter(exchange);
}
// 获取请求的 token
HttpHeaders reqHeaders = req.getHeaders();
String token = req.getHeaders().getFirst("token");
if (StringUtils.isBlank(token)) {
return FilterUtil.setParamToResponse(exchange, HttpStatus.UNAUTHORIZED, "请先登录");
}
// 验证 token
JWT jwt = JWTUtil.parseToken(token);
if (jwt == null) {
log.info(StringUtils.join("[token=", token, "]转换为jwt失败"));
return FilterUtil.setParamToResponse(exchange, HttpStatus.UNAUTHORIZED, "令牌未识别");
}
JSONObject json = jwt.getPayloads();
if (json == null || json.isEmpty() || json.isNull("key") || json.isNull(JWTPayload.ISSUED_AT)) {
log.info(StringUtils.join("[token=", token, "]解析异常"));
return FilterUtil.setParamToResponse(exchange, HttpStatus.UNAUTHORIZED, "令牌未识别");
}
String userId = json.get("key", String.class);
if (StringUtils.isBlank(userId)) {
return FilterUtil.setParamToResponse(exchange, HttpStatus.UNAUTHORIZED, "令牌未识别");
}
boolean verify = jwt.setKey(JwtUtils.createKey(userId)).verify();
if (!verify) {
return FilterUtil.setParamToResponse(exchange, HttpStatus.UNAUTHORIZED, "令牌未识别");
}
// 过期时间
long seconds = Instant.now().getEpochSecond() - json.get(JWTPayload.ISSUED_AT, Long.class);
if (seconds < 0) {
return FilterUtil.setParamToResponse(exchange, HttpStatus.UNAUTHORIZED, "令牌未识别");
} else if (seconds >= 1500 && seconds <= 1800) {
// 自动刷新令牌
ServerHttpResponse httpResponse = exchange.getResponse();
HttpHeaders repHeaders = httpResponse.getHeaders();
repHeaders.set("token", JwtUtils.createToken(userId));
} else if (seconds > 1800) {
// 登录超时
return FilterUtil.setParamToResponse(exchange, HttpStatus.UNAUTHORIZED, "登录超时");
}
Consumer<HttpHeaders> headersConsumer = httpHeaders -> {
for (Map.Entry<String, List<String>> entry : reqHeaders.entrySet()) {
httpHeaders.put(entry.getKey(), entry.getValue());
}
httpHeaders.add("userId", userId);
};
return chain.filter(exchange.mutate().request(req.mutate().headers(headersConsumer).build()).build());
};
}
}
2. controller层代码实现
@ApiOperation(value = "添加")
@PostMapping("")
public PojoBaseResponse<Boolean> add(@RequestBody @Validated MainRouteDTO vo, @RequestHeader String userId) {
return ResponseUtils.pojo(mainRouteService.add(vo, userId));
}
到此这篇关于springcloud获取网关封装的的文章就介绍到这了,更多相关springcloud网关头部信息内容请搜索编程学习网以前的文章希望大家以后多多支持编程学习网!
沃梦达教程
本文标题为:springcloud如何获取网关封装的头部信息
基础教程推荐
猜你喜欢
- ConditionalOnProperty配置swagger不生效问题及解决 2023-01-02
- springboot自定义starter方法及注解实例 2023-03-31
- Java实现查找文件和替换文件内容 2023-04-06
- JDK数组阻塞队列源码深入分析总结 2023-04-18
- Java数据结构之对象比较详解 2023-03-07
- Java并发编程进阶之线程控制篇 2023-03-07
- java实现多人聊天系统 2023-05-19
- java基础知识之FileInputStream流的使用 2023-08-11
- Java实现线程插队的示例代码 2022-09-03
- Java文件管理操作的知识点整理 2023-05-19