初始代码
This commit is contained in:
26
yunzhupaas-cloud-common/yunzhupaas-common-feign/pom.xml
Normal file
26
yunzhupaas-cloud-common/yunzhupaas-common-feign/pom.xml
Normal file
@@ -0,0 +1,26 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>yunzhupaas-cloud-common</artifactId>
|
||||
<groupId>com.yunzhupaas</groupId>
|
||||
<version>5.2.0-RELEASE</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>yunzhupaas-common-feign</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.yunzhupaas</groupId>
|
||||
<artifactId>yunzhupaas-common-auth</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,85 @@
|
||||
package com.yunzhupaas;
|
||||
|
||||
import feign.Logger;
|
||||
import feign.RequestInterceptor;
|
||||
import feign.RequestTemplate;
|
||||
import com.yunzhupaas.base.UserInfo;
|
||||
import com.yunzhupaas.constant.GlobalConst;
|
||||
import com.yunzhupaas.consts.AuthConsts;
|
||||
import com.yunzhupaas.properties.SecurityProperties;
|
||||
import com.yunzhupaas.util.Constants;
|
||||
import com.yunzhupaas.util.ServletUtil;
|
||||
import com.yunzhupaas.util.UserProvider;
|
||||
import com.yunzhupaas.utils.FeignHolder;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author 云筑产品开发平台组
|
||||
* @version V3.1.0
|
||||
* @copyright 深圳市乐程软件有限公司
|
||||
* @date 2024/3/16 10:51
|
||||
*/
|
||||
@Configuration
|
||||
public class FeignConfig implements RequestInterceptor {
|
||||
|
||||
@Autowired
|
||||
private SecurityProperties securityProperties;
|
||||
|
||||
@Override
|
||||
public void apply(RequestTemplate template) {
|
||||
// 添加token
|
||||
Map<String, String> headers = FeignHolder.get();
|
||||
String token = null;
|
||||
if (headers != null) {
|
||||
// 通过FeignHolder调用
|
||||
template.header("User-Agent", headers.get("user-agent"));
|
||||
template.header(Constants.AUTHORIZATION, headers.get(Constants.AUTHORIZATION));
|
||||
template.header("X-Real-IP", headers.get("x-real-ip"));
|
||||
template.header("X-Forwarded-For", headers.get("x-forwarded-for"));
|
||||
/*
|
||||
* headers.entrySet().forEach((k)->{
|
||||
* template.header(k.getKey(), k.getValue());
|
||||
* });
|
||||
*/
|
||||
} else {
|
||||
// 先获取当前本地缓存中UserInfo的TOKEN
|
||||
// 适配临时切换用户
|
||||
UserInfo userInfo = UserProvider.getLocalLoginUser();
|
||||
if (userInfo != null && userInfo.getToken() != null) {
|
||||
token = userInfo.getToken();
|
||||
template.header(Constants.AUTHORIZATION, token);
|
||||
}
|
||||
// Web环境直接调用
|
||||
HttpServletRequest request = ServletUtil.getRequest();
|
||||
if (request != null) {
|
||||
if (token == null) {
|
||||
template.header(Constants.AUTHORIZATION, request.getHeader(Constants.AUTHORIZATION));
|
||||
}
|
||||
template.header("User-Agent", request.getHeader("User-Agent"));
|
||||
template.header("X-Real-IP", request.getHeader("X-Real-IP"));
|
||||
template.header("X-Forwarded-For", request.getHeader("X-Forwarded-For"));
|
||||
}
|
||||
}
|
||||
if (securityProperties.isEnableInnerAuth() || securityProperties.isEnablePreAuth()) {
|
||||
template.header(AuthConsts.INNER_TOKEN_KEY, UserProvider.getInnerAuthToken());
|
||||
}
|
||||
template.header(GlobalConst.HEADER_HOST, ServletUtil.getRequestHost());
|
||||
}
|
||||
|
||||
/**
|
||||
* Openfeign调用日志
|
||||
* NONE,BASIC,HEADERS,FULL共有四种等级
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Bean
|
||||
Logger.Level feignLoggerLeave() {
|
||||
return Logger.Level.FULL;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package com.yunzhupaas.utils;
|
||||
|
||||
import com.yunzhupaas.util.ThreadPoolExecutorUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* 异步FEIGN请求, 把当前REQUEST的HEADER存入新线程中, 在新线程中调用feign请求, FeignConfig
|
||||
* 中取当前headers发送到新服务
|
||||
* 使用方式:
|
||||
* 1、直接调用set设置Header, Feign请求全部调用完之后调用clear清除线程变量
|
||||
* 2、调用asyncFeign、sendFeign 调用完后自动清除线程变量
|
||||
*/
|
||||
// @ConditionalOnBean(ThreadPoolTaskExecutor.class)
|
||||
@Component
|
||||
@Slf4j
|
||||
public class FeignHolder {
|
||||
private static ThreadLocal<Map<String, String>> feignHeader = new ThreadLocal<>();
|
||||
|
||||
public static void set(Map<String, String> headers) {
|
||||
TreeMap map = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
|
||||
map.putAll(headers);
|
||||
feignHeader.set(map);
|
||||
}
|
||||
|
||||
public static Map<String, String> get() {
|
||||
return feignHeader.get();
|
||||
}
|
||||
|
||||
public static void clear() {
|
||||
feignHeader.remove();
|
||||
}
|
||||
|
||||
/**
|
||||
* Async异步发送请求, 完成后清空线程变量
|
||||
*
|
||||
* @param headers
|
||||
* @param supplier
|
||||
* @return
|
||||
*/
|
||||
public static Future<?> asyncFeign(Map<String, String> headers, Supplier supplier) {
|
||||
return ThreadPoolExecutorUtil.getExecutor().submit(() -> sendFeign(headers, supplier));
|
||||
}
|
||||
|
||||
/**
|
||||
* 同步发送请求, 完成后清空线程变量
|
||||
*
|
||||
* @param headers
|
||||
* @param supplier
|
||||
*/
|
||||
public static <R> R sendFeign(Map<String, String> headers, Supplier<R> supplier) {
|
||||
try {
|
||||
set(headers);
|
||||
return supplier.get();
|
||||
} catch (Exception e) {
|
||||
log.error("同步发送Feign请求失败", e);
|
||||
return null;
|
||||
} finally {
|
||||
clear();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.yunzhupaas.utils;
|
||||
|
||||
import com.yunzhupaas.constant.ModuleName;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @author 云筑产品开发平台组
|
||||
* @version V3.1.0
|
||||
* @copyright 深圳市乐程软件有限公司
|
||||
* @date 2024/3/16 10:51
|
||||
*/
|
||||
@Data
|
||||
public class FeignName {
|
||||
public static final String SYSTEM_SERVER_NAME = ModuleName.SYSTEM_SERVER_NAME;
|
||||
|
||||
public static final String MESSAGE_SERVER_NAME = ModuleName.MESSAGE_SERVER_NAME;
|
||||
|
||||
public static final String VUSUALDEV_SERVER_NAME = ModuleName.VUSUALDEV_SERVER_NAME;
|
||||
|
||||
public static final String EXAMPLE_SERVER_NAME = ModuleName.EXAMPLE_SERVER_NAME;
|
||||
|
||||
public static final String EXTEND_SERVER_NAME = ModuleName.EXTEND_SERVER_NAME;
|
||||
|
||||
public static final String APP_SERVER_NAME = ModuleName.APP_SERVER_NAME;
|
||||
|
||||
public static final String WORKFLOW_SERVER_NAME = ModuleName.WORKFLOW_SERVER_NAME;
|
||||
|
||||
public static final String FILE_SERVER_NAME = ModuleName.FILE_SERVER_NAME;
|
||||
|
||||
public static final String PERMISSION_SERVER_NAME = ModuleName.PERMISSION_SERVER_NAME;
|
||||
|
||||
public static final String FORM_SERVER_NAME = ModuleName.FORM_SERVER_NAME;
|
||||
|
||||
public static final String OAUTH_SERVER_NAME = ModuleName.OAUTH_SERVER_NAME;
|
||||
|
||||
public static final String SCHEDULE_SERVER_NAME = ModuleName.SCHEDULE_SERVER_NAME;
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user