From e58c8c9d6c07ce40e6b76ad9ec350e2fb7e53ac0 Mon Sep 17 00:00:00 2001 From: Looly Date: Sat, 6 Nov 2021 01:17:08 +0800 Subject: [PATCH] add method --- CHANGELOG.md | 1 + .../main/java/cn/hutool/crypto/SmUtil.java | 7 +- .../main/java/cn/hutool/http/HttpRequest.java | 66 ++++++++++++++----- 3 files changed, 53 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 80eb45a5a..7f8bd61f0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ * 【core 】 CollUtil增加safeContains方法(pr#1926@Github) * 【core 】 ActualTypeMapperPool增加getStrKeyMap方法(pr#447@Gitee) * 【core 】 TreeUtil增加walk方法(pr#1932@Gitee) +* 【crypto 】 SmUtil增加sm3WithSalt(pr#454@Gitee) ### 🐞Bug修复 * 【core 】 修复UrlBuilder.addPath歧义问题(issue#1912@Github) diff --git a/hutool-crypto/src/main/java/cn/hutool/crypto/SmUtil.java b/hutool-crypto/src/main/java/cn/hutool/crypto/SmUtil.java index 7473ecbed..6aada8c7f 100644 --- a/hutool-crypto/src/main/java/cn/hutool/crypto/SmUtil.java +++ b/hutool-crypto/src/main/java/cn/hutool/crypto/SmUtil.java @@ -54,7 +54,7 @@ public class SmUtil { public static final ECDomainParameters SM2_DOMAIN_PARAMS = BCUtil.toDomainParams(GMNamedCurves.getByName(SM2_CURVE_NAME)); /** * SM2国密算法公钥参数的Oid标识 - */ + */ public static final ASN1ObjectIdentifier ID_SM2_PUBLIC_KEY_PARAM = new ASN1ObjectIdentifier("1.2.156.10197.1.301"); /** @@ -134,12 +134,13 @@ public class SmUtil { } /** - * SM3加密,可以传入盐
+ * SM3加密,可以传入盐 * * @param salt 加密盐 * @return {@link SM3} + * @since 5.7.16 */ - public static SM3 sm3(byte[] salt) { + public static SM3 sm3WithSalt(byte[] salt) { return new SM3(salt); } diff --git a/hutool-http/src/main/java/cn/hutool/http/HttpRequest.java b/hutool-http/src/main/java/cn/hutool/http/HttpRequest.java index 074d642e2..d5750e000 100644 --- a/hutool-http/src/main/java/cn/hutool/http/HttpRequest.java +++ b/hutool-http/src/main/java/cn/hutool/http/HttpRequest.java @@ -147,6 +147,11 @@ public class HttpRequest extends HttpBase { */ private SSLSocketFactory ssf; + /** + * 请求前的处理器,用于在请求前重新编辑请求,类似于拦截器 + */ + private Consumer consumer; + /** * 构造,URL编码默认使用UTF-8 * @@ -919,6 +924,16 @@ public class HttpRequest extends HttpBase { return this; } + /** + * 设置请求前的处理器,用于在请求前重新编辑请求,类似于拦截器 + * + * @param consumer 请求前的处理器,用于在请求前重新编辑请求,类似于拦截器 + * @since 5.7.16 + */ + public void setConsumer(Consumer consumer) { + this.consumer = consumer; + } + /** * 执行Reuqest请求 * @@ -949,22 +964,7 @@ public class HttpRequest extends HttpBase { * @return this */ public HttpResponse execute(boolean isAsync) { - // 初始化URL - urlWithParamIfGet(); - // 初始化 connection - initConnection(); - // 发送请求 - send(); - - // 手动实现重定向 - HttpResponse httpResponse = sendRedirectIfPossible(); - - // 获取响应 - if (null == httpResponse) { - httpResponse = new HttpResponse(this.httpConnection, this.charset, isAsync, isIgnoreResponseBody()); - } - - return httpResponse; + return doExecute(isAsync, this.consumer); } /** @@ -1054,6 +1054,35 @@ public class HttpRequest extends HttpBase { // ---------------------------------------------------------------- Private method start + /** + * 执行Reuqest请求 + * + * @param isAsync 是否异步 + * @return this + */ + private HttpResponse doExecute(boolean isAsync, Consumer consumer) { + if (null != consumer) { + consumer.accept(this); + } + + // 初始化URL + urlWithParamIfGet(); + // 初始化 connection + initConnection(); + // 发送请求 + send(); + + // 手动实现重定向 + HttpResponse httpResponse = sendRedirectIfPossible(isAsync); + + // 获取响应 + if (null == httpResponse) { + httpResponse = new HttpResponse(this.httpConnection, this.charset, isAsync, isIgnoreResponseBody()); + } + + return httpResponse; + } + /** * 初始化网络连接 */ @@ -1108,9 +1137,10 @@ public class HttpRequest extends HttpBase { /** * 调用转发,如果需要转发返回转发结果,否则返回{@code null} * + * @param isAsync 是否异步 * @return {@link HttpResponse},无转发返回 {@code null} */ - private HttpResponse sendRedirectIfPossible() { + private HttpResponse sendRedirectIfPossible(boolean isAsync) { if (this.maxRedirectCount < 1) { // 不重定向 return null; @@ -1132,7 +1162,7 @@ public class HttpRequest extends HttpBase { setUrl(httpConnection.header(Header.LOCATION)); if (redirectCount < this.maxRedirectCount) { redirectCount++; - return execute(); + return doExecute(isAsync, null); } } }