当前位置: 首页 > news >正文

宁夏微信服务网站企业英文网站制作

宁夏微信服务网站,企业英文网站制作,html网页制作用什么软件,深圳网站设计 工作室SpringCloudGateway网关处拦截并修改请求 需求背景 老系统没有引入Token的概念#xff0c;之前的租户Id拼接在请求上#xff0c;有的是以Get#xff0c;Param传参形式#xff1b;有的是以Post#xff0c;Body传参的。需要在网关层拦截请求并进行请求修改后转发到对应服务。…SpringCloudGateway网关处拦截并修改请求 需求背景 老系统没有引入Token的概念之前的租户Id拼接在请求上有的是以GetParam传参形式有的是以PostBody传参的。需要在网关层拦截请求并进行请求修改后转发到对应服务。 举个例子 Get请求 /user/getInfo?userId1 经过网关处理后变为 /user/getInfo?userId1tenantId2333 Post请求: /user/getInfo Body携带参数为 {userId: 1 }经过网关处理后变为 {userId: 1,tenantId: 2333 }解决办法 全局过滤器配置 通过Bean注解配置一个全局过滤器用于在请求被转发到微服务前进行处理。处理GET请求 如果是GET请求直接修改URL并返回不对请求体进行修改。处理非GET请求 对非GET请求使用装饰者模式创建ModifyRequestBodyServerHttpRequestDecorator对象对请求体进行修改。去掉Content-Length头 在修改请求体的同时通过mutate()方法去掉请求头中的Content-Length。修改请求体的装饰者类 定义了一个内部类ModifyRequestBodyServerHttpRequestDecorator继承自ServerHttpRequestDecorator用于实现请求体的修改。 代码示例 // 导入必要的类和包 package com.***.gateway.config;import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; import lombok.extern.slf4j.Slf4j; import org.jetbrains.annotations.NotNull; import org.springframework.cloud.gateway.filter.GlobalFilter; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.buffer.DataBuffer; import org.springframework.core.io.buffer.DataBufferFactory; import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.http.server.reactive.ServerHttpRequestDecorator; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.util.UriComponentsBuilder; import reactor.core.publisher.Flux;import java.io.IOException; import java.nio.charset.StandardCharsets;Configuration Slf4j public class GatewayConfig {// 配置全局过滤器Beanpublic GlobalFilter customGlobalFilter() {return (exchange, chain) - {// 获取原始请求对象ServerHttpRequest request exchange.getRequest();// 构建URI组件构建器用于修改请求URLUriComponentsBuilder uriBuilder UriComponentsBuilder.fromUri(request.getURI());// 初始化租户IDString tenantId ;// 检查请求头中是否包含 TenantId如果有则获取其值if (request.getHeaders().containsKey(TenantId)) {tenantId request.getHeaders().get(TenantId).get(0);uriBuilder.queryParam(tenantId, tenantId);}// 如果请求是GET请求则直接返回if (request.getMethodValue().equals(GET)) {log.info(请求是Get请求url is {}, uriBuilder.build().toUri());ServerHttpRequest modifiedRequest request.mutate().uri(uriBuilder.build().toUri()).build();// 创建新的ServerWebExchange该对象包含修改后的请求ServerWebExchange modifiedExchange exchange.mutate().request(modifiedRequest).build();// 继续执行过滤器链return chain.filter(modifiedExchange);}// 使用装饰者模式修改请求体ServerHttpRequest modifiedRequest new ModifyRequestBodyServerHttpRequestDecorator(request, tenantId, exchange.getResponse().bufferFactory());// 去掉Content-Length请求头modifiedRequest modifiedRequest.mutate().header(Content-Length, (String) null).build();// 创建新的ServerWebExchange该对象包含修改后的请求ServerWebExchange modifiedExchange exchange.mutate().request(modifiedRequest).build();// 继续执行过滤器链return chain.filter(modifiedExchange);};}// 定义修改请求体的装饰者类private static class ModifyRequestBodyServerHttpRequestDecorator extends ServerHttpRequestDecorator {private final String tenantId;private final DataBufferFactory bufferFactory;private final ObjectMapper objectMapper new ObjectMapper();// 构造方法传入原始请求、tenantId和数据缓冲工厂ModifyRequestBodyServerHttpRequestDecorator(ServerHttpRequest delegate, String tenantId, DataBufferFactory bufferFactory) {super(delegate);this.tenantId tenantId;this.bufferFactory bufferFactory;}// 重写获取请求体的方法对请求体进行修改NotNullOverridepublic FluxDataBuffer getBody() {return super.getBody().map(dataBuffer - {// 读取原始请求体数据byte[] bytes new byte[dataBuffer.readableByteCount()];dataBuffer.read(bytes);String body new String(bytes, StandardCharsets.UTF_8);// 修改请求体内容String newBody modifyJsonBody(body);// 创建新的 DataBufferbyte[] newData newBody.getBytes(StandardCharsets.UTF_8);return bufferFactory.wrap(newData);});}// 对 JSON 请求体进行修改添加 tenantId 字段private String modifyJsonBody(String originalBody) {try {JsonNode jsonNode objectMapper.readTree(originalBody);((ObjectNode) jsonNode).put(tenantId, tenantId);return objectMapper.writeValueAsString(jsonNode);} catch (IOException e) {log.error(Error modifying JSON body, e);return originalBody;}}} } 解决路径文章参考 http://t.csdnimg.cn/9kos5 http://t.csdnimg.cn/Aklwh 关于装饰者模式 装饰者模式是一种结构型设计模式它允许你通过将对象放入包含行为的特殊封装类中来为原始对象添加新的行为。这种模式能够在不修改原始对象的情况下动态地扩展其功能。在上段代码里主要使用装饰者模式去修改Body 的传参。 主要角色 Component组件 定义一个抽象接口或抽象类声明对象的一些基本操作。ConcreteComponent具体组件 实现了Component接口是被装饰的具体对象也是我们最终要添加新行为的对象。Decorator装饰者抽象类 继承了Component并持有一个Component对象的引用同时实现了Component定义的接口。它可以通过该引用调用Component的操作同时可以添加、扩展或修改Component的行为。ConcreteDecorator具体装饰者 扩展Decorator具体实现新行为的类。 装饰者模式的工作流程 客户端通过Component接口与ConcreteComponent对象进行交互。ConcreteComponent对象处理客户端的请求。客户端可以通过Decorator接口与ConcreteDecorator对象进行交互Decorator持有ConcreteComponent的引用。ConcreteDecorator在调用ConcreteComponent的操作前后可以添加、扩展或修改行为。 给普通咖啡加点糖和牛奶 代码示例 public class DecoratorPatternExample {// Component组件interface Coffee {String getDescription();double cost();}// ConcreteComponent具体组件static class SimpleCoffee implements Coffee {Overridepublic String getDescription() {return Simple Coffee;}Overridepublic double cost() {return 1.0;}}// Decorator装饰者抽象类abstract static class CoffeeDecorator implements Coffee {protected Coffee decoratedCoffee;public CoffeeDecorator(Coffee coffee) {this.decoratedCoffee coffee;}Overridepublic String getDescription() {return decoratedCoffee.getDescription();}Overridepublic double cost() {return decoratedCoffee.cost();}}// ConcreteDecorator具体装饰者static class MilkDecorator extends CoffeeDecorator {public MilkDecorator(Coffee coffee) {super(coffee);}Overridepublic String getDescription() {return super.getDescription() , with Milk;}Overridepublic double cost() {return super.cost() 0.5;}}// ConcreteDecorator具体装饰者static class SugarDecorator extends CoffeeDecorator {public SugarDecorator(Coffee coffee) {super(coffee);}Overridepublic String getDescription() {return super.getDescription() , with Sugar;}Overridepublic double cost() {return super.cost() 0.2;}}public static void main(String[] args) {// 创建一个简单的咖啡Coffee simpleCoffee new SimpleCoffee();System.out.println(Cost: simpleCoffee.cost() , Description: simpleCoffee.getDescription());// 使用装饰者模式添加牛奶和糖Coffee milkSugarCoffee new MilkDecorator(new SugarDecorator(simpleCoffee));System.out.println(Cost: milkSugarCoffee.cost() , Description: milkSugarCoffee.getDescription());} }
http://www.zqtcl.cn/news/203745/

