diff --git a/CHANGELOG.md b/CHANGELOG.md index 5221d1ce9..ceda3180f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ ------------------------------------------------------------------------------------------------------------- -# 5.7.2 (2021-06-18) +# 5.7.2 (2021-06-19) ### 🐣新特性 * 【core 】 增加UserPassAuthenticator @@ -18,6 +18,7 @@ * 【core 】 修复ConcurrencyTester重复使用时开始测试未清空之前任务的问题(issue#I3VSDO@Gitee) * 【poi 】 修复使用BigWriter写出,ExcelWriter修改单元格值失败的问题(issue#I3VSDO@Gitee) * 【jwt 】 修复Hmac算法下生成签名是hex的问题(issue#I3W6IP@Gitee) +* 【jwt 】 修复TreeUtil.build中deep失效问题(issue#1661@Github) ------------------------------------------------------------------------------------------------------------- diff --git a/hutool-all/src/main/java/cn/hutool/Hutool.java b/hutool-all/src/main/java/cn/hutool/Hutool.java index 4c17cf16e..1b1b9749a 100644 --- a/hutool-all/src/main/java/cn/hutool/Hutool.java +++ b/hutool-all/src/main/java/cn/hutool/Hutool.java @@ -23,6 +23,11 @@ import cn.hutool.core.util.StrUtil; import java.util.Set; /** + *
+ * 秋千水,竹马道,一眼见你,万物不及。
+ * 春水生,春林初胜,春风十里不如你。
+ *
* Hutool是一个小而全的Java工具类库,通过静态方法封装,降低相关API的学习成本,提高工作效率,使Java拥有函数式语言般的优雅,让Java语言也可以“甜甜的”。 *
diff --git a/hutool-core/src/main/java/cn/hutool/core/convert/ConverterRegistry.java b/hutool-core/src/main/java/cn/hutool/core/convert/ConverterRegistry.java index 6800a9213..2475cb022 100644 --- a/hutool-core/src/main/java/cn/hutool/core/convert/ConverterRegistry.java +++ b/hutool-core/src/main/java/cn/hutool/core/convert/ConverterRegistry.java @@ -118,6 +118,9 @@ public class ConverterRegistry implements Serializable { return SingletonHolder.INSTANCE; } + /** + * 构造 + */ public ConverterRegistry() { defaultConverter(); putCustomBySpi(); diff --git a/hutool-core/src/main/java/cn/hutool/core/date/DateTime.java b/hutool-core/src/main/java/cn/hutool/core/date/DateTime.java index 190071050..29f06a07f 100644 --- a/hutool-core/src/main/java/cn/hutool/core/date/DateTime.java +++ b/hutool-core/src/main/java/cn/hutool/core/date/DateTime.java @@ -724,8 +724,8 @@ public class DateTime extends Date { * 当前日期是否在日期指定范围内* -用户管理 - * --用户添加 - * --用户管理 + * -用户管理 + * +用户添加 * - 部门管理 - * --部门添加 - * --部门管理 + * -部门管理 + * +部门添加 ** * @return 树列表 */ public List
- * 结构:xxxxx.yyyyy.zzzzz + * 结构:header.payload.signature *
@@ -32,8 +33,9 @@ import java.util.Map; *
* * @author looly + * @since 5.7.0 */ -public class JWT { +public class JWT implements RegisteredPayload+ * 如果某个时间没有设置,则不检查(表示无限制) + * + * @param dateToCheck 被检查的时间,一般为当前时间 + * @return this + * @throws ValidateException 验证失败的异常 + */ + public JWTValidator validateDate(Date dateToCheck) throws ValidateException { + validateDate(this.jwt.getPayload(), dateToCheck); + return this; + } + + /** + * 验证算法 + * + * @param jwt {@link JWT}对象 + * @param signer 用于验证的签名器 + * @throws ValidateException 验证异常 + */ + private static void validateAlgorithm(JWT jwt, JWTSigner signer) throws ValidateException { + final String algorithmId = jwt.getAlgorithm(); + if (null == signer) { + signer = jwt.getSigner(); + } + + if (StrUtil.isEmpty(algorithmId)) { + // 可能无签名 + if (null == signer || signer instanceof NoneJWTSigner) { + return; + } + throw new ValidateException("No algorithm defined in header!"); + } + + if (null == signer) { + throw new IllegalArgumentException("No Signer for validate algorithm!"); + } + + final String algorithmIdInSigner = signer.getAlgorithmId(); + if (false == StrUtil.equals(algorithmId, algorithmIdInSigner)) { + throw new ValidateException("Algorithm [{}] defined in header doesn't match to [{}]!" + , algorithmId, algorithmIdInSigner); + } + + // 通过算法验证签名是否正确 + if (false == jwt.verify(signer)) { + throw new ValidateException("Signature verification failed!"); + } + } + + /** + * 检查JWT的以下三两个时间: + * + *
+ * 如果某个时间没有设置,则不检查(表示无限制)
+ *
+ * @param payload {@link JWTPayload}
+ * @param dateToCheck 被检查的时间,一般为当前时间
+ * @throws ValidateException 验证异常
+ */
+ private static void validateDate(JWTPayload payload, Date dateToCheck) throws ValidateException {
+ if (null == dateToCheck) {
+ // 默认当前时间
+ dateToCheck = DateUtil.date();
+ }
+
+ // 检查生效时间(被检查时间必须晚于生效时间)
+ final Date notBefore = payload.getClaimsJson().getDate(JWTPayload.NOT_BEFORE);
+ if (null != notBefore && dateToCheck.before(notBefore)) {
+ throw new ValidateException("Current date [{}] is before 'nbf' [{}]",
+ dateToCheck, DateUtil.date(notBefore));
+ }
+
+ // 检查失效时间(被检查时间必须早于失效时间)
+ final Date expiresAt = payload.getClaimsJson().getDate(JWTPayload.EXPIRES_AT);
+ if (null != expiresAt && dateToCheck.after(expiresAt)) {
+ throw new ValidateException("Current date [{}] is after 'exp' [{}]",
+ dateToCheck, DateUtil.date(expiresAt));
+ }
+
+ // 检查签发时间(被检查时间必须晚于签发时间)
+ final Date issueAt = payload.getClaimsJson().getDate(JWTPayload.ISSUED_AT);
+ if (null != issueAt && dateToCheck.before(issueAt)) {
+ throw new ValidateException("Current date [{}] is before 'iat' [{}]",
+ dateToCheck, DateUtil.date(issueAt));
+ }
+ }
+}
diff --git a/hutool-jwt/src/main/java/cn/hutool/jwt/RegisteredPayload.java b/hutool-jwt/src/main/java/cn/hutool/jwt/RegisteredPayload.java
new file mode 100644
index 000000000..f0c0fc8e0
--- /dev/null
+++ b/hutool-jwt/src/main/java/cn/hutool/jwt/RegisteredPayload.java
@@ -0,0 +1,122 @@
+package cn.hutool.jwt;
+
+import java.util.Date;
+
+/**
+ * 注册的标准载荷(Payload)声明
+ *
+ * @param