Skip to content

Commit

Permalink
优化JWT自动识别header中的算法,并可自定义header中key的顺序
Browse files Browse the repository at this point in the history
  • Loading branch information
looly committed Sep 13, 2022
1 parent d14483a commit 65c91c5
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

### 🐣新特性
* 【core 】 BooleanUtil的andOfWrap和orOfWrap()忽略null(issue#2599@Github)
* 【jwt 】 优化JWT自动识别header中的算法,并可自定义header中key的顺序(issue#I5QRUO@Gitee)
### 🐞Bug修复

-------------------------------------------------------------------------------------------------------------
Expand Down
17 changes: 14 additions & 3 deletions hutool-jwt/src/main/java/cn/hutool/jwt/JWT.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,17 @@ public JWT setCharset(Charset charset) {
}

/**
* 设置密钥,默认算法是:HS256(HmacSHA256)
* 设置密钥,如果头部指定了算法,直接使用,否则默认算法是:HS256(HmacSHA256)
*
* @param key 密钥
* @return this
*/
public JWT setKey(byte[] key) {
// 检查头信息中是否有算法信息
final String claim = (String) this.header.getClaim(JWTHeader.ALGORITHM);
if (StrUtil.isNotBlank(claim)) {
return setSigner(JWTSignerUtil.createSigner(claim, key));
}
return setSigner(JWTSignerUtil.hs256(key));
}

Expand Down Expand Up @@ -309,9 +314,15 @@ public String sign() {
public String sign(JWTSigner signer) {
Assert.notNull(signer, () -> new JWTException("No Signer provided!"));

// 检查tye信息
final String type = (String) this.header.getClaim(JWTHeader.TYPE);
if (StrUtil.isBlank(type)) {
this.header.setClaim(JWTHeader.TYPE, "JWT");
}

// 检查头信息中是否有算法信息
final String claim = (String) this.header.getClaim(JWTHeader.ALGORITHM);
if (StrUtil.isBlank(claim)) {
final String algorithm = (String) this.header.getClaim(JWTHeader.ALGORITHM);
if (StrUtil.isBlank(algorithm)) {
this.header.setClaim(JWTHeader.ALGORITHM,
AlgorithmUtil.getId(signer.getAlgorithm()));
}
Expand Down
4 changes: 1 addition & 3 deletions hutool-jwt/src/main/java/cn/hutool/jwt/JWTHeader.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ public class JWTHeader extends Claims {
/**
* 构造,初始化默认(typ=JWT)
*/
public JWTHeader() {
setClaim(TYPE, "JWT");
}
public JWTHeader() {}

/**
* 增加“kid”头信息
Expand Down
39 changes: 39 additions & 0 deletions hutool-jwt/src/test/java/cn/hutool/jwt/IssueI5QRUOTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package cn.hutool.jwt;

import org.junit.Assert;
import org.junit.Test;

import java.util.LinkedHashMap;
import java.util.Map;

public class IssueI5QRUOTest {

@Test
public void createTokenTest(){
// https://jwt.io/

// 自定义header顺序
final Map<String, Object> header = new LinkedHashMap<String, Object>(){
{
put(JWTHeader.ALGORITHM, "HS384");
put(JWTHeader.TYPE, "JWT");
}
};

final Map<String, Object> payload = new LinkedHashMap<String, Object>(){
{
put("sub", "1234567890");
put("name", "John Doe");
put("iat", 1516239022);
}
};

final String token = JWTUtil.createToken(header, payload, "123456".getBytes());
Assert.assertEquals("eyJhbGciOiJIUzM4NCIsInR5cCI6IkpXVCJ9." +
"eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ." +
"3Ywq9NlR3cBST4nfcdbR-fcZ8374RHzU50X6flKvG-tnWFMalMaHRm3cMpXs1NrZ", token);

final boolean verify = JWT.of(token).setKey("123456".getBytes()).verify();
Assert.assertTrue(verify);
}
}

0 comments on commit 65c91c5

Please sign in to comment.