Merge branch 'master' into taobao_model

taobao_model
tanghc 5 years ago
commit 88802f14cd
  1. 4
      changelog.md
  2. 7
      doc/docs/files/10089_自定义校验token.md
  3. 2
      sop-auth/pom.xml
  4. 2
      sop-common/pom.xml
  5. 6
      sop-common/sop-bridge-gateway/pom.xml
  6. 6
      sop-common/sop-bridge-zuul/pom.xml
  7. 4
      sop-common/sop-gateway-common/pom.xml
  8. 72
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/ServerWebExchangeUtil.java
  9. 5
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/filter/IndexFilter.java
  10. 4
      sop-common/sop-service-common/pom.xml
  11. 2
      sop-example/sop-book/sop-book-web/pom.xml
  12. 2
      sop-example/sop-easyopen/pom.xml
  13. 2
      sop-example/sop-springmvc/pom.xml
  14. 2
      sop-example/sop-story/sop-story-web/pom.xml
  15. 2
      sop-gateway/pom.xml
  16. 2
      sop-website/pom.xml

@ -1,5 +1,9 @@
# changelog # changelog
## 3.1.5
修复文件上传大小不一致问题
## 3.1.4 ## 3.1.4
- 优化跨域 - 优化跨域

@ -34,10 +34,11 @@ public StoryResult token(StoryParam story) {
`TokenValidator`是一个函数式接口,可以直接使用Lambda表达式,示例代码如下: `TokenValidator`是一个函数式接口,可以直接使用Lambda表达式,示例代码如下:
```java ```java
public class ZuulConfig extends AlipayZuulConfiguration { @Component
public class MyConfig {
@Override @PostConstruct
protected void doAfter() { public void after() {
ApiConfig.getInstance().setTokenValidator(apiParam -> { ApiConfig.getInstance().setTokenValidator(apiParam -> {
// 获取客户端传递过来的token // 获取客户端传递过来的token
String token = apiParam.fetchAccessToken(); String token = apiParam.fetchAccessToken();

@ -26,7 +26,7 @@
<dependency> <dependency>
<groupId>com.gitee.sop</groupId> <groupId>com.gitee.sop</groupId>
<artifactId>sop-service-common</artifactId> <artifactId>sop-service-common</artifactId>
<version>3.1.4-SNAPSHOT</version> <version>3.1.5-SNAPSHOT</version>
</dependency> </dependency>
<!-- sop相关配置 end--> <!-- sop相关配置 end-->

@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.gitee.sop</groupId> <groupId>com.gitee.sop</groupId>
<artifactId>sop-common</artifactId> <artifactId>sop-common</artifactId>
<version>3.1.4-SNAPSHOT</version> <version>3.1.5-SNAPSHOT</version>
<packaging>pom</packaging> <packaging>pom</packaging>
<properties> <properties>

@ -5,11 +5,11 @@
<parent> <parent>
<artifactId>sop-common</artifactId> <artifactId>sop-common</artifactId>
<groupId>com.gitee.sop</groupId> <groupId>com.gitee.sop</groupId>
<version>3.1.4-SNAPSHOT</version> <version>3.1.5-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath> <relativePath>../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<version>3.1.4-SNAPSHOT</version> <version>3.1.5-SNAPSHOT</version>
<artifactId>sop-bridge-gateway</artifactId> <artifactId>sop-bridge-gateway</artifactId>
@ -17,7 +17,7 @@
<dependency> <dependency>
<groupId>com.gitee.sop</groupId> <groupId>com.gitee.sop</groupId>
<artifactId>sop-gateway-common</artifactId> <artifactId>sop-gateway-common</artifactId>
<version>3.1.4-SNAPSHOT</version> <version>3.1.5-SNAPSHOT</version>
</dependency> </dependency>
<dependency> <dependency>

@ -5,11 +5,11 @@
<parent> <parent>
<artifactId>sop-common</artifactId> <artifactId>sop-common</artifactId>
<groupId>com.gitee.sop</groupId> <groupId>com.gitee.sop</groupId>
<version>3.1.4-SNAPSHOT</version> <version>3.1.5-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath> <relativePath>../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<version>3.1.4-SNAPSHOT</version> <version>3.1.5-SNAPSHOT</version>
<artifactId>sop-bridge-zuul</artifactId> <artifactId>sop-bridge-zuul</artifactId>
@ -17,7 +17,7 @@
<dependency> <dependency>
<groupId>com.gitee.sop</groupId> <groupId>com.gitee.sop</groupId>
<artifactId>sop-gateway-common</artifactId> <artifactId>sop-gateway-common</artifactId>
<version>3.1.4-SNAPSHOT</version> <version>3.1.5-SNAPSHOT</version>
</dependency> </dependency>
<dependency> <dependency>

@ -5,11 +5,11 @@
<parent> <parent>
<groupId>com.gitee.sop</groupId> <groupId>com.gitee.sop</groupId>
<artifactId>sop-common</artifactId> <artifactId>sop-common</artifactId>
<version>3.1.4-SNAPSHOT</version> <version>3.1.5-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath> <relativePath>../pom.xml</relativePath>
</parent> </parent>
<artifactId>sop-gateway-common</artifactId> <artifactId>sop-gateway-common</artifactId>
<version>3.1.4-SNAPSHOT</version> <version>3.1.5-SNAPSHOT</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>sop-gateway-common</name> <name>sop-gateway-common</name>

@ -30,6 +30,8 @@ import reactor.core.publisher.Mono;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.io.IOException; import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -47,6 +49,10 @@ public class ServerWebExchangeUtil {
private static final String UNKNOWN_PATH = "/sop/unknown"; private static final String UNKNOWN_PATH = "/sop/unknown";
private static final String REST_PATH = "/rest"; private static final String REST_PATH = "/rest";
private static final String IP_UNKNOWN = "unknown";
private static final String IP_LOCAL = "127.0.0.1";
private static final int IP_LEN = 15;
private static FormHttpMessageConverter formHttpMessageConverter = new FormHttpMessageConverter(); private static FormHttpMessageConverter formHttpMessageConverter = new FormHttpMessageConverter();
private static final List<HttpMessageReader<?>> messageReaders; private static final List<HttpMessageReader<?>> messageReaders;
@ -111,21 +117,31 @@ public class ServerWebExchangeUtil {
return chain.filter(newExchange); return chain.filter(newExchange);
} }
public static ApiParam getApiParam(ServerWebExchange exchange, String body) { public static ApiParam getApiParamByQuery(ServerWebExchange exchange, String query) {
ApiParam apiParam = new ApiParam();
String ip = getIP(exchange.getRequest());
apiParam.setIp(ip);
Map<String, ?> params = RequestUtil.parseQueryToMap(query);
if (params != null) {
apiParam.putAll(params);
}
setApiParam(exchange, apiParam);
return apiParam;
}
public static ApiParam getApiParam(ServerWebExchange exchange, byte[] body) {
MediaType contentType = exchange.getRequest().getHeaders().getContentType(); MediaType contentType = exchange.getRequest().getHeaders().getContentType();
if (contentType == null) { if (contentType == null) {
contentType = MediaType.APPLICATION_FORM_URLENCODED; contentType = MediaType.APPLICATION_FORM_URLENCODED;
} }
ApiParam apiParam = new ApiParam(); ApiParam apiParam = new ApiParam();
String ip = Optional.ofNullable(exchange.getRequest().getRemoteAddress()) String ip = getIP(exchange.getRequest());
.map(address -> address.getAddress().getHostAddress())
.orElse("");
apiParam.setIp(ip); apiParam.setIp(ip);
Map<String, ?> params = null; Map<String, ?> params = null;
String contentTypeStr = contentType.toString().toLowerCase(); String contentTypeStr = contentType.toString().toLowerCase();
// 如果是json方式提交 // 如果是json方式提交
if (StringUtils.containsAny(contentTypeStr, "json", "text")) { if (StringUtils.containsAny(contentTypeStr, "json", "text")) {
JSONObject jsonObject = JSON.parseObject(body); JSONObject jsonObject = JSON.parseObject(new String(body, SopConstants.CHARSET_UTF8));
apiParam.putAll(jsonObject); apiParam.putAll(jsonObject);
} else if (StringUtils.containsIgnoreCase(contentTypeStr, "multipart")) { } else if (StringUtils.containsIgnoreCase(contentTypeStr, "multipart")) {
// 如果是文件上传请求 // 如果是文件上传请求
@ -136,7 +152,7 @@ public class ServerWebExchangeUtil {
apiParam.setUploadContext(uploadInfo.getUploadContext()); apiParam.setUploadContext(uploadInfo.getUploadContext());
} else { } else {
// APPLICATION_FORM_URLENCODED请求 // APPLICATION_FORM_URLENCODED请求
params = RequestUtil.parseQueryToMap(body); params = RequestUtil.parseQueryToMap(new String(body, SopConstants.CHARSET_UTF8));
} }
if (params != null) { if (params != null) {
apiParam.putAll(params); apiParam.putAll(params);
@ -145,6 +161,45 @@ public class ServerWebExchangeUtil {
return apiParam; return apiParam;
} }
/**
* 获取客户端真实ip
* @param request request
* @return 返回ip
*/
public static String getIP(ServerHttpRequest request) {
HttpHeaders headers = request.getHeaders();
String ipAddress = headers.getFirst("x-forwarded-for");
if (ipAddress == null || ipAddress.length() == 0 || IP_UNKNOWN.equalsIgnoreCase(ipAddress)) {
ipAddress = headers.getFirst("Proxy-Client-IP");
}
if (ipAddress == null || ipAddress.length() == 0 || IP_UNKNOWN.equalsIgnoreCase(ipAddress)) {
ipAddress = headers.getFirst("WL-Proxy-Client-IP");
}
if (ipAddress == null || ipAddress.length() == 0 || IP_UNKNOWN.equalsIgnoreCase(ipAddress)) {
ipAddress = Optional.ofNullable(request.getRemoteAddress())
.map(address -> address.getAddress().getHostAddress())
.orElse("");
if (IP_LOCAL.equals(ipAddress)) {
// 根据网卡取本机配置的IP
try {
InetAddress inet = InetAddress.getLocalHost();
ipAddress = inet.getHostAddress();
} catch (UnknownHostException e) {
// ignore
}
}
}
// 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
if (ipAddress != null && ipAddress.length() > IP_LEN) {
int index = ipAddress.indexOf(",");
if (index > 0) {
ipAddress = ipAddress.substring(0, index);
}
}
return ipAddress;
}
public static ApiParam getApiParam(ServerWebExchange exchange, Map<String, String> params) { public static ApiParam getApiParam(ServerWebExchange exchange, Map<String, String> params) {
ApiParam apiParam = new ApiParam(); ApiParam apiParam = new ApiParam();
apiParam.putAll(params); apiParam.putAll(params);
@ -204,11 +259,10 @@ public class ServerWebExchangeUtil {
* 获取一个文件上传request * 获取一个文件上传request
* *
* @param exchange 当前ServerWebExchange * @param exchange 当前ServerWebExchange
* @param requestBody 上传文件请求体内容 * @param data 上传文件请求体内容
* @return 返回文件上传request * @return 返回文件上传request
*/ */
public static HttpServletRequest getFileUploadRequest(ServerWebExchange exchange, String requestBody) { public static HttpServletRequest getFileUploadRequest(ServerWebExchange exchange, byte[] data) {
byte[] data = requestBody.getBytes(StandardCharsets.UTF_8);
return new FileUploadHttpServletRequest(exchange.getRequest(), data); return new FileUploadHttpServletRequest(exchange.getRequest(), data);
} }

@ -82,9 +82,8 @@ public class IndexFilter implements WebFilter {
// 读取请求体中的内容 // 读取请求体中的内容
Mono<?> modifiedBody = serverRequest.bodyToMono(byte[].class) Mono<?> modifiedBody = serverRequest.bodyToMono(byte[].class)
.flatMap(data -> { .flatMap(data -> {
String body = new String(data, SopConstants.CHARSET_UTF8);
// 构建ApiParam // 构建ApiParam
ApiParam apiParam = ServerWebExchangeUtil.getApiParam(exchange, body); ApiParam apiParam = ServerWebExchangeUtil.getApiParam(exchange, data);
// 签名验证 // 签名验证
doValidate(exchange, apiParam); doValidate(exchange, apiParam);
return Mono.just(data); return Mono.just(data);
@ -113,7 +112,7 @@ public class IndexFilter implements WebFilter {
// 原始参数 // 原始参数
String originalQuery = uri.getRawQuery(); String originalQuery = uri.getRawQuery();
// 构建ApiParam // 构建ApiParam
ApiParam apiParam = ServerWebExchangeUtil.getApiParam(exchange, originalQuery); ApiParam apiParam = ServerWebExchangeUtil.getApiParamByQuery(exchange, originalQuery);
// 签名验证 // 签名验证
doValidate(exchange, apiParam); doValidate(exchange, apiParam);

@ -6,11 +6,11 @@
<parent> <parent>
<groupId>com.gitee.sop</groupId> <groupId>com.gitee.sop</groupId>
<artifactId>sop-common</artifactId> <artifactId>sop-common</artifactId>
<version>3.1.4-SNAPSHOT</version> <version>3.1.5-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath> <relativePath>../pom.xml</relativePath>
</parent> </parent>
<artifactId>sop-service-common</artifactId> <artifactId>sop-service-common</artifactId>
<version>3.1.4-SNAPSHOT</version> <version>3.1.5-SNAPSHOT</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>sop-service-common</name> <name>sop-service-common</name>

@ -28,7 +28,7 @@
<dependency> <dependency>
<groupId>com.gitee.sop</groupId> <groupId>com.gitee.sop</groupId>
<artifactId>sop-service-common</artifactId> <artifactId>sop-service-common</artifactId>
<version>3.1.4-SNAPSHOT</version> <version>3.1.5-SNAPSHOT</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.gitee.sop</groupId> <groupId>com.gitee.sop</groupId>

@ -29,7 +29,7 @@
<dependency> <dependency>
<groupId>com.gitee.sop</groupId> <groupId>com.gitee.sop</groupId>
<artifactId>sop-service-common</artifactId> <artifactId>sop-service-common</artifactId>
<version>3.1.4-SNAPSHOT</version> <version>3.1.5-SNAPSHOT</version>
</dependency> </dependency>
<!-- 使用nacos注册中心 <!-- 使用nacos注册中心

@ -20,7 +20,7 @@
<dependency> <dependency>
<groupId>com.gitee.sop</groupId> <groupId>com.gitee.sop</groupId>
<artifactId>sop-service-common</artifactId> <artifactId>sop-service-common</artifactId>
<version>3.1.4-SNAPSHOT</version> <version>3.1.5-SNAPSHOT</version>
</dependency> </dependency>
<!-- nacos --> <!-- nacos -->
<dependency> <dependency>

@ -28,7 +28,7 @@
<dependency> <dependency>
<groupId>com.gitee.sop</groupId> <groupId>com.gitee.sop</groupId>
<artifactId>sop-service-common</artifactId> <artifactId>sop-service-common</artifactId>
<version>3.1.4-SNAPSHOT</version> <version>3.1.5-SNAPSHOT</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.gitee.sop</groupId> <groupId>com.gitee.sop</groupId>

@ -34,7 +34,7 @@
<groupId>com.gitee.sop</groupId> <groupId>com.gitee.sop</groupId>
<artifactId>sop-bridge-gateway</artifactId> <artifactId>sop-bridge-gateway</artifactId>
<!--<artifactId>sop-bridge-zuul</artifactId>--> <!--<artifactId>sop-bridge-zuul</artifactId>-->
<version>3.1.4-SNAPSHOT</version> <version>3.1.5-SNAPSHOT</version>
</dependency> </dependency>
<dependency> <dependency>

@ -35,7 +35,7 @@
<dependency> <dependency>
<groupId>com.gitee.sop</groupId> <groupId>com.gitee.sop</groupId>
<artifactId>sop-gateway-common</artifactId> <artifactId>sop-gateway-common</artifactId>
<version>3.1.4-SNAPSHOT</version> <version>3.1.5-SNAPSHOT</version>
</dependency> </dependency>
<dependency> <dependency>

Loading…
Cancel
Save