From ca5436abd755ad04735810d9e146c7dcd1158663 Mon Sep 17 00:00:00 2001
From: liriming <1343021927@qq.com>
Date: 星期二, 25 三月 2025 11:05:32 +0800
Subject: [PATCH] 认证

---
 ansteel-biz/src/main/java/com/iailab/module/ansteel/config/FeignTokenInterceptor.java |   43 +++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 43 insertions(+), 0 deletions(-)

diff --git a/ansteel-biz/src/main/java/com/iailab/module/ansteel/config/FeignTokenInterceptor.java b/ansteel-biz/src/main/java/com/iailab/module/ansteel/config/FeignTokenInterceptor.java
new file mode 100644
index 0000000..2e45c51
--- /dev/null
+++ b/ansteel-biz/src/main/java/com/iailab/module/ansteel/config/FeignTokenInterceptor.java
@@ -0,0 +1,43 @@
+package com.iailab.module.ansteel.config;
+
+import com.iailab.module.ansteel.util.token.IailabClient;
+import feign.RequestInterceptor;
+import feign.RequestTemplate;
+import org.springframework.beans.factory.annotation.Autowired;
+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 {
+
+    @Autowired
+    private IailabClient iailabClient;
+
+    @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