forked from dromara/hutool
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
优化JWT自动识别header中的算法,并可自定义header中key的顺序
- Loading branch information
Showing
4 changed files
with
55 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
hutool-jwt/src/test/java/cn/hutool/jwt/IssueI5QRUOTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |