Skip to content

Commit

Permalink
[type:fix] fix sign plugin context path error (#5379)
Browse files Browse the repository at this point in the history
* [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]>
  • Loading branch information
moremind and misaya295 authored Jan 2, 2024
1 parent e3384f8 commit e27a26b
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
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.
*/
Expand All @@ -34,7 +36,8 @@ public ShenyuContext decorator(final ShenyuContext shenyuContext, final MetaData
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
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.
*/
Expand All @@ -34,7 +36,8 @@ public ShenyuContext decorator(final ShenyuContext shenyuContext, final MetaData
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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void testDecorator() {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
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.
*/
Expand All @@ -34,7 +36,8 @@ public ShenyuContext decorator(final ShenyuContext shenyuContext, final MetaData
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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -112,7 +115,7 @@ private VerifyResult signatureVerify(final ServerWebExchange exchange, final BiF
VerifyResult result = verify(signParameters, appAuthData, signFunction);

if (result.isSuccess()) {
handleExchange(exchange, appAuthData, shenyuContext.getContextPath());
handleExchange(exchange, appAuthData, shenyuContext);
}

return result;
Expand Down Expand Up @@ -203,16 +206,33 @@ private VerifyResult verifySign(final String signKey,

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());
}
}

0 comments on commit e27a26b

Please sign in to comment.