1, the first look at what is Feign.
The entire "desert Zhiqiu" Bowen https://blog.csdn.net/wo18237095579/article/details/83343915
2, if the interfaces to other services without making rights processing, Bowen described above with reference to the first point.
3, how to access services if other interfaces done a deal with permissions (such as OAuth 2)?
a, have permission to do a direct call processing service interface will cause unauthorized http 401 error occurs when you call, and then lead to final service http 500 internal server error
b, solution: The most convenient is to request header Riga the token, along with the past;
b1, Feign there is provided an interface, RequestInterceptor;
as long as the realization of this interface, easy to do some processing, for example, we verify the token request header is called Access-Token
, we will first remove the token of the current request, then put his head feign request;
b2, a new configuration class
-
-
public class FeignConfig implements RequestInterceptor {
-
-
public void apply(RequestTemplate requestTemplate) {
-
ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
-
HttpServletRequest request = attributes.getRequest();
-
requestTemplate.header(HttpHeaders.AUTHORIZATION, request.getHeader(HttpHeaders.AUTHORIZATION));
-
}
-
}
b3, the interface was added @FeignClient configuration = {FeignConfig.class}
@FeignClient(value="被调用的服务名",configuration={FeignConfig.class})
You can do for permission to call handling service interface
Here is a quote on "Da Mo Zhiqiu" https://blog.csdn.net/wo18237095579/article/details/83343915