Skip to content

重载生成 token 的相关方法,让方法的 identity 参数支持传入 String 类型 #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions core/src/main/java/io/github/talelin/core/token/DoubleJWT.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* access、refresh token
*
* @author pedro@TaleLin
* @author colorful@TaleLin
*/
public class DoubleJWT {

Expand Down Expand Up @@ -72,6 +73,16 @@ public String generateToken(String tokenType, long identity, String scope, long
.sign(algorithm);
}

public String generateToken(String tokenType, String identity, String scope, long expire) {
Date expireDate = DateUtil.getDurationDate(expire);
return builder
.withClaim("type", tokenType)
.withClaim("identity", identity)
.withClaim("scope", scope)
.withExpiresAt(expireDate)
.sign(algorithm);
}

public Map<String, Claim> decodeAccessToken(String token) {
DecodedJWT jwt = accessVerifier.verify(token);
checkTokenExpired(jwt.getExpiresAt());
Expand Down Expand Up @@ -111,16 +122,30 @@ public String generateAccessToken(long identity) {
return generateToken(TokenConstant.ACCESS_TYPE, identity, TokenConstant.LIN_SCOPE, this.accessExpire);
}

public String generateAccessToken(String identity) {
return generateToken(TokenConstant.ACCESS_TYPE, identity, TokenConstant.LIN_SCOPE, this.accessExpire);
}

public String generateRefreshToken(long identity) {
return generateToken(TokenConstant.REFRESH_TYPE, identity, TokenConstant.LIN_SCOPE, this.refreshExpire);
}

public String generateRefreshToken(String identity) {
return generateToken(TokenConstant.REFRESH_TYPE, identity, TokenConstant.LIN_SCOPE, this.refreshExpire);
}

public Tokens generateTokens(long identity) {
String access = this.generateToken(TokenConstant.ACCESS_TYPE, identity, TokenConstant.LIN_SCOPE, this.accessExpire);
String refresh = this.generateToken(TokenConstant.REFRESH_TYPE, identity, TokenConstant.LIN_SCOPE, this.refreshExpire);
return new Tokens(access, refresh);
}

public Tokens generateTokens(String identity) {
String access = this.generateToken(TokenConstant.ACCESS_TYPE, identity, TokenConstant.LIN_SCOPE, this.accessExpire);
String refresh = this.generateToken(TokenConstant.REFRESH_TYPE, identity, TokenConstant.LIN_SCOPE, this.refreshExpire);
return new Tokens(access, refresh);
}


/***
* 获得令牌的验证器
Expand Down
36 changes: 36 additions & 0 deletions core/src/test/java/io/github/talelin/core/token/DoubleJWTTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,21 @@ public void decodeAccessToken1() throws InterruptedException {
}
}

@Test
public void decodeAccessToken2() throws InterruptedException {
DoubleJWT jwt = new DoubleJWT("secret", 1000, 2000);
String token = jwt.generateAccessToken("Colorful");
assertNotNull(token);
log.info(token);
Thread.sleep(1000);
try {
Map<String, Claim> claimMap = jwt.decodeAccessToken(token);
System.out.println(claimMap);
} catch (JWTVerificationException e) {
assertEquals("token is expired", e.getMessage());
}
}

@Test
public void decodeRefreshToken1() throws InterruptedException {
DoubleJWT jwt = new DoubleJWT("secret", 1000, 2000);
Expand Down Expand Up @@ -105,6 +120,14 @@ public void generateRefreshToken() {
log.info(token);
}

@Test
public void generateRefreshToken1() {
DoubleJWT jwt = new DoubleJWT("secret", 1000, 2000);
String token = jwt.generateRefreshToken("Colorful");
assertNotNull(token);
log.info(token);
}

@Test
public void generateTokens() {
DoubleJWT jwt = new DoubleJWT("secret", 10000, 20000);
Expand All @@ -118,6 +141,19 @@ public void generateTokens() {
Assert.assertEquals(TokenConstant.ACCESS_TYPE, claimMap.get("type").asString());
}

@Test
public void generateTokens1() {
DoubleJWT jwt = new DoubleJWT("secret", 10000, 20000);
Tokens tokens = jwt.generateTokens("Colorful");
assertNotNull(tokens.getAccessToken());
assertNotNull(tokens.getRefreshToken());
log.info("{}", tokens);

Map<String, Claim> claimMap = jwt.decodeAccessToken(tokens.getAccessToken());
Assert.assertEquals(TokenConstant.LIN_SCOPE, claimMap.get("scope").asString());
Assert.assertEquals(TokenConstant.ACCESS_TYPE, claimMap.get("type").asString());
}

@Test
public void getAccessVerifier() {
DoubleJWT jwt = new DoubleJWT("secret", 1000, 2000);
Expand Down