相关文章:

  • 企业网站栏目规划的重要性wordpress改变为中文
  • 云服务器怎么上传网站个人建一个网站多少钱
  • 东莞网站建设包装制品flash网站制作
  • 办网站怎么赚钱做二手电脑的网站
  • 大型电子商务网站建设成本旅游网站前台怎么做
  • 深圳网站建设..网站点击图片放大
  • 上海企业扶持政策洛阳400电话洛阳网站seo
  • 保亭县住房城市建设局网站app免费制作平台下载
  • 抚州市建设局网站在网站做商城平台需要哪些资质
  • 潍坊专业网站建设多少钱素马设计官网
  • 深圳网站建设 套餐近期新闻事件
  • 网站开发外包维护合同淘宝客源码程序 爱淘宝风格+程序自动采集商品 淘宝客网站模板
  • 烟台企业网站开发军事新闻最新24小时
  • wordpress网站更换域名网站空间建站
  • 十堰网站建设公司电话网页设计与制作教程江西高校出版社
  • 英文网站seo常州建设局考试网站
  • wordpress 多网站哈尔滨 建网站
  • 免费网站源代码怎么制作网站教程
  • Thinkphp开发wordpress网站怎么优化seo
  • tp框架做视频网站站长统计芭乐鸭脖小猪
  • asp网站发布ftp国内f型网页布局的网站
  • 无限空间 网站四川省建设厅网站填报获奖
  • 广东佛山最新通知北京seo怎么优化
  • 浙江省通信管理局 网站备案 管理部门科技公司经营范围包括哪些
  • 网站域名备案转接入手续深圳外贸公司qc招聘
  • 湖北网站建设服务公司可以做产品推广的网站
  • 做经营性的网站备案条件wordpress删除菜单
  • js商城网站个安装wordpress
  • 想给学校社团做网站企业服务平台是做什么的
  • 网站推广渠道的类型wordpress看不到表格