irpas技术客

feign.feignException$Unauthorized:[401] during [GET] to xxx..._房东啊

网络投稿 592

feign.feignException$Unauthorized:[401] during [GET] to xxx…:

在SpringCloud的模块调用中,新增的模块实现其功能时调用其他模块的相关方法。在进行接口调用测试时,服务端会报出一个错误,Feign远程调用时没有权限;

问题分析

在调用方法的时候, 请求头中的 Authorization 是传递了的, 为什么Feign远程调用时, 报出没有权限这样的错误呢 ?

原因

Feign在进行远程调用时, 默认是没有将请求头继续往下传递的, 而系统微服务接入认证之后, 必须携带JWT令牌才可以访问, 没有携带令牌就访问系统服务, 就会出现401 , 未认证错误。

解决办法

使用Feign的拦截器来解决, 拦截所有的feign的远程调用, 在进行远程调用时, 拦截住请求, 并让请求头Authorization 继续往下传递。

@Component public class FeignInterceptor implements RequestInterceptor { @Override public void apply(RequestTemplate template) { RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); if(requestAttributes != null){ HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest(); String authorization = request.getHeader("Authorization"); template.header("Authorization", authorization); } } }


1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,会注明原创字样,如未注明都非原创,如有侵权请联系删除!;3.作者投稿可能会经我们编辑修改或补充;4.本站不提供任何储存功能只提供收集或者投稿人的网盘链接。

标签: #during #get #To #xxx