当前位置:网站首页>One of token passing between microservices @feign's token passing
One of token passing between microservices @feign's token passing
2022-06-26 04:53:00 【RabbitMq_ mr wang】
Method 1 ,@RequestHeader
Add... To the microservice method header of the request caller @RequestHeader Used to receive incoming requests from the client token, Key code snippets
@GetMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
public ShareDTO findById(HttpServletRequest request,
@PathVariable Integer id,
@RequestParam(name = "foo") String foo,
@RequestHeader("X-Token") String token) {
System.out.println(foo);
return this.shareService.findById(id, token);
}
Here we get header Medium “X-Token”
When using Feign Obtained when calling other microservices Token It is passed into the request header of the next microservice
@GetMapping("/users/{id}")
UserDTO findById(@PathVariable Integer id, @RequestHeader("X-Token") String token);
there @RequestHeader It means that the parameter token Put in the request header of the next request header in . Here we are , Use this way to token Transfer can be realized .
Method 2 Use Feign Of Interceptor
Step1 Realization RequestInterceptor Interface
apply-> Get X-Token
Step2 take token Pass on to the next request
Achieve one RequestInterceptor
The implementation is as follows
// Omit package name
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.apache.commons.lang3.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;
/**
* @author frend
* @version v0.0.1
* @apiNote feign Pass on token Intercept for unified processing
* @time 2020/10/26 12:48
* @parjectName rate-content-center
*/
public class TokenTelayRequestIntecepor implements RequestInterceptor {
@Override
public void apply(RequestTemplate requestTemplate) {
// from header obtain X-token
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
ServletRequestAttributes srat = (ServletRequestAttributes) requestAttributes;
HttpServletRequest request = srat.getRequest();
String token = request.getHeader("X-Token");
if (StringUtils.isNotBlank(token)) {
// take token Pass out
requestTemplate.header("X-Token", token);
}
}
}
Get it here first HTTPServletRequest
Then from request Get to the header Of “X-Token”
Put this token Pass to requestTemplate
Interceptor After the implementation, you need to do some work on this Interceptor Set configuration
Step3 To configure Feign
Configure a global requestInterceptors: Implement the above RequestInterceptor The full path of the class to this configuration
feign:
client:
config:
default:
loggerLevel: full
requestInterceptors:
- org.rate.contentcenter.feignclients.interceptor.TokenTelayRequestIntecepor
Here we are , Overall FeignToken The delivery is complete .
Finally, make a conclusion :
1. Use @RequestHeader The way is more intuitive , It is also easier to understand , But when the interface is modified , Or when there are too many service interfaces , The task is huge
2. Use RequestInterceptor Interceptor , There are some abstractions in understanding , However, this method has great advantages in actual operation and code coupling .
边栏推荐
- torchvision_ Transform (image enhancement)
- numpy 随机数
- 为什么许多shopify独立站卖家都在用聊天机器人?一分钟读懂行业秘密!
- Dameng database backup and restore
- PSIM software learning ---08 call of C program block
- Essential foundation of programming - Summary of written interview examination sites - computer network (1) overview
- Multipass Chinese documents - improve mount performance
- Thymeleaf data echo, single selection backfill, drop-down backfill, time frame backfill
- Zhongshanshan: engineers after being blasted will take off | ONEFLOW u
- 文件上传与安全狗
猜你喜欢

PSIM software learning ---08 call of C program block

1.11 learning summary

Why do many Shopify independent station sellers use chat robots? Read industry secrets in one minute!

图解OneFlow的学习率调整策略

Zhongshanshan: engineers after being blasted will take off | ONEFLOW u

ROS 笔记(07)— 客户端 Client 和服务端 Server 的实现

天才制造者:獨行俠、科技巨頭和AI|深度學習崛起十年
![[H5 development] 01 take you to experience H5 development from a simple page ~ the whole page implementation process from static page to interface adjustment manual teaching](/img/e4/27611abdd000019e70f4447265808c.jpg)
[H5 development] 01 take you to experience H5 development from a simple page ~ the whole page implementation process from static page to interface adjustment manual teaching

5. <tag-栈和常规问题>补充: lt.946. 验证栈序列(同剑指 Offer 31. 栈的压入、弹出序列)

08_ Spingboot integrated redis
随机推荐
Use fill and fill in Matplotlib_ Between fill the blank area between functions
Sklearn Library -- linear regression model
Selection of programming language
2.22.2.14
2022.2.13
Thinkphp6 parsing QR code
微信小程序保存图片的方法
Dbeaver installation and configuration of offline driver
Multipass Chinese document - share data with instances
Multipass中文文档-使用实例命令别名
2022.1.24
条件查询
Why do many Shopify independent station sellers use chat robots? Read industry secrets in one minute!
Interpretation of yolov5 training results
Astype conversion data type
Multipass Chinese document - use packer to package multipass image
做软件测试学历重要还是能力重要
MySql如何删除所有多余的重复数据
How to use the configured slave data source for the scheduled task configuration class scheduleconfig
1.18 learning summary