From a863c00d3fb1813cebd5e0a7efbf7e5a7a838a72 Mon Sep 17 00:00:00 2001 From: tanghc Date: Fri, 26 Jul 2019 12:20:46 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=8F=82=E6=95=B0?= =?UTF-8?q?=E7=BB=91=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../servercommon/bean/OpenContextImpl.java | 31 +++++----- .../gitee/sop/servercommon/util/OpenUtil.java | 34 ++++++----- .../storyweb/controller/AlipayController.java | 7 +++ .../TraditionalWebappController.java | 2 +- .../controller/RedirectController.java | 11 +++- .../java/com/gitee/sop/test/PostFormTest.java | 56 +++++++++++++++++++ 6 files changed, 108 insertions(+), 33 deletions(-) create mode 100644 sop-test/src/test/java/com/gitee/sop/test/PostFormTest.java diff --git a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/OpenContextImpl.java b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/OpenContextImpl.java index 138a4082..56aefeec 100644 --- a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/OpenContextImpl.java +++ b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/OpenContextImpl.java @@ -17,18 +17,23 @@ import static com.gitee.sop.servercommon.bean.ParamNames.VERSION_NAME; * @author tanghc */ public class OpenContextImpl implements OpenContext { - private JSONObject jsonObject; + private JSONObject rootJsonObject; private T bizObject; - public OpenContextImpl(JSONObject jsonObject, Class bizClass) { - this.jsonObject = jsonObject; - JSONObject bizJsonObj = this.jsonObject.getJSONObject(BIZ_CONTENT_NAME); - bizObject = (bizClass == null || bizJsonObj == null) ? null : (T) bizJsonObj.toJavaObject(bizClass); + public OpenContextImpl(JSONObject rootJsonObject, Class bizClass) { + this.rootJsonObject = rootJsonObject; + if (bizClass != null) { + JSONObject bizJsonObj = this.rootJsonObject.getJSONObject(BIZ_CONTENT_NAME); + if (bizJsonObj == null) { + bizJsonObj = rootJsonObject; + } + bizObject = (T) bizJsonObj.toJavaObject(bizClass); + } } @Override public String getAppId() { - return jsonObject.getString(APP_KEY_NAME); + return rootJsonObject.getString(APP_KEY_NAME); } @Override @@ -38,36 +43,36 @@ public class OpenContextImpl implements OpenContext { @Override public String getBizContent() { - return jsonObject.getString(BIZ_CONTENT_NAME); + return rootJsonObject.getString(BIZ_CONTENT_NAME); } @Override public String getCharset() { - return jsonObject.getString(CHARSET_NAME); + return rootJsonObject.getString(CHARSET_NAME); } @Override public String getMethod() { - return jsonObject.getString(API_NAME); + return rootJsonObject.getString(API_NAME); } @Override public String getVersion() { - return jsonObject.getString(VERSION_NAME); + return rootJsonObject.getString(VERSION_NAME); } @Override public String getFormat() { - return jsonObject.getString(FORMAT_NAME); + return rootJsonObject.getString(FORMAT_NAME); } @Override public String getSignType() { - return jsonObject.getString(SIGN_TYPE_NAME); + return rootJsonObject.getString(SIGN_TYPE_NAME); } @Override public Date getTimestamp() { - return jsonObject.getDate(TIMESTAMP_NAME); + return rootJsonObject.getDate(TIMESTAMP_NAME); } } diff --git a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/util/OpenUtil.java b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/util/OpenUtil.java index 10f590c1..b6f75d7c 100644 --- a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/util/OpenUtil.java +++ b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/util/OpenUtil.java @@ -5,9 +5,9 @@ import com.alibaba.fastjson.JSONObject; import lombok.extern.slf4j.Slf4j; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; +import org.springframework.http.MediaType; import javax.servlet.http.HttpServletRequest; -import java.io.IOException; import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -19,39 +19,37 @@ import java.util.Set; @Slf4j public class OpenUtil { - private static final String CONTENT_TYPE_URLENCODED = "application/x-www-form-urlencoded"; - private static final String CONTENT_TYPE_JSON = "application/json"; - private static final String CONTENT_TYPE_TEXT = "text/plain"; - private static final String UTF8 = "UTF-8"; /** - * 从request中获取参数。如果提交方式是application/x-www-form-urlencoded,则组装成json格式。 + * 获取request中的参数 * * @param request request对象 - * @return 返回json - * @throws IOException + * @return 返回JSONObject */ public static JSONObject getRequestParams(HttpServletRequest request) { - String requestJson = "{}"; String contentType = request.getContentType(); - if (StringUtils.isBlank(contentType)) { - return new JSONObject(); + if (contentType == null) { + contentType = ""; } contentType = contentType.toLowerCase(); - if (StringUtils.containsAny(contentType, CONTENT_TYPE_JSON, CONTENT_TYPE_TEXT)) { + JSONObject jsonObject; + if (StringUtils.containsAny(contentType, MediaType.APPLICATION_JSON_VALUE, MediaType.TEXT_PLAIN_VALUE)) { try { - requestJson = IOUtils.toString(request.getInputStream(), UTF8); - } catch (IOException e) { + String requestJson = IOUtils.toString(request.getInputStream(), UTF8); + jsonObject = JSON.parseObject(requestJson); + } catch (Exception e) { + jsonObject = new JSONObject(); log.error("获取文本数据流失败", e); } - } else if (StringUtils.containsAny(contentType, CONTENT_TYPE_URLENCODED)) { + } else { Map params = convertRequestParamsToMap(request); - requestJson = JSON.toJSONString(params); + jsonObject = new JSONObject(params); } - return JSON.parseObject(requestJson); + return jsonObject; } + /** * request中的参数转换成map * @@ -60,7 +58,7 @@ public class OpenUtil { */ public static Map convertRequestParamsToMap(HttpServletRequest request) { Map paramMap = request.getParameterMap(); - if(paramMap == null || paramMap.isEmpty()) { + if (paramMap == null || paramMap.isEmpty()) { return Collections.emptyMap(); } Map retMap = new HashMap(paramMap.size()); diff --git a/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/AlipayController.java b/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/AlipayController.java index e15f79e8..8459f1f4 100644 --- a/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/AlipayController.java +++ b/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/AlipayController.java @@ -64,6 +64,13 @@ public class AlipayController { return story; } + // 忽略验证 + @ApiMapping(value = "story.get", version = "2.1", ignoreValidate = true) + public Story getStory21(Story story) { + story.setName(story.getName() + ", story.get2.1, ignoreValidate = true"); + return story; + } + // http://localhost:2222/getStory2 // 遗留接口具备开放平台能力 @ApiAbility diff --git a/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/TraditionalWebappController.java b/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/TraditionalWebappController.java index a2ede01a..fd48dddb 100644 --- a/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/TraditionalWebappController.java +++ b/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/TraditionalWebappController.java @@ -45,7 +45,7 @@ public class TraditionalWebappController { } // http://localhost:8081/rest/food/getFoodByObj?id=2 - @ApiMapping(value = "ggetFoodByObj", method = RequestMethod.GET) + @ApiMapping(value = "getFoodByObj", method = RequestMethod.GET) public Food getFoodByObj(Food food) { return food; } diff --git a/sop-gateway/src/main/java/com/gitee/sop/gateway/controller/RedirectController.java b/sop-gateway/src/main/java/com/gitee/sop/gateway/controller/RedirectController.java index a1d09f27..2e4a7273 100644 --- a/sop-gateway/src/main/java/com/gitee/sop/gateway/controller/RedirectController.java +++ b/sop-gateway/src/main/java/com/gitee/sop/gateway/controller/RedirectController.java @@ -1,6 +1,8 @@ package com.gitee.sop.gateway.controller; import com.gitee.sop.gatewaycommon.bean.SopConstants; +import com.gitee.sop.gatewaycommon.param.ParamNames; +import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; @@ -29,7 +31,14 @@ public class RedirectController { ) throws ServletException, IOException { request.setAttribute(SopConstants.REDIRECT_METHOD_KEY, method); request.setAttribute(SopConstants.REDIRECT_VERSION_KEY, version); - request.getRequestDispatcher(path).forward(request, response); + String queryString = request.getQueryString(); + String versionQuery = ParamNames.VERSION_NAME + '=' + version; + if (StringUtils.isBlank(queryString)) { + queryString = versionQuery; + } else { + queryString = queryString + '&' + versionQuery; + } + request.getRequestDispatcher(path + '?' + queryString).forward(request, response); } } diff --git a/sop-test/src/test/java/com/gitee/sop/test/PostFormTest.java b/sop-test/src/test/java/com/gitee/sop/test/PostFormTest.java new file mode 100644 index 00000000..2f265cc6 --- /dev/null +++ b/sop-test/src/test/java/com/gitee/sop/test/PostFormTest.java @@ -0,0 +1,56 @@ +package com.gitee.sop.test; + +import com.alibaba.fastjson.JSON; +import com.gitee.sop.test.alipay.AlipaySignature; +import org.junit.Test; + +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +/** + * post请求,模拟表单提交(application/x-www-form-urlencoded) + */ +public class PostFormTest extends TestBase { + + String url = "http://localhost:8081/api"; // zuul + String appId = "2019032617262200001"; + // 支付宝私钥 + String privateKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCXJv1pQFqWNA/++OYEV7WYXwexZK/J8LY1OWlP9X0T6wHFOvxNKRvMkJ5544SbgsJpVcvRDPrcxmhPbi/sAhdO4x2PiPKIz9Yni2OtYCCeaiE056B+e1O2jXoLeXbfi9fPivJZkxH/tb4xfLkH3bA8ZAQnQsoXA0SguykMRZntF0TndUfvDrLqwhlR8r5iRdZLB6F8o8qXH6UPDfNEnf/K8wX5T4EB1b8x8QJ7Ua4GcIUqeUxGHdQpzNbJdaQvoi06lgccmL+PHzminkFYON7alj1CjDN833j7QMHdPtS9l7B67fOU/p2LAAkPMtoVBfxQt9aFj7B8rEhGCz02iJIBAgMBAAECggEARqOuIpY0v6WtJBfmR3lGIOOokLrhfJrGTLF8CiZMQha+SRJ7/wOLPlsH9SbjPlopyViTXCuYwbzn2tdABigkBHYXxpDV6CJZjzmRZ+FY3S/0POlTFElGojYUJ3CooWiVfyUMhdg5vSuOq0oCny53woFrf32zPH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public void testPostForm() throws Exception { + + // 公共请求参数 + Map params = new HashMap(); + params.put("app_id", appId); + params.put("method", "demo.post.json"); + params.put("format", "json"); + params.put("charset", "utf-8"); + params.put("sign_type", "RSA2"); + params.put("timestamp", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); + params.put("version", "1.0"); + + // 业务参数 + Map bizContent = new HashMap<>(); + bizContent.put("name", "葫芦娃"); + + params.put("biz_content", JSON.toJSONString(bizContent)); + + System.out.println("----------- 请求信息 -----------"); + System.out.println("请求参数:" + buildParamQuery(params)); + System.out.println("商户秘钥:" + privateKey); + String content = AlipaySignature.getSignContent(params); + System.out.println("待签名内容:" + content); + String sign = AlipaySignature.rsa256Sign(content, privateKey, "utf-8"); + System.out.println("签名(sign):" + sign); + + params.put("sign", sign); + + System.out.println("----------- 返回结果 -----------"); + String responseData = post(url, params);// 发送请求 + System.out.println(responseData); + } + +} From d42c9e9ba42cdef123c9e76e8963b0480243cc56 Mon Sep 17 00:00:00 2001 From: tanghc Date: Fri, 26 Jul 2019 12:23:04 +0800 Subject: [PATCH 2/2] 1.13.3 --- changelog.md | 4 ++++ sop-admin/sop-admin-server/pom.xml | 2 +- sop-common/pom.xml | 2 +- sop-common/sop-gateway-common/pom.xml | 4 ++-- sop-common/sop-registry-api/pom.xml | 2 +- sop-common/sop-service-common/pom.xml | 4 ++-- sop-example/sop-auth/pom.xml | 2 +- sop-example/sop-book/sop-book-web/pom.xml | 2 +- sop-example/sop-easyopen/pom.xml | 2 +- sop-example/sop-springmvc/pom.xml | 2 +- sop-example/sop-story/sop-story-web/pom.xml | 2 +- sop-gateway/pom.xml | 2 +- sop-website/website-server/pom.xml | 2 +- 13 files changed, 18 insertions(+), 14 deletions(-) diff --git a/changelog.md b/changelog.md index 2f976f0e..48bf34a5 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,9 @@ # changelog +## 1.13.3 + +- 优化参数绑定 + ## 1.13.2 - 修复json方式请求获取不到参数问题 diff --git a/sop-admin/sop-admin-server/pom.xml b/sop-admin/sop-admin-server/pom.xml index 2ee0eb82..c1e9eeb4 100644 --- a/sop-admin/sop-admin-server/pom.xml +++ b/sop-admin/sop-admin-server/pom.xml @@ -30,7 +30,7 @@ com.gitee.sop sop-registry-api - 1.13.2-SNAPSHOT + 1.13.3-SNAPSHOT diff --git a/sop-common/pom.xml b/sop-common/pom.xml index 4048a6c2..a9236684 100644 --- a/sop-common/pom.xml +++ b/sop-common/pom.xml @@ -5,7 +5,7 @@ 4.0.0 com.gitee.sop sop-common - 1.13.2-SNAPSHOT + 1.13.3-SNAPSHOT pom diff --git a/sop-common/sop-gateway-common/pom.xml b/sop-common/sop-gateway-common/pom.xml index de238aee..cfc97ff7 100644 --- a/sop-common/sop-gateway-common/pom.xml +++ b/sop-common/sop-gateway-common/pom.xml @@ -5,11 +5,11 @@ com.gitee.sop sop-common - 1.13.2-SNAPSHOT + 1.13.3-SNAPSHOT ../pom.xml sop-gateway-common - 1.13.2-SNAPSHOT + 1.13.3-SNAPSHOT jar sop-gateway-common diff --git a/sop-common/sop-registry-api/pom.xml b/sop-common/sop-registry-api/pom.xml index 2e96c9ea..1cf666bb 100644 --- a/sop-common/sop-registry-api/pom.xml +++ b/sop-common/sop-registry-api/pom.xml @@ -5,7 +5,7 @@ 4.0.0 com.gitee.sop sop-registry-api - 1.13.2-SNAPSHOT + 1.13.3-SNAPSHOT UTF-8 diff --git a/sop-common/sop-service-common/pom.xml b/sop-common/sop-service-common/pom.xml index 27d89f62..02d5ddd5 100644 --- a/sop-common/sop-service-common/pom.xml +++ b/sop-common/sop-service-common/pom.xml @@ -6,11 +6,11 @@ com.gitee.sop sop-common - 1.13.2-SNAPSHOT + 1.13.3-SNAPSHOT ../pom.xml sop-service-common - 1.13.2-SNAPSHOT + 1.13.3-SNAPSHOT jar sop-service-common diff --git a/sop-example/sop-auth/pom.xml b/sop-example/sop-auth/pom.xml index f705c20e..6b44d096 100644 --- a/sop-example/sop-auth/pom.xml +++ b/sop-example/sop-auth/pom.xml @@ -26,7 +26,7 @@ com.gitee.sop sop-service-common - 1.13.2-SNAPSHOT + 1.13.3-SNAPSHOT org.springframework.cloud diff --git a/sop-example/sop-book/sop-book-web/pom.xml b/sop-example/sop-book/sop-book-web/pom.xml index a01339ba..992a4668 100644 --- a/sop-example/sop-book/sop-book-web/pom.xml +++ b/sop-example/sop-book/sop-book-web/pom.xml @@ -24,7 +24,7 @@ com.gitee.sop sop-service-common - 1.13.2-SNAPSHOT + 1.13.3-SNAPSHOT com.gitee.sop diff --git a/sop-example/sop-easyopen/pom.xml b/sop-example/sop-easyopen/pom.xml index 9198b804..67a58e39 100644 --- a/sop-example/sop-easyopen/pom.xml +++ b/sop-example/sop-easyopen/pom.xml @@ -29,7 +29,7 @@ com.gitee.sop sop-service-common - 1.13.2-SNAPSHOT + 1.13.3-SNAPSHOT org.springframework.cloud diff --git a/sop-example/sop-springmvc/pom.xml b/sop-example/sop-springmvc/pom.xml index b7ad30f7..d840982e 100644 --- a/sop-example/sop-springmvc/pom.xml +++ b/sop-example/sop-springmvc/pom.xml @@ -20,7 +20,7 @@ com.gitee.sop sop-service-common - 1.13.2-SNAPSHOT + 1.13.3-SNAPSHOT diff --git a/sop-example/sop-story/sop-story-web/pom.xml b/sop-example/sop-story/sop-story-web/pom.xml index 1f09cd60..590fbd13 100644 --- a/sop-example/sop-story/sop-story-web/pom.xml +++ b/sop-example/sop-story/sop-story-web/pom.xml @@ -24,7 +24,7 @@ com.gitee.sop sop-service-common - 1.13.2-SNAPSHOT + 1.13.3-SNAPSHOT com.gitee.sop diff --git a/sop-gateway/pom.xml b/sop-gateway/pom.xml index cd7f594d..63f0c304 100644 --- a/sop-gateway/pom.xml +++ b/sop-gateway/pom.xml @@ -23,7 +23,7 @@ com.gitee.sop sop-gateway-common - 1.13.2-SNAPSHOT + 1.13.3-SNAPSHOT diff --git a/sop-website/website-server/pom.xml b/sop-website/website-server/pom.xml index 8e0d2c59..bd580499 100644 --- a/sop-website/website-server/pom.xml +++ b/sop-website/website-server/pom.xml @@ -25,7 +25,7 @@ com.gitee.sop sop-registry-api - 1.13.2-SNAPSHOT + 1.13.3-SNAPSHOT