pull/1/head
tanghc 5 years ago
parent 3925568bea
commit f5b5a1286f
  1. 41
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/configuration/BaseGatewayConfiguration.java
  2. 52
      sop-gateway/src/main/java/com/gitee/sop/gateway/controller/RestWebFlux.java

@ -15,17 +15,25 @@ import com.gitee.sop.gatewaycommon.gateway.route.ReadBodyRoutePredicateFactory;
import com.gitee.sop.gatewaycommon.manager.AbstractConfiguration;
import com.gitee.sop.gatewaycommon.manager.RouteRepositoryContext;
import com.gitee.sop.gatewaycommon.param.ParamBuilder;
import com.gitee.sop.gatewaycommon.param.ParamNames;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.codec.ServerCodecConfigurer;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.reactive.result.view.ViewResolver;
import org.springframework.web.server.ServerWebExchange;
import java.net.URI;
import java.util.Collections;
import java.util.List;
@ -39,6 +47,9 @@ public class BaseGatewayConfiguration extends AbstractConfiguration {
ApiConfig.getInstance().setUseGateway(true);
}
@Value("${sop.restful.path:/rest}")
private String restPath;
/**
* 自定义异常处理[@@]注册Bean时依赖的Bean会从容器中直接获取所以直接注入即可
*
@ -122,4 +133,34 @@ public class BaseGatewayConfiguration extends AbstractConfiguration {
EnvGrayFilter envGrayFilter() {
return new EnvGrayFilter();
}
/**
* 307 Temporary Redirect临时重定向:
* <p>
* 在这种情况下请求应该与另一个URI重复但后续的请求应仍使用原始的URI
* 与302相反当重新发出原始请求时不允许更改请求方法 例如应该使用另一个POST请求来重复POST请求
* <p>
* 308 Permanent Redirect 永久重定向:
* <p>
* 请求和所有将来的请求应该使用另一个URI重复
* 307和308重复302和301的行为但不允许HTTP方法更改 例如将表单提交给永久重定向的资源可能会顺利进行
* <p>
* https://www.cnblogs.com/wuguanglin/p/redirect.html
*
* @return
*/
@Bean
@ConditionalOnProperty(value = "sop.restful.enable", havingValue = "true")
RouterFunction<ServerResponse> routerFunction() {
return RouterFunctions.route(RequestPredicates.GET(restPath + "/**"), (serverRequest) -> {
String url = serverRequest.path();
int index = url.indexOf(restPath);
// 取/rest的后面部分
String path = url.substring(index + restPath.length());
String query = ParamNames.API_NAME + "=" + path + "&" + ParamNames.VERSION_NAME + "=";
return ServerResponse
.temporaryRedirect(URI.create("/?" + query))
.build();
});
}
}

@ -1,52 +0,0 @@
package com.gitee.sop.gateway.controller;
import com.gitee.sop.gatewaycommon.param.ParamNames;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;
import java.net.URI;
/**
* @author tanghc
*/
@Configuration
public class RestWebFlux {
@Value("${sop.restful.path:/rest}")
private String restPath;
/**
* 307 Temporary Redirect临时重定向:
* <p>
* 在这种情况下请求应该与另一个URI重复但后续的请求应仍使用原始的URI
* 与302相反当重新发出原始请求时不允许更改请求方法 例如应该使用另一个POST请求来重复POST请求
* <p>
* 308 Permanent Redirect 永久重定向:
* <p>
* 请求和所有将来的请求应该使用另一个URI重复
* 307和308重复302和301的行为但不允许HTTP方法更改 例如将表单提交给永久重定向的资源可能会顺利进行
* <p>
* https://www.cnblogs.com/wuguanglin/p/redirect.html
*
* @return
*/
@Bean
RouterFunction<ServerResponse> routerFunction() {
return RouterFunctions.route(RequestPredicates.GET(restPath + "/**"), (serverRequest) -> {
String url = serverRequest.path();
int index = url.indexOf(restPath);
// 取/rest的后面部分
String path = url.substring(index + restPath.length());
String query = ParamNames.API_NAME + "=" + path + "&" + ParamNames.VERSION_NAME + "=";
return ServerResponse
.temporaryRedirect(URI.create("/?" + query))
.build();
});
}
}
Loading…
Cancel
Save