当前位置:网站首页>RestTemplate、Feign实现Token传递
RestTemplate、Feign实现Token传递
2022-07-06 06:08:00 【雪峰.贵】
文章目录
Fegin 方式
客户端与服务端
一、加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
二、定义注解
public @interface CheckLogin {
}
三、定义切面
import com.itmuch.contentcenter.util.JwtOperator;
import io.jsonwebtoken.Claims;
import lombok.RequiredArgsConstructor;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
@Aspect //切面注解
@Component
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class CheckLoginAspect {
private final JwtOperator jwtOperator;
//AOP的Advice(before,after returning,after throwing,around)的其中一个
@Around("@annotation(com.itmuch.contentcenter.auth.CheckLogin)")//加了@CheckLogin的方法都会走到这里
public Object checkLogin(ProceedingJoinPoint point) throws Throwable {
checkToken();
return point.proceed();
}
private void checkToken() {
try {
// 1. 从header里面获取token
HttpServletRequest request = getHttpServletRequest();
String token = request.getHeader("X-Token");
// 2. 校验token是否合法&是否过期;如果不合法或已过期直接抛异常;如果合法放行
Boolean isValid = jwtOperator.validateToken(token);
if (!isValid) {
throw new SecurityException("Token不合法!");
}
// 3. 如果校验成功,那么就将用户的信息设置到request的attribute里面
Claims claims = jwtOperator.getClaimsFromToken(token);
request.setAttribute("id", claims.get("id"));
request.setAttribute("wxNickname", claims.get("wxNickname"));
request.setAttribute("role", claims.get("role"));
} catch (Throwable throwable) {
throw new SecurityException("Token不合法");
}
}
private HttpServletRequest getHttpServletRequest() {
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
ServletRequestAttributes attributes = (ServletRequestAttributes) requestAttributes;
return attributes.getRequest();
}
}
四、客户端创建Fegin的拦截器
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
public class TokenRelayRequestIntecepor implements RequestInterceptor {
@Override
public void apply(RequestTemplate requestTemplate) {
// 1. 从header里面获取token
HttpServletRequest request = getHttpServletRequest();
String token = request.getHeader("X-Token");
if(!StringUtils.isEmpty(token)){
requestTemplate.header("X-Token",token);
}
}
private HttpServletRequest getHttpServletRequest() {
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
ServletRequestAttributes attributes = (ServletRequestAttributes) requestAttributes;
return attributes.getRequest();
}
}
四、客户端加配置
feign:
client:
config:
default: #全局
loggerLevel: full
requestInterceptors:
- com.itmuch.contentcenter.feignclient.interceptor.TokenRelayRequestIntecepor
五、测试
传入客户端token,token会通过Fegin的拦截器把token加上。
所有Fegin的接口都会带上token
RestTemplate方式
1.创建RestTemplate的拦截器
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
public class RestTemplateRequestInterceptor implements ClientHttpRequestInterceptor {
@Override
public ClientHttpResponse intercept(HttpRequest httpRequest, byte[] body, ClientHttpRequestExecution clientHttpRequestExecution) throws IOException {
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
ServletRequestAttributes attributes = (ServletRequestAttributes) requestAttributes;
HttpServletRequest request = attributes.getRequest();
String token = request.getHeader("X-Token");
HttpHeaders headers = httpRequest.getHeaders();
headers.add("X-Token", token);
// 保证请求继续执行
return clientHttpRequestExecution.execute(httpRequest, body);
}
}
2.RestTemplate实例
@Bean
@LoadBalanced
@SentinelRestTemplate(
blockHandler = "handleBlock",
fallback = "handleFallback",
fallbackClass = SentinelBlockHandler.class,
blockHandlerClass = SentinelBlockHandler.class)
public RestTemplate restTemplate(){
//把配置的拦截器加到实例里
RestTemplate restTemplate = new RestTemplate();
restTemplate.setInterceptors(
Collections.singletonList(new RestTemplateRequestInterceptor())
);
return restTemplate;
}
边栏推荐
- 【Postman】Monitors 监测API可定时周期运行
- Expose the serial fraudster Liu Qing in the currency circle, and default hundreds of millions of Cheng Laolai
- PAT(乙级)2022年夏季考试
- P问题、NP问题、NPC问题、NP-hard问题详解
- 【无App Push 通用测试方案
- JMeter做接口测试,如何提取登录Cookie
- Function of contenttype
- nodejs实现微博第三方登录
- Wib3.0 leapfrogging, in leapfrogging (ง • ̀_•́) ง
- 假设检验学习笔记
猜你喜欢
(5) Explanation of yolo-v3 core source code (3)
isam2运行流程
J'ai un chaton.
[API interface tool] Introduction to postman interface
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
功能安全之故障(fault),错误(error),失效(failure)
selenium源码通读·9 |DesiredCapabilities类分析
【Postman】Monitors 监测API可定时周期运行
[Thesis code] SML part code reading
JMeter做接口测试,如何提取登录Cookie
随机推荐
Overview of three core areas of Mathematics: algebra
浅谈专项测试之弱网络测试
Expose the serial fraudster Liu Qing in the currency circle, and default hundreds of millions of Cheng Laolai
[leetcode] day96 - the first unique character & ransom letter & letter ectopic word
【Tera Term】黑猫带你学TTL脚本——嵌入式开发中串口自动化神技能
(5) Explanation of yolo-v3 core source code (3)
Function of contenttype
单元测试的意义
How to use the container reflection method encapsulated by thinkphp5.1 in business code
Eigen sparse matrix operation
对数据安全的思考(转载)
Company video accelerated playback
Fault, error, failure of functional safety
MySQL之基础知识
properties文件
IDEA 新UI使用
【无App Push 通用测试方案
LeetCode 1200. 最小绝对差
Wib3.0 leapfrogging, in leapfrogging (ง • ̀_•́) ง
[postman] collections configuration running process