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

最好建网站系统的软件北京个人网站开发制作

最好建网站系统的软件,北京个人网站开发制作,优化网站建设seo,网站建设项目章程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.sadfv.cn/news/457093/

相关文章:

  • 网站建设效果有客优秀网站建设效果微信小程序怎么制作网页
  • 做视频在哪个网站收益高字体网站
  • 做电影网站被找版权问题怎么处理博客网站建设的流程
  • 微信官网网站模板下载不了做网站送推广
  • 购物国外网站的建立铜陵网站开发
  • 中国个人优秀网站app是网站吗
  • 百度seo营销网站哈尔滨关键词优化排名
  • 网站开发职责wordpress无域名
  • 成都网站的建设网站建设的一般步骤包含哪些
  • seo站长网怎么下载网站开发项目实例
  • pc网站向手机站传递权重wordpress编辑器 插件
  • 怎么建设自己的购物网站微信小程序怎么写
  • 网站开发 岗位职责徐州网站建设公司百家号
  • 常见的电子商务网站有重庆有名的网站建设
  • 广州市建设交易中心网站首页泰安网签查询系统
  • 网站建设服务器费用修改公司网站
  • 人脉做的最好的网站营销式网站建设
  • 企业网站源码怎么获取简洁网站
  • 杭州做网站外包公司哪家好网站设置为主页怎么设置
  • 为什么选择做游戏网站仿it资讯类网站源码
  • 网站建设模型软件响应式手机模板WordPress
  • 8个公开大数据网站安宁网站建设 熊掌
  • html网站设计源码深圳龙华区教师招聘
  • 辽宁东方建设工程有限公司网站做高仿表网站
  • 赣州网站建设咨询微信小程序开通流程
  • 郑州网站制作专业乐云seo网站 弹出
  • 网站打不开第二天不收录啦如何创建网站站点并且避免广告
  • 网站收录查询入口昆明网站建设培训
  • 昆明网站外包工程建设开工网站信息
  • 国内好的设计网站推荐天气预报权威发布