Feign调用服务 token透传

Feign调用服务 token透传,第1张

Feign调用服务 token透传

1.yml配置

feign:
  hystrix:
    enabled: true # 开启熔断
  client:
    config:
      default:   #配置全局的feign的配置 如果有指定的服务配置 默认的配置不会生效
        connectTimeout: 60000 # 指定的是消费者连接服务提供者的连接超时时间,单位是毫秒
        readTimeout: 20000  # 指定的是调用服务提供者的服务的超时时间,单位是毫秒
        requestInterceptors: # 指定的是feign 自定义拦截器的相对路径
          - net.zlw.cloud.workflow.config.FeignConfigration
        loggerLevel: full # 配置日志等级
#hystrix 配置
hystrix:
  command:
    default:
      execution:
        timeout:
          #如果enabled设置为false,则请求超时交给ribbon控制
          enabled: true
        isolation:
          strategy: SEMAPHORE # 隔离策略
          thread:
            # 熔断器超时时间,默认:1000/毫秒
            timeoutInMilliseconds: 20000

-------------------------------------------------------------------------------------------------------------------------------

重点:strategy: SEMAPHORE # 隔离策略

2.实现  RequestInterceptor

import feign.RequestInterceptor;
import feign.RequestTemplate;
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.util.Enumeration;


public class FeignConfigration implements RequestInterceptor {


    @Override
    public void apply(RequestTemplate template) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        Enumeration headerNames = request.getHeaderNames();
        if (headerNames != null) {
            while (headerNames.hasMoreElements()) {
                String name = headerNames.nextElement();
                String values = request.getHeader(name);
                if (name.equals("hytoken")||name.equals("HYTOKEN")) {
                    template.header(name, values);
                    break;
                }
            }
        }

    }
}

3.FeignClient

@FeignClient(name = "em-api",configuration = FeignConfigration.class)
restTemplate调用 token 

1.工具类

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.util.MultiValueMap;

import javax.servlet.http.cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

    public class HttpEntityUtil {

        public static HttpEntity getEntity(HttpServletRequest request,MultiValueMap param){
            String hytoken = request.getHeader("hytoken");
            HttpHeaders header = new HttpHeaders();
            if (hytoken != null && !"".equals(hytoken)) {
                header.put("hytoken", Collections.singletonList(hytoken));
            } else {
                List cookies = new ArrayList<>();
                cookie[] cookie = request.getcookies();
                for (int i = 0; i < cookie.length; i++) {
                    if (cookie[i].getName().equals("SESSION")) {
                        cookies.add(cookie[i].getName() + "=" + cookie[i].getValue());
                        break;
                    }
                }
                header.put(HttpHeaders.cookie, cookies);
            }
            HttpEntity httpEntity = new HttpEntity(param, header);


            return httpEntity;
        }



    }

2.

HttpEntity httpEntity = HttpEntityUtil.getEntity(request,这里写请求参数);
String resultString = restTemplate.postForObject(url,httpEntity,String.class);

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5653104.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存