解决feign调用远程token问题
This commit is contained in:
parent
a110127415
commit
6987b5015e
@ -1,6 +1,7 @@
|
|||||||
package net.jieyuu.config;
|
package net.jieyuu.config;
|
||||||
|
|
||||||
|
|
||||||
|
import feign.RequestInterceptor;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import org.redisson.Redisson;
|
import org.redisson.Redisson;
|
||||||
import org.redisson.api.RedissonClient;
|
import org.redisson.api.RedissonClient;
|
||||||
@ -12,10 +13,14 @@ import org.springframework.data.redis.connection.RedisConnectionFactory;
|
|||||||
import org.springframework.data.redis.core.RedisTemplate;
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
import org.springframework.data.redis.serializer.RedisSerializer;
|
import org.springframework.data.redis.serializer.RedisSerializer;
|
||||||
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||||
|
import org.springframework.web.context.request.RequestContextHolder;
|
||||||
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@Data
|
@Data
|
||||||
public class AppConfig {
|
public class AppConfig {
|
||||||
|
|
||||||
@Value("${spring.redis.host}")
|
@Value("${spring.redis.host}")
|
||||||
private String redisHost;
|
private String redisHost;
|
||||||
@ -68,4 +73,30 @@ public class AppConfig {
|
|||||||
|
|
||||||
return redisTemplate;
|
return redisTemplate;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* feign 调用token丢失解决
|
||||||
|
* 新增拦截器
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Bean
|
||||||
|
public RequestInterceptor requestInterceptor() {
|
||||||
|
|
||||||
|
return template -> {
|
||||||
|
// 获取当前请求的属性信息
|
||||||
|
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
||||||
|
|
||||||
|
if (attributes != null) {
|
||||||
|
// 如果请求不为空,则记录请求头信息,并将请求头中的“token”值设置到新的请求模板的头部
|
||||||
|
HttpServletRequest request = attributes.getRequest();
|
||||||
|
if (null == request) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
String token = request.getHeader("token");
|
||||||
|
// 获取token并且设置到新的请求模板的头部
|
||||||
|
template.header("token", token);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
package net.jieyuu.config;
|
package net.jieyuu.config;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import net.jieyuu.interceptor.LoginInterceptor;
|
import net.jieyuu.interceptor.LoginInterceptor;
|
||||||
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||||
|
@ -3,7 +3,6 @@ package net.jieyuu.config;
|
|||||||
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import net.jieyuu.interceptor.LoginInterceptor;
|
import net.jieyuu.interceptor.LoginInterceptor;
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||||
|
Loading…
Reference in New Issue
Block a user