IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 网络协议 -> Feign调用服务 token透传 -> 正文阅读

[网络协议]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<String> 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<Object,Object> param){
            String hytoken = request.getHeader("hytoken");
            HttpHeaders header = new HttpHeaders();
            if (hytoken != null && !"".equals(hytoken)) {
                header.put("hytoken", Collections.singletonList(hytoken));
            } else {
                List<String> 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);
  网络协议 最新文章
使用Easyswoole 搭建简单的Websoket服务
常见的数据通信方式有哪些?
Openssl 1024bit RSA算法---公私钥获取和处
HTTPS协议的密钥交换流程
《小白WEB安全入门》03. 漏洞篇
HttpRunner4.x 安装与使用
2021-07-04
手写RPC学习笔记
K8S高可用版本部署
mySQL计算IP地址范围
上一篇文章      下一篇文章      查看所有文章
加:2021-12-10 11:24:18  更:2021-12-10 11:25:17 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年10日历 -2024/10/5 6:54:14-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码