网站后台怎么转发网页内容(Spring后台微服务需要配置ssl,我只是个搬砖的)

优采云 发布时间: 2021-11-15 20:10

  网站后台怎么转发网页内容(Spring后台微服务需要配置ssl,我只是个搬砖的)

  Spring Cloud Gateway 收到https请求,根据路由规则转发给后台微服务,但是默认转发给后台微服务时,仍然是https请求,需要后台微服务配置ssl,每个服务器需要有一个域名,在这个名字下是不实用的。正常情况下,一个网站唯一的外部网关是通过域名访问的,转发给后端微服务时,使用的是IP地址。

  SpringCloud Zuul默认会帮我们将https转换为http,gatewy需要我们添加过滤器来实现转换:

  我只是一个实体,参考大佬的博客:

  大佬分析了一下源码,给出了解决办法:

  SpringCloud Gateway https请求转为http并路由到后台微服务

  添加过滤器:

  import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR;

import java.net.URI;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;

import org.springframework.cloud.gateway.filter.GlobalFilter;

import org.springframework.core.Ordered;

import org.springframework.stereotype.Component;

import org.springframework.web.server.ServerWebExchange;

import org.springframework.web.util.UriComponentsBuilder;

import reactor.core.publisher.Mono;

@Component

public class SchemeFilter implements GlobalFilter, Ordered {

@Override

public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) {

Object uriObj = exchange.getAttributes().get(GATEWAY_REQUEST_URL_ATTR);

if (uriObj != null) {

URI uri = (URI) uriObj;

uri = this.upgradeConnection(uri, "http");

exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, uri);

}

return chain.filter(exchange);

}

private URI upgradeConnection(URI uri, String scheme) {

UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUri(uri).scheme(scheme);

if (uri.getRawQuery() != null) {

// When building the URI, UriComponentsBuilder verify the allowed characters and does not

// support the '+' so we replace it for its equivalent '%20'.

// See issue https://jira.spring.io/browse/SPR-10172

uriComponentsBuilder.replaceQuery(uri.getRawQuery().replace("+", "%20"));

}

return uriComponentsBuilder.build(true).toUri();

}

@Override

public int getOrder() {

return 10101;

}

}

  然后。. . . . . 就是这样

  删除路由前缀:说说Zuul和Gateway路由的区别

  使用Zuul后,再使用Gateway,你会发现Zuul会删除路由前缀。例如:

0 个评论

要回复文章请先登录注册


官方客服QQ群

微信人工客服

QQ人工客服


线