From 8766cfc344d3635cd7e50a7c674cd5feb54d5a3d Mon Sep 17 00:00:00 2001 From: liriming <1343021927@qq.com> Date: 星期一, 02 十二月 2024 09:30:35 +0800 Subject: [PATCH] 预警mq监听并发送 --- shasteel-biz/src/main/java/com/iailab/module/shasteel/config/FeignTokenInterceptor.java | 41 +++++++++++++++++++++++++++++++++++++++++ 1 files changed, 41 insertions(+), 0 deletions(-) diff --git a/shasteel-biz/src/main/java/com/iailab/module/shasteel/config/FeignTokenInterceptor.java b/shasteel-biz/src/main/java/com/iailab/module/shasteel/config/FeignTokenInterceptor.java new file mode 100644 index 0000000..7a2bbbb --- /dev/null +++ b/shasteel-biz/src/main/java/com/iailab/module/shasteel/config/FeignTokenInterceptor.java @@ -0,0 +1,41 @@ +package com.iailab.module.shasteel.config; + +import com.iailab.module.shasteel.util.token.IailabClient; +import feign.RequestInterceptor; +import feign.RequestTemplate; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.HttpHeaders; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; + +@Configuration +public class FeignTokenInterceptor implements RequestInterceptor { + + @Override + public void apply(RequestTemplate requestTemplate) { + // 从当前请求上下文中获取Token + String token = getTokenFromCurrentRequest(); + + if (token == null) { + // 如果没有获取到Token,从system-server中获取token + token = IailabClient.getToken(); + } + + if (token!= null) { + requestTemplate.header(HttpHeaders.AUTHORIZATION, token); + } + + Long tenantId = IailabClient.getTenantId(); + if (tenantId != null) { + requestTemplate.header("tenant-id", String.valueOf(tenantId)); + } + } + + private String getTokenFromCurrentRequest() { + ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); + if (attributes!= null) { + return attributes.getRequest().getHeader(HttpHeaders.AUTHORIZATION); + } + return null; + } +} \ No newline at end of file -- Gitblit v1.9.3