本文主要是介绍解决restTemplate之间调用,token只传递一次问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在每个微服务下添加一下代码:
package com.rocwo.rwshop.config;import com.rocwo.rwshop.commons.authentication.properties.SecurityProperties;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.Base64;
import java.util.Enumeration;/*** 描述:* author: chenjian* date: 2020-07-21 13:33*/
@Log4j2
@Configuration
public class FeignConfiguration implements RequestInterceptor {@Autowiredprivate SecurityProperties securityProperties;@Overridepublic void apply(RequestTemplate template) {ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();if(null==attributes){return;}HttpServletRequest request = attributes.getRequest();Enumeration<String> headerNames = request.getHeaderNames();if (headerNames != null) {while (headerNames.hasMoreElements()) {String name = headerNames.nextElement();String values = request.getHeader(name);// 跳过 content-lengthif (name.equals("content-length")){continue;}template.header(name, values);}}}}
这篇关于解决restTemplate之间调用,token只传递一次问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!