-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
345 additions
and
28 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
## 介绍 | ||
|
||
我们可以通过`JWT`实现链式创建JWT对象或JWT字符串,Hutool同样提供了一些快捷方法封装在`JWTUtil`中。主要包括: | ||
|
||
- JWT创建 | ||
- JWT解析 | ||
- JWT验证 | ||
|
||
## 使用 | ||
|
||
- JWT创建 | ||
|
||
```java | ||
Map<String, Object> map = new HashMap<String, Object>() { | ||
private static final long serialVersionUID = 1L; | ||
{ | ||
put("uid", Integer.parseInt("123")); | ||
put("expire_time", System.currentTimeMillis() + 1000 * 60 * 60 * 24 * 15); | ||
} | ||
}; | ||
|
||
JWTUtil.createToken(map, "1234".getBytes()); | ||
``` | ||
|
||
- JWT解析 | ||
|
||
```java | ||
String rightToken = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9." + | ||
"eyJzdWIiOiIxMjM0NTY3ODkwIiwiYWRtaW4iOnRydWUsIm5hbWUiOiJsb29seSJ9." + | ||
"U2aQkC2THYV9L0fTN-yBBI7gmo5xhmvMhATtu8v0zEA"; | ||
|
||
final JWT jwt = JWTUtil.parseToken(rightToken); | ||
|
||
jwt.getHeader(JWTHeader.TYPE); | ||
jwt.getPayload("sub"); | ||
``` | ||
|
||
- JWT验证 | ||
|
||
```java | ||
String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9." + | ||
"eyJ1c2VyX25hbWUiOiJhZG1pbiIsInNjb3BlIjpbImFsbCJdLCJleHAiOjE2MjQwMDQ4MjIsInVzZXJJZCI6MSwiYXV0aG9yaXRpZXMiOlsiUk9MRV_op5LoibLkuozlj7ciLCJzeXNfbWVudV8xIiwiUk9MRV_op5LoibLkuIDlj7ciLCJzeXNfbWVudV8yIl0sImp0aSI6ImQ0YzVlYjgwLTA5ZTctNGU0ZC1hZTg3LTVkNGI5M2FhNmFiNiIsImNsaWVudF9pZCI6ImhhbmR5LXNob3AifQ." + | ||
"aixF1eKlAKS_k3ynFnStE7-IRGiD5YaqznvK2xEjBew"; | ||
|
||
JWTUtil.verify(token, "123456".getBytes()); | ||
``` |
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,61 @@ | ||
## 介绍 | ||
|
||
JWT签名算法比较多,主要分为非对称算法和对称算法,支持的算法定义在`SignAlgorithm`中。 | ||
|
||
### 对称签名 | ||
|
||
- HS256(HmacSHA256) | ||
- HS384(HmacSHA384) | ||
- HS512(HmacSHA512) | ||
|
||
### 非对称签名 | ||
|
||
- RS256(SHA256withRSA) | ||
- RS384(SHA384withRSA) | ||
- RS512(SHA512withRSA) | ||
|
||
- ES256(SHA256withECDSA) | ||
- ES384(SHA384withECDSA) | ||
- ES512(SHA512withECDSA) | ||
|
||
### 依赖于BounyCastle的算法 | ||
|
||
- PS256(SHA256WithRSA/PSS) | ||
- PS384(SHA384WithRSA/PSS) | ||
- PS512(SHA512WithRSA/PSS) | ||
|
||
## 使用 | ||
|
||
### 创建预定义算法签名器 | ||
|
||
`JWTSignerUtil`中预定义了一些算法的签名器的创建方法,如创建HS256的签名器: | ||
|
||
```java | ||
final JWTSigner signer = JWTSignerUtil.hs256("123456".getBytes()); | ||
JWT jwt = JWT.create().setSigner(signer); | ||
``` | ||
|
||
### 创建自定义算法签名器 | ||
|
||
通过`JWTSignerUtil.createSigner`即可通过动态传入`algorithmId`创建对应的签名器,如我们如果需要实现`ps256`算法,则首先引入`bcprov-jdk15to18`包: | ||
|
||
```xml | ||
<dependency> | ||
<groupId>org.bouncycastle</groupId> | ||
<artifactId>bcprov-jdk15to18</artifactId> | ||
<version>1.69</version> | ||
</dependency> | ||
``` | ||
|
||
再创建对应签名器即可: | ||
|
||
```java | ||
String id = "ps256"; | ||
final JWTSigner signer = JWTSignerUtil.createSigner(id, KeyUtil.generateKeyPair(AlgorithmUtil.getAlgorithm(id))); | ||
|
||
JWT jwt = JWT.create().setSigner(signer); | ||
``` | ||
|
||
### 自行实现算法签名器 | ||
|
||
`JWTSigner`接口是一个通用的签名器接口,如果想实现自定义算法,实现此接口即可。 |
Oops, something went wrong.