mirror of
https://gitee.com/durcframework/SOP.git
synced 2025-08-11 21:57:56 +08:00
2.3.2
This commit is contained in:
@@ -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.AbstractConfiguration;
|
||||||
import com.gitee.sop.gatewaycommon.manager.RouteRepositoryContext;
|
import com.gitee.sop.gatewaycommon.manager.RouteRepositoryContext;
|
||||||
import com.gitee.sop.gatewaycommon.param.ParamBuilder;
|
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.ObjectProvider;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
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.boot.web.reactive.error.ErrorWebExceptionHandler;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Primary;
|
import org.springframework.context.annotation.Primary;
|
||||||
import org.springframework.core.Ordered;
|
import org.springframework.core.Ordered;
|
||||||
import org.springframework.core.annotation.Order;
|
import org.springframework.core.annotation.Order;
|
||||||
import org.springframework.http.codec.ServerCodecConfigurer;
|
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.reactive.result.view.ViewResolver;
|
||||||
import org.springframework.web.server.ServerWebExchange;
|
import org.springframework.web.server.ServerWebExchange;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -39,6 +47,9 @@ public class BaseGatewayConfiguration extends AbstractConfiguration {
|
|||||||
ApiConfig.getInstance().setUseGateway(true);
|
ApiConfig.getInstance().setUseGateway(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Value("${sop.restful.path:/rest}")
|
||||||
|
private String restPath;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 自定义异常处理[@@]注册Bean时依赖的Bean,会从容器中直接获取,所以直接注入即可
|
* 自定义异常处理[@@]注册Bean时依赖的Bean,会从容器中直接获取,所以直接注入即可
|
||||||
*
|
*
|
||||||
@@ -122,4 +133,34 @@ public class BaseGatewayConfiguration extends AbstractConfiguration {
|
|||||||
EnvGrayFilter envGrayFilter() {
|
EnvGrayFilter envGrayFilter() {
|
||||||
return new 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();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Reference in New Issue
Block a user