mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
add /GlobalCustomFormat
This commit is contained in:
@@ -3,6 +3,7 @@ package cn.hutool.jwt;
|
||||
import cn.hutool.core.codec.Base64;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.json.JSONConfig;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
|
||||
@@ -19,11 +20,9 @@ import java.util.Map;
|
||||
public class Claims implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private JSONObject claimJSON;
|
||||
private final JSONConfig CONFIG = JSONConfig.create().setDateFormat("#sss").setOrder(true);
|
||||
|
||||
public Claims() {
|
||||
this.claimJSON = new JSONObject();
|
||||
}
|
||||
private JSONObject claimJSON;
|
||||
|
||||
/**
|
||||
* 增加Claims属性,如果属性值为{@code null},则移除这个属性
|
||||
@@ -32,6 +31,7 @@ public class Claims implements Serializable {
|
||||
* @param value 属性值
|
||||
*/
|
||||
protected void setClaim(String name, Object value) {
|
||||
init();
|
||||
Assert.notNull(name, "Name must be not null!");
|
||||
if (value == null) {
|
||||
claimJSON.remove(name);
|
||||
@@ -59,6 +59,7 @@ public class Claims implements Serializable {
|
||||
* @return 属性
|
||||
*/
|
||||
public Object getClaim(String name) {
|
||||
init();
|
||||
return this.claimJSON.getObj(name);
|
||||
}
|
||||
|
||||
@@ -68,6 +69,7 @@ public class Claims implements Serializable {
|
||||
* @return JSON字符串
|
||||
*/
|
||||
public JSONObject getClaimsJson() {
|
||||
init();
|
||||
return this.claimJSON;
|
||||
}
|
||||
|
||||
@@ -78,11 +80,18 @@ public class Claims implements Serializable {
|
||||
* @param charset 编码
|
||||
*/
|
||||
public void parse(String tokenPart, Charset charset) {
|
||||
this.claimJSON = JSONUtil.parseObj(Base64.decodeStr(tokenPart, charset));
|
||||
this.claimJSON = JSONUtil.parseObj(Base64.decodeStr(tokenPart, charset), CONFIG);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
init();
|
||||
return this.claimJSON.toString();
|
||||
}
|
||||
|
||||
private void init(){
|
||||
if(null == this.claimJSON){
|
||||
this.claimJSON = new JSONObject(CONFIG);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,5 +1,6 @@
|
||||
package cn.hutool.jwt;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.crypto.KeyUtil;
|
||||
import cn.hutool.jwt.signers.AlgorithmUtil;
|
||||
import cn.hutool.jwt.signers.JWTSigner;
|
||||
@@ -113,6 +114,7 @@ public class JWTSignerTest {
|
||||
.setPayload("sub", "1234567890")
|
||||
.setPayload("name", "looly")
|
||||
.setPayload("admin", true)
|
||||
.setExpiresAt(DateUtil.tomorrow())
|
||||
.setSigner(signer);
|
||||
|
||||
String token = jwt.sign();
|
||||
|
@@ -1,5 +1,6 @@
|
||||
package cn.hutool.jwt;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.jwt.signers.JWTSignerUtil;
|
||||
import org.junit.Assert;
|
||||
@@ -14,11 +15,12 @@ public class JWTTest {
|
||||
.setPayload("sub", "1234567890")
|
||||
.setPayload("name", "looly")
|
||||
.setPayload("admin", true)
|
||||
.setPayload(RegisteredPayload.EXPIRES_AT, DateUtil.parse("2022-01-01"))
|
||||
.setKey(key);
|
||||
|
||||
String rightToken = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9." +
|
||||
"eyJzdWIiOiIxMjM0NTY3ODkwIiwiYWRtaW4iOnRydWUsIm5hbWUiOiJsb29seSJ9." +
|
||||
"U2aQkC2THYV9L0fTN-yBBI7gmo5xhmvMhATtu8v0zEA";
|
||||
"eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6Imxvb2x5IiwiYWRtaW4iOnRydWUsImV4cCI6IjE2NDA5NjY0MDAifQ." +
|
||||
"c9qo9Z9vdN2gulvOEReU9iEi0bqgyVqjaNKbP1DmTL4";
|
||||
|
||||
String token = jwt.sign();
|
||||
Assert.assertEquals(token, rightToken);
|
||||
|
Reference in New Issue
Block a user