mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:37:59 +08:00
add test
This commit is contained in:
parent
a717d03420
commit
2cdce52d4a
@ -4,6 +4,7 @@ import cn.hutool.core.convert.AbstractConverter;
|
||||
import cn.hutool.core.convert.ConvertException;
|
||||
import cn.hutool.core.date.DateTime;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.date.format.GlobalCustomFormat;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
|
||||
import java.time.temporal.TemporalAccessor;
|
||||
@ -120,6 +121,11 @@ public class DateConverter extends AbstractConverter<java.util.Date> {
|
||||
* @return 目标类型对象
|
||||
*/
|
||||
private java.util.Date wrap(long mills) {
|
||||
if(GlobalCustomFormat.FORMAT_SECONDS.equals(this.format)){
|
||||
// Unix时间戳
|
||||
return DateUtil.date(mills * 1000);
|
||||
}
|
||||
|
||||
// 返回指定类型
|
||||
if (java.util.Date.class == targetType) {
|
||||
return new java.util.Date(mills);
|
||||
|
@ -5,6 +5,7 @@ import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.core.convert.ConvertException;
|
||||
import cn.hutool.core.date.DateTime;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.date.format.GlobalCustomFormat;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
|
||||
@ -177,7 +178,16 @@ public class TemporalAccessorConverter extends AbstractConverter<TemporalAccesso
|
||||
} else if(Era.class.equals(this.targetType)){
|
||||
return IsoEra.of(Math.toIntExact(time));
|
||||
}
|
||||
return parseFromInstant(Instant.ofEpochMilli(time), null);
|
||||
|
||||
final Instant instant;
|
||||
if(GlobalCustomFormat.FORMAT_SECONDS.equals(this.format)){
|
||||
// https://gitee.com/dromara/hutool/issues/I6IS5B
|
||||
// Unix时间戳
|
||||
instant = Instant.ofEpochSecond(time);
|
||||
}else{
|
||||
instant = Instant.ofEpochMilli(time);
|
||||
}
|
||||
return parseFromInstant(instant, null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -18,7 +18,13 @@ import java.util.function.Function;
|
||||
*/
|
||||
public class GlobalCustomFormat {
|
||||
|
||||
/**
|
||||
* 格式:秒时间戳(Unix时间戳)
|
||||
*/
|
||||
public static final String FORMAT_SECONDS = "#sss";
|
||||
/**
|
||||
* 格式:毫秒时间戳
|
||||
*/
|
||||
public static final String FORMAT_MILLISECONDS = "#SSS";
|
||||
|
||||
private static final Map<CharSequence, Function<Date, String>> formatterMap;
|
||||
|
@ -1,6 +1,7 @@
|
||||
package cn.hutool.jwt;
|
||||
|
||||
import cn.hutool.core.codec.Base64;
|
||||
import cn.hutool.core.date.format.GlobalCustomFormat;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.json.JSONConfig;
|
||||
@ -21,7 +22,7 @@ public class Claims implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
// 时间使用秒级时间戳表示
|
||||
private final JSONConfig CONFIG = JSONConfig.create().setDateFormat("#sss");
|
||||
private final JSONConfig CONFIG = JSONConfig.create().setDateFormat(GlobalCustomFormat.FORMAT_SECONDS);
|
||||
|
||||
private JSONObject claimJSON;
|
||||
|
||||
@ -43,9 +44,10 @@ public class Claims implements Serializable {
|
||||
|
||||
/**
|
||||
* 加入多个Claims属性
|
||||
*
|
||||
* @param headerClaims 多个Claims属性
|
||||
*/
|
||||
protected void putAll(Map<String, ?> headerClaims){
|
||||
protected void putAll(Map<String, ?> headerClaims) {
|
||||
if (MapUtil.isNotEmpty(headerClaims)) {
|
||||
for (Map.Entry<String, ?> entry : headerClaims.entrySet()) {
|
||||
setClaim(entry.getKey(), entry.getValue());
|
||||
@ -90,8 +92,8 @@ public class Claims implements Serializable {
|
||||
return this.claimJSON.toString();
|
||||
}
|
||||
|
||||
private void init(){
|
||||
if(null == this.claimJSON){
|
||||
private void init() {
|
||||
if (null == this.claimJSON) {
|
||||
this.claimJSON = new JSONObject(CONFIG);
|
||||
}
|
||||
}
|
||||
|
51
hutool-jwt/src/test/java/cn/hutool/jwt/IssueI6IS5BTest.java
Normal file
51
hutool-jwt/src/test/java/cn/hutool/jwt/IssueI6IS5BTest.java
Normal file
@ -0,0 +1,51 @@
|
||||
package cn.hutool.jwt;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.date.LocalDateTimeUtil;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import lombok.Data;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Date;
|
||||
|
||||
public class IssueI6IS5BTest {
|
||||
@Test
|
||||
public void payloadToBeanTest() {
|
||||
final LocalDateTime iat = LocalDateTimeUtil.of(DateUtil.parse("2023-03-03"));
|
||||
final JwtToken jwtToken = new JwtToken();
|
||||
jwtToken.setIat(iat);
|
||||
final String token = JWTUtil.createToken(JSONUtil.parseObj(jwtToken), "123".getBytes(StandardCharsets.UTF_8));
|
||||
Assert.assertEquals("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE2Nzc3NzI4MDB9.SXU_mm1wT5lNoK-Dq5Y8f3BItv_44zuAlyeWLqajpXg", token);
|
||||
final JSONObject payloads = JWTUtil.parseToken(token).getPayloads();
|
||||
Assert.assertEquals("{\"iat\":1677772800}", payloads.toString());
|
||||
final JwtToken o = payloads.toBean(JwtToken.class);
|
||||
Assert.assertEquals(iat, o.getIat());
|
||||
}
|
||||
|
||||
@Data
|
||||
static class JwtToken {
|
||||
private LocalDateTime iat;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void payloadToBeanTest2() {
|
||||
final Date iat = DateUtil.parse("2023-03-03");
|
||||
final JwtToken2 jwtToken = new JwtToken2();
|
||||
jwtToken.setIat(iat);
|
||||
final String token = JWTUtil.createToken(JSONUtil.parseObj(jwtToken), "123".getBytes(StandardCharsets.UTF_8));
|
||||
Assert.assertEquals("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE2Nzc3NzI4MDB9.SXU_mm1wT5lNoK-Dq5Y8f3BItv_44zuAlyeWLqajpXg", token);
|
||||
final JSONObject payloads = JWTUtil.parseToken(token).getPayloads();
|
||||
Assert.assertEquals("{\"iat\":1677772800}", payloads.toString());
|
||||
final JwtToken2 o = payloads.toBean(JwtToken2.class);
|
||||
Assert.assertEquals(iat, o.getIat());
|
||||
}
|
||||
|
||||
@Data
|
||||
static class JwtToken2 {
|
||||
private Date iat;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user