spring cloud service between the various micro how to call each other (Feign, Feign with token access service interface)

 

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

 

  1. @Configuration
  2. public class FeignConfig implements RequestInterceptor {
  3. @Override
  4. public void apply(RequestTemplate requestTemplate) {
  5. ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
  6. HttpServletRequest request = attributes.getRequest();
  7. requestTemplate.header(HttpHeaders.AUTHORIZATION, request.getHeader(HttpHeaders.AUTHORIZATION));
  8. }
  9. }

 

b3, the interface was added @FeignClient configuration = {FeignConfig.class}

 

@FeignClientvalue="被调用的服务名",configuration={FeignConfig.class})

 

You can do for permission to call handling service interface

1, the first look at what is Feign. The entire "desert Zhiqiu" Bowen https://blog.csdn.net/wo18237095579/article/details/83343915
[1 Shǒuxiān Xiang Kan shénme Chi Feige. Zhèlǐ yǐnyòng "Damon Zhi Qiu" de https://Blog.Csdn.Net/wo18237095579/article/details/83343915 Bowen]
1. First look at what is Feign.

Here is a quote on "Da Mo Zhiqiu" https://blog.csdn.net/wo18237095579/article/details/83343915

Guess you like

Origin www.cnblogs.com/lykbk/p/fddfdfgdgfdgfg656756565.html
Recommended