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

个性化推荐网站开发源码建材网站制作

个性化推荐网站开发源码,建材网站制作,惠州市seo网站设计,建成学校网站文章目录 前言一、常用网关过滤器1. 常用过滤器2. 示例3. Default Filters 二、定义接口服务1. 定义接口 三、自定义过滤器1. 过滤器类2. 应用配置 四、单元测试1. 正常2. 黑名单 总结 前言 上一章我们通过#xff0c;路由断言根据请求IP地址的黑名单功能#xff0c;作用范围… 文章目录 前言一、常用网关过滤器1. 常用过滤器2. 示例3. Default Filters 二、定义接口服务1. 定义接口 三、自定义过滤器1. 过滤器类2. 应用配置 四、单元测试1. 正常2. 黑名单 总结 前言 上一章我们通过路由断言根据请求IP地址的黑名单功能作用范围比较大。 这一章我们通过网关过滤器来实现特定请求url的黑名单功能作用范围进一步细化到接口。 一、常用网关过滤器 官方内置的网关过滤器太多了这里我只介绍常用的部分 网关过滤器又分为前置和后置比较典型的有AddRequestHeader(前)AddResponseHeader(后) 1. 常用过滤器 过滤器类型用法描述AddRequestHeader- AddRequestHeaderX-Request-red, blue将X-Request-red:blue请求头添加到所有匹配请求的下游请求头中。AddRequestHeadersIfNotPresent- AddRequestHeadersIfNotPresentX-Request-Color-1:blue,X-Request-Color-2:green在请求头不存在的情况下,为所有匹配请求的下游请求标头添加了2个标头X-Request-Color-1:bule和X-Request-Color-2:greenAddRequestParameter- AddRequestParameterred, blue将为所有匹配的请求在下游请求的查询参数中添加redblue。AddResponseHeader- AddResponseHeaderX-Response-Red, Blue这会将X-Response-Red:Blue添加到所有匹配请求的下游响应头中。RemoveRequestHeader- RemoveRequestHeaderX-Request-Foo这将在X-Request-Foo请求头头被发送到下游之前将其删除。RemoveRequestParameter- RemoveRequestParameterred这将在向下游发送请求之前删除红色查询参数。RemoveResponseHeader- RemoveResponseHeaderX-Response-Foo这将在响应返回给网关客户端之前从响应头中删除X-Response-Foo。PrefixPath- PrefixPath/mypath将为所有匹配请求添加前缀/mypath。因此对/hello的请求被发送到/mypath/hello。StripPrefix- StripPrefix2当通过网关向/name/blue/red发出请求时向nameservice发出的请求替换为nameservice/red。 2. 示例 RewritePath 对于/red/blue的请求路径这会在发出下游请求之前将路径设置为/blue。请注意由于YAML规范$应该替换为$\。 spring:cloud:gateway:routes:- id: rewritepath_routeuri: https://example.orgpredicates:- Path/red/**filters:- RewritePath/red/?(?segment.*), /$\{segment}ModifyRequestBody 修改请求正文 Bean public RouteLocator routes(RouteLocatorBuilder builder) {return builder.routes().route(rewrite_request_obj, r - r.host(*.rewriterequestobj.org).filters(f - f.prefixPath(/httpbin).modifyRequestBody(String.class, Hello.class, MediaType.APPLICATION_JSON_VALUE,(exchange, s) - Mono.just(new Hello(s.toUpperCase())))).uri(uri)).build(); }static class Hello {String message;public Hello() { }public Hello(String message) {this.message message;}public String getMessage() {return message;}public void setMessage(String message) {this.message message;} }ModifyResponseBody 修改响应正文 Bean public RouteLocator routes(RouteLocatorBuilder builder) {return builder.routes().route(rewrite_response_upper, r - r.host(*.rewriteresponseupper.org).filters(f - f.prefixPath(/httpbin).modifyResponseBody(String.class, String.class,(exchange, s) - Mono.just(s.toUpperCase()))).uri(uri)).build(); }3. Default Filters 要添加过滤器并将其应用于所有路由可以使用spring.cloud.gateway.default-filters。此属性接受过滤器列表。以下列表定义了一组默认过滤器 spring:cloud:gateway:default-filters:- AddResponseHeaderX-Response-Default-Red, Default-Blue- PrefixPath/httpbin更多网关过滤器请查看 二、定义接口服务 上一章我们使用的是提供者服务这里就使用消费者服务配置看起来会更加直观。 1. 定义接口 定义三个服务分别是 user-service/hellouser-service/hello1user-service/hello2 package org.example.nacos.consumer.controller;import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;/*** Create by zjg on 2024/7/21*/ RestController RequestMapping(/consumer/) public class HelloController {RequestMapping(hello)public String hello(){return hello consumer;}RequestMapping(hello1)public String hello1(){return hello consumer;}RequestMapping(hello2)public String hello2(){return hello consumer;} }三、自定义过滤器 1. 过滤器类 package org.example.gateway.filter;import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.validation.constraints.NotEmpty; import org.example.common.model.Result; import org.springframework.cloud.gateway.filter.GatewayFilter; import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory; import org.springframework.cloud.gateway.route.Route; import org.springframework.http.HttpStatus; import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.http.server.reactive.ServerHttpResponse; import org.springframework.stereotype.Component; import org.springframework.validation.annotation.Validated; import reactor.core.publisher.Flux; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Map; import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR; import static org.springframework.cloud.gateway.support.ShortcutConfigurable.ShortcutType.GATHER_LIST;/*** Create by zjg on 2024/7/24*/ Component public class BlackListGatewayFilterFactory extends AbstractGatewayFilterFactoryBlackListGatewayFilterFactory.Config {public BlackListGatewayFilterFactory() {super(Config.class);}Overridepublic GatewayFilter apply(Config config) {return (exchange, chain) - {Route route exchange.getAttribute(GATEWAY_ROUTE_ATTR);ServerHttpRequest request exchange.getRequest();ServerHttpResponse response exchange.getResponse();String path request.getURI().getPath();if (config.url.contains(path)) {response.setStatusCode(HttpStatus.NOT_ACCEPTABLE);Result result Result.error(HttpStatus.NOT_ACCEPTABLE.value(), 接口服务禁用, 该接口服务已加入黑名单,禁止访问!);ObjectMapper objectMapper new ObjectMapper();try {return response.writeWith(Flux.just(response.bufferFactory().wrap(objectMapper.writeValueAsBytes(result))));} catch (JsonProcessingException e) {throw new RuntimeException(e);}}return chain.filter(exchange);};}Overridepublic ShortcutType shortcutType() {return GATHER_LIST;}Overridepublic ListString shortcutFieldOrder() {return Collections.singletonList(url);}Validatedpublic static class Config {NotEmptyprivate ListString url new ArrayList();public ListString getUrl() {return url;}public void setUrl(ListString url) {this.url url;}} }2. 应用配置 spring:cloud:gateway:routes:- id: provider-serviceuri: lb://provider-servicepredicates:- Path/provider/**- BlackRemoteAddr192.168.1.1/24,127.0.0.1,192.168.0.102- id: consumer-serviceuri: lb://consumer-servicepredicates:- Path/consumer/**filters:- BlackList/consumer/hello1,/consumer/hello2四、单元测试 1. 正常 localhost:8888/consumer/hello 2. 黑名单 localhost:8888/consumer/hello1 localhost:8888/consumer/hello2 总结 回到顶部 这样我们就可以通过配置文件轻松地控制接口的对外服务。
http://www.hkea.cn/news/14280289/

相关文章:

  • 公司网站制作方案怎么做wap网站
  • 本地网站搭建如何访问网页做统计表的网站
  • php网站搬家软件制作网页的步骤搜集素材
  • 网站的推广策略商城类网站建设方案
  • 台州快速建站公司环境设计公司排名
  • 山东省建设厅注册中心网站北京seo代理计费
  • 哪个网站做兼职猎头jsp网站建设论文
  • 简单的企业网站建设教程内蒙古建设工程信息网
  • ip做网站需要过白名单吗杭州网站排名提升
  • php就是做网站吗长兴网站建设
  • 做一个自己的网站流程广州市服务好的网站制作排名
  • 马克杯网站开发app网站制作
  • vue 做pc网站可以吗网站打开出现建设中
  • 网站备案授权书填写模板网站分析 案例
  • 辽宁网站建设电话网站速度查询
  • 建设营销网站的目的wordpress浏览pdf
  • 手机网站模版php源码天元建设集团有限公司济南第六建筑工程公司
  • 网站建设合作流程云存储做网站
  • 制作公司网站教程广州白云网站建设公司
  • 广告网站推荐全国做暧小视频网站
  • 贵阳建设工程招聘信息网站建立主题网站的顺序一般是
  • 自己搭建环境建设网站卖主机 服务器的网站
  • 软件技术专业里有网站开发吗网站建设管理ppt
  • 怎么推广自己的店铺站内优化网站怎么做
  • 做淘宝的货源网站不用交钱的夜间禁用app
  • 免费网站制作新闻广西省桂林市
  • 设计网站的功能有哪些内容国内买机票的网站建设
  • 网站仿制公司代运营电商平台公司
  • 莱芜网站建设sikesoftwordpress 搜索 标题 内容
  • 女性门户网站织梦模板室内外设计