This is an automated email from the ASF dual-hosted git repository.
xiaoyu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git
The following commit(s) were added to refs/heads/master by this push:
new e27a26b8ca [type:fix] fix sign plugin context path error (#5379)
e27a26b8ca is described below
commit e27a26b8ca3de5c167dc676f22d280f217cd1e5f
Author: moremind <[email protected]>
AuthorDate: Tue Jan 2 16:50:52 2024 +0800
[type:fix] fix sign plugin context path error (#5379)
* [type:fix] fix sign plugin cnotext path error
* [type:fix] fix sign plugin cnotext path error
* [type:fix] fix sign plugin cnotext path error
* [type:fix] fix sign plugin cnotext path error
* [type:fix] fix sign plugin cnotext path error
* [type:fix] fix sign plugin cnotext path error
* [type:fix] fix sign plugin cnotext path error
* [type:fix] fix sign plugin cnotext path error
* fix sign
* fix sign
---------
Co-authored-by: Misaya295 <[email protected]>
---
.../context/DivideShenyuContextDecorator.java | 5 +++-
.../context/SpringCloudShenyuContextDecorator.java | 5 +++-
.../SpringCloudShenyuContextDecoratorTest.java | 2 +-
.../context/WebSocketShenyuContextDecorator.java | 5 +++-
.../plugin/sign/service/ComposableSignService.java | 28 ++++++++++++++++++----
5 files changed, 37 insertions(+), 8 deletions(-)
diff --git
a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-divide/src/main/java/org/apache/shenyu/plugin/divide/context/DivideShenyuContextDecorator.java
b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-divide/src/main/java/org/apache/shenyu/plugin/divide/context/DivideShenyuContextDecorator.java
index 0cb5f0c922..0b85459774 100644
---
a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-divide/src/main/java/org/apache/shenyu/plugin/divide/context/DivideShenyuContextDecorator.java
+++
b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-divide/src/main/java/org/apache/shenyu/plugin/divide/context/DivideShenyuContextDecorator.java
@@ -23,6 +23,8 @@ import org.apache.shenyu.common.enums.RpcTypeEnum;
import org.apache.shenyu.plugin.api.context.ShenyuContext;
import org.apache.shenyu.plugin.api.context.ShenyuContextDecorator;
+import java.util.Optional;
+
/**
* The type Divide shenyu context decorator.
*/
@@ -34,7 +36,8 @@ public class DivideShenyuContextDecorator implements
ShenyuContextDecorator {
shenyuContext.setMethod(path);
shenyuContext.setRealUrl(path);
shenyuContext.setRpcType(RpcTypeEnum.HTTP.getName());
- shenyuContext.setModule(String.format("%s-%s",
PluginEnum.DIVIDE.getName(), shenyuContext.getRpcType()));
+
shenyuContext.setModule(Optional.ofNullable(metaData).map(MetaData::getAppName)
+ .orElse(String.format("%s-%s", PluginEnum.DIVIDE.getName(),
shenyuContext.getRpcType())));
return shenyuContext;
}
}
diff --git
a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-springcloud/src/main/java/org/apache/shenyu/plugin/springcloud/context/SpringCloudShenyuContextDecorator.java
b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-springcloud/src/main/java/org/apache/shenyu/plugin/springcloud/context/SpringCloudShenyuContextDecorator.java
index 00199dee16..0f06b4e9ca 100644
---
a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-springcloud/src/main/java/org/apache/shenyu/plugin/springcloud/context/SpringCloudShenyuContextDecorator.java
+++
b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-springcloud/src/main/java/org/apache/shenyu/plugin/springcloud/context/SpringCloudShenyuContextDecorator.java
@@ -23,6 +23,8 @@ import org.apache.shenyu.common.enums.RpcTypeEnum;
import org.apache.shenyu.plugin.api.context.ShenyuContext;
import org.apache.shenyu.plugin.api.context.ShenyuContextDecorator;
+import java.util.Optional;
+
/**
* The type SpringCloud shenyu context decorator.
*/
@@ -34,7 +36,8 @@ public class SpringCloudShenyuContextDecorator implements
ShenyuContextDecorator
shenyuContext.setMethod(path);
shenyuContext.setRealUrl(path);
shenyuContext.setRpcType(RpcTypeEnum.SPRING_CLOUD.getName());
- shenyuContext.setModule(String.format("%s-%s",
PluginEnum.DIVIDE.getName(), shenyuContext.getRpcType()));
+
shenyuContext.setModule(Optional.ofNullable(metaData).map(MetaData::getAppName)
+ .orElse(String.format("%s-%s",
PluginEnum.SPRING_CLOUD.getName(), shenyuContext.getRpcType())));
return shenyuContext;
}
diff --git
a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-springcloud/src/test/java/org/apache/shenyu/plugin/springcloud/context/SpringCloudShenyuContextDecoratorTest.java
b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-springcloud/src/test/java/org/apache/shenyu/plugin/springcloud/context/SpringCloudShenyuContextDecoratorTest.java
index 18cb470a6e..9680dbf918 100644
---
a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-springcloud/src/test/java/org/apache/shenyu/plugin/springcloud/context/SpringCloudShenyuContextDecoratorTest.java
+++
b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-springcloud/src/test/java/org/apache/shenyu/plugin/springcloud/context/SpringCloudShenyuContextDecoratorTest.java
@@ -42,7 +42,7 @@ public final class SpringCloudShenyuContextDecoratorTest {
Assertions.assertNull(shenyuContext.getMethod());
Assertions.assertNull(shenyuContext.getRealUrl());
Assertions.assertEquals(shenyuContext.getRpcType(), "springCloud");
- Assertions.assertEquals(shenyuContext.getModule(),
"divide-springCloud");
+ Assertions.assertEquals(shenyuContext.getModule(),
"springCloud-springCloud");
}
@Test
diff --git
a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-websocket/src/main/java/org/apache/shenyu/plugin/websocket/context/WebSocketShenyuContextDecorator.java
b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-websocket/src/main/java/org/apache/shenyu/plugin/websocket/context/WebSocketShenyuContextDecorator.java
index 4dcea169a5..910f1c5e44 100644
---
a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-websocket/src/main/java/org/apache/shenyu/plugin/websocket/context/WebSocketShenyuContextDecorator.java
+++
b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-websocket/src/main/java/org/apache/shenyu/plugin/websocket/context/WebSocketShenyuContextDecorator.java
@@ -23,6 +23,8 @@ import org.apache.shenyu.common.enums.RpcTypeEnum;
import org.apache.shenyu.plugin.api.context.ShenyuContext;
import org.apache.shenyu.plugin.api.context.ShenyuContextDecorator;
+import java.util.Optional;
+
/**
* The type WebSocket shenyu context decorator.
*/
@@ -34,7 +36,8 @@ public class WebSocketShenyuContextDecorator implements
ShenyuContextDecorator {
shenyuContext.setMethod(path);
shenyuContext.setRealUrl(path);
shenyuContext.setRpcType(RpcTypeEnum.WEB_SOCKET.getName());
- shenyuContext.setModule(String.format("%s-%s",
PluginEnum.WEB_SOCKET.getName(), shenyuContext.getRpcType()));
+
shenyuContext.setModule(Optional.ofNullable(metaData).map(MetaData::getAppName)
+ .orElse(String.format("%s-%s",
PluginEnum.WEB_SOCKET.getName(), shenyuContext.getRpcType())));
return shenyuContext;
}
diff --git
a/shenyu-plugin/shenyu-plugin-security/shenyu-plugin-sign/src/main/java/org/apache/shenyu/plugin/sign/service/ComposableSignService.java
b/shenyu-plugin/shenyu-plugin-security/shenyu-plugin-sign/src/main/java/org/apache/shenyu/plugin/sign/service/ComposableSignService.java
index 61a4a416e8..c07937e1f7 100644
---
a/shenyu-plugin/shenyu-plugin-security/shenyu-plugin-sign/src/main/java/org/apache/shenyu/plugin/sign/service/ComposableSignService.java
+++
b/shenyu-plugin/shenyu-plugin-security/shenyu-plugin-sign/src/main/java/org/apache/shenyu/plugin/sign/service/ComposableSignService.java
@@ -18,12 +18,15 @@
package org.apache.shenyu.plugin.sign.service;
import org.apache.commons.collections4.CollectionUtils;
+import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shenyu.common.constant.Constants;
import org.apache.shenyu.common.dto.AppAuthData;
import org.apache.shenyu.common.dto.AuthParamData;
import org.apache.shenyu.common.dto.AuthPathData;
+import org.apache.shenyu.common.enums.PluginEnum;
+import org.apache.shenyu.common.exception.ShenyuException;
import org.apache.shenyu.common.utils.DateUtils;
import org.apache.shenyu.plugin.api.context.ShenyuContext;
import org.apache.shenyu.plugin.api.result.ShenyuResultEnum;
@@ -112,7 +115,7 @@ public class ComposableSignService implements SignService {
VerifyResult result = verify(signParameters, appAuthData,
signFunction);
if (result.isSuccess()) {
- handleExchange(exchange, appAuthData,
shenyuContext.getContextPath());
+ handleExchange(exchange, appAuthData, shenyuContext);
}
return result;
@@ -203,16 +206,33 @@ public class ComposableSignService implements SignService
{
private void handleExchange(final ServerWebExchange exchange,
final AppAuthData appAuthData,
- final String contextPath) {
+ final ShenyuContext context) {
List<AuthParamData> paramDataList = appAuthData.getParamDataList();
if (!CollectionUtils.isEmpty(paramDataList)) {
- paramDataList.stream().filter(p ->
- ("/" + p.getAppName()).equals(contextPath))
+ String realAppName;
+ if (skipSignExchange(context)) {
+ String rawPath = exchange.getRequest().getURI().getRawPath();
+ // get the context path from the request url
+ String[] contextPath = StringUtils.split(rawPath, "/");
+ if (ArrayUtils.isEmpty(contextPath)) {
+ throw new ShenyuException("Cannot find the context
path(AppName) from the request url");
+ }
+ realAppName = contextPath[0];
+ } else {
+ realAppName = context.getModule();
+ }
+ paramDataList.stream().filter(p ->
p.getAppName().equals(realAppName))
.map(AuthParamData::getAppParam)
.filter(StringUtils::isNoneBlank).findFirst()
.ifPresent(param ->
exchange.getRequest().mutate().headers(httpHeaders ->
httpHeaders.set(Constants.APP_PARAM, param)).build());
}
}
+
+ private boolean skipSignExchange(final ShenyuContext context) {
+ return StringUtils.equals(String.format("%s-%s",
PluginEnum.SPRING_CLOUD.getName(), context.getRpcType()), context.getModule())
+ || StringUtils.equals(String.format("%s-%s",
PluginEnum.DIVIDE.getName(), context.getRpcType()), context.getModule())
+ || StringUtils.equals(String.format("%s-%s",
PluginEnum.WEB_SOCKET.getName(), context.getRpcType()), context.getModule());
+ }
}