diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/configuration/BaseGatewayConfiguration.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/configuration/BaseGatewayConfiguration.java index 7a315d02..1b92d609 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/configuration/BaseGatewayConfiguration.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/configuration/BaseGatewayConfiguration.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(临时重定向): + *

+ * 在这种情况下,请求应该与另一个URI重复,但后续的请求应仍使用原始的URI。 + * 与302相反,当重新发出原始请求时,不允许更改请求方法。 例如,应该使用另一个POST请求来重复POST请求 + *

+ * 308 Permanent Redirect (永久重定向): + *

+ * 请求和所有将来的请求应该使用另一个URI重复。 + * 307和308重复302和301的行为,但不允许HTTP方法更改。 例如,将表单提交给永久重定向的资源可能会顺利进行。 + *

+ * https://www.cnblogs.com/wuguanglin/p/redirect.html + * + * @return + */ + @Bean + @ConditionalOnProperty(value = "sop.restful.enable", havingValue = "true") + RouterFunction 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(); + }); + } } diff --git a/sop-gateway/src/main/java/com/gitee/sop/gateway/controller/RestWebFlux.java b/sop-gateway/src/main/java/com/gitee/sop/gateway/controller/RestWebFlux.java deleted file mode 100644 index b6854f29..00000000 --- a/sop-gateway/src/main/java/com/gitee/sop/gateway/controller/RestWebFlux.java +++ /dev/null @@ -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(临时重定向): - *

- * 在这种情况下,请求应该与另一个URI重复,但后续的请求应仍使用原始的URI。 - * 与302相反,当重新发出原始请求时,不允许更改请求方法。 例如,应该使用另一个POST请求来重复POST请求 - *

- * 308 Permanent Redirect (永久重定向): - *

- * 请求和所有将来的请求应该使用另一个URI重复。 - * 307和308重复302和301的行为,但不允许HTTP方法更改。 例如,将表单提交给永久重定向的资源可能会顺利进行。 - *

- * https://www.cnblogs.com/wuguanglin/p/redirect.html - * - * @return - */ - @Bean - RouterFunction 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(); - }); - } - -}