feign服务之间调用如何带token

feign服务之间调用如何带token


spring cloud 服务之间调用如果需要携带token信息,需要重新添加token

import javax.servlet.http.HttpServletRequest;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import feign.RequestInterceptor;
import feign.RequestTemplate;

/**
 * Feign调用添加token
 */
@Configuration
public class FeignRequestConfig implements RequestInterceptor {

    @Override
    public void apply(RequestTemplate template) {
        ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        // url添加token
        template.append("?access_token=" + request.getParameter("access_token"));
        // header添加token
        template.header("Access-Token", request.getHeader("Access-Token"));
    }
}

你可能感兴趣的:(spring,cloud,feign,spring)