|
@@ -1,8 +1,12 @@
|
|
|
package com.ruoyi.framework.config;
|
|
|
|
|
|
+import com.alibaba.nacos.client.naming.utils.SignUtil;
|
|
|
+import com.ruoyi.framework.config.properties.TokenProperties;
|
|
|
import feign.*;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import okhttp3.ConnectionPool;
|
|
|
import okhttp3.OkHttpClient;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
|
|
import org.springframework.cloud.openfeign.EnableFeignClients;
|
|
@@ -10,7 +14,13 @@ import org.springframework.cloud.openfeign.FeignAutoConfiguration;
|
|
|
import org.springframework.cloud.openfeign.support.SpringMvcContract;
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
+import org.springframework.web.context.request.RequestContextHolder;
|
|
|
+import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.SortedMap;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
/**
|
|
@@ -22,8 +32,29 @@ import java.util.concurrent.TimeUnit;
|
|
|
@Configuration
|
|
|
@ConditionalOnClass(Feign.class)
|
|
|
@AutoConfigureBefore(FeignAutoConfiguration.class)
|
|
|
+@Slf4j
|
|
|
public class FeignConfig {
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private TokenProperties tokenProperties;
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public RequestInterceptor requestInterceptor() {
|
|
|
+ return requestTemplate -> {
|
|
|
+ ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
|
|
+ if (null != attributes) {
|
|
|
+ HttpServletRequest request = attributes.getRequest();
|
|
|
+ log.info("Feign request: {}", request.getRequestURI());
|
|
|
+ // 将token信息放入header中
|
|
|
+ String token = request.getHeader(tokenProperties.getHeader());
|
|
|
+ if(token==null || "".equals(token)){
|
|
|
+ token = request.getParameter("token");
|
|
|
+ }
|
|
|
+ requestTemplate.header(tokenProperties.getHeader(), token);
|
|
|
+ }
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
@Bean
|
|
|
public OkHttpClient okHttpClient(){
|
|
|
return new OkHttpClient.Builder()
|