Skip to content

Commit 288bc22

Browse files
committed
add Token extra filed
1 parent 67de72d commit 288bc22

File tree

5 files changed

+85
-15
lines changed

5 files changed

+85
-15
lines changed

springboot-starter-security-jwt/src/main/java/com/codingapi/springboot/security/filter/MyAuthenticationFilter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
4343

4444
Token token = jwt.parser(sign);
4545
if (token.canRestToken()) {
46-
Token newSign = jwt.create(token.getUsername(), token.getDecodeIv(), token.getAuthorities());
46+
Token newSign = jwt.create(token.getUsername(), token.decodeIv(), token.getAuthorities(),token.getExtra());
4747
log.info("reset token ");
4848
response.setHeader(TOKEN_KEY, newSign.getToken());
4949
}

springboot-starter-security-jwt/src/main/java/com/codingapi/springboot/security/jwt/Jwt.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,20 @@ public Jwt(String secretKey, int jwtTime, int jwtRestTime) {
2323
this.jwtRestTime = jwtRestTime;
2424
}
2525

26+
public Token create(String username, List<String> authorities,String extra){
27+
return create(username, null,authorities, extra);
28+
}
29+
30+
public Token create(String username, List<String> authorities){
31+
return create(username, null,authorities, null);
32+
}
33+
2634
public Token create(String username, String iv, List<String> authorities){
27-
Token token = new Token(username, iv, authorities, jwtTime, jwtRestTime);
35+
return create(username, iv,authorities, null);
36+
}
37+
38+
public Token create(String username, String iv,List<String> authorities,String extra){
39+
Token token = new Token(username, iv,extra, authorities, jwtTime, jwtRestTime);
2840
String jwt = Jwts.builder().setSubject(token.toJson()).signWith(key).compact();
2941
token.setToken(jwt);
3042
return token;

springboot-starter-security-jwt/src/main/java/com/codingapi/springboot/security/jwt/Token.java

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package com.codingapi.springboot.security.jwt;
22

3+
import com.alibaba.fastjson.JSONObject;
34
import com.codingapi.springboot.framework.serializable.JsonSerializable;
45
import com.codingapi.springboot.security.crypto.MyAES;
56
import com.codingapi.springboot.security.exception.TokenExpiredException;
67
import lombok.Getter;
8+
import lombok.NoArgsConstructor;
79
import lombok.Setter;
810
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
911
import org.springframework.security.core.authority.SimpleGrantedAuthority;
@@ -15,22 +17,25 @@
1517

1618
@Getter
1719
@Setter
20+
@NoArgsConstructor
1821
public class Token implements JsonSerializable {
1922

2023
private String username;
24+
private String extra;
2125
private String iv;
26+
2227
private String token;
2328
private List<String> authorities;
2429
private long expireTime;
2530
private long remindTime;
2631

2732

28-
public Token() {
29-
}
30-
31-
public Token(String username, String iv, List<String> authorities, int expireValue, int remindValue){
33+
public Token(String username, String iv,String extra, List<String> authorities, int expireValue, int remindValue){
3234
this.username = username;
33-
this.iv = MyAES.getInstance().encode(iv);
35+
this.extra = extra;
36+
if(iv!=null) {
37+
this.iv = MyAES.getInstance().encode(iv);
38+
}
3439
this.authorities = authorities;
3540
this.expireTime = System.currentTimeMillis() + expireValue;
3641
this.remindTime = System.currentTimeMillis() + remindValue;
@@ -47,16 +52,22 @@ public boolean isExpire() {
4752
return expireTime <= System.currentTimeMillis();
4853
}
4954

50-
51-
public String getIv() {
52-
return iv;
55+
public String decodeIv(){
56+
if(iv==null){
57+
return null;
58+
}
59+
return MyAES.getInstance().decode(iv);
5360
}
5461

55-
@Transient
56-
public String getDecodeIv(){
57-
return MyAES.getInstance().decode(iv);
62+
63+
public <T> T parseExtra(Class<T> clazz){
64+
if(extra==null){
65+
return null;
66+
}
67+
return JSONObject.parseObject(extra,clazz);
5868
}
5969

70+
6071
public boolean canRestToken() {
6172
return !isExpire() && remindTime <= System.currentTimeMillis();
6273
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.codingapi.springboot.security.jwt;
2+
3+
import com.codingapi.springboot.framework.serializable.JsonSerializable;
4+
import lombok.AllArgsConstructor;
5+
import lombok.Getter;
6+
import lombok.NoArgsConstructor;
7+
import lombok.Setter;
8+
9+
@Setter
10+
@Getter
11+
@AllArgsConstructor
12+
@NoArgsConstructor
13+
public class TestVO implements JsonSerializable {
14+
15+
private String name;
16+
}

springboot-starter-security-jwt/src/test/java/com/codingapi/springboot/security/jwt/TokenTest.java

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class TokenTest {
1717
private Jwt jwt;
1818

1919
@Test
20-
void verify() throws TokenExpiredException {
20+
void verify1() throws TokenExpiredException {
2121
String username = "admin";
2222
String iv = "123456";
2323
List<String> authorities = Collections.singletonList("ADMIN");
@@ -26,6 +26,37 @@ void verify() throws TokenExpiredException {
2626
token.verify();
2727

2828
Token data = jwt.parser(token.getToken());
29-
assertEquals(data.getDecodeIv(),iv);
29+
assertEquals(data.decodeIv(),iv);
30+
assertEquals(data.getAuthorities(),authorities);
31+
}
32+
33+
@Test
34+
void verify2() throws TokenExpiredException {
35+
String username = "admin";
36+
List<String> authorities = Collections.singletonList("ADMIN");
37+
38+
Token token =jwt.create(username,authorities);
39+
token.verify();
40+
41+
Token data = jwt.parser(token.getToken());
42+
assertEquals(data.getUsername(),username);
43+
assertEquals(data.getAuthorities(),authorities);
44+
}
45+
46+
47+
48+
@Test
49+
void verify3() throws TokenExpiredException {
50+
String username = "admin";
51+
TestVO testVO = new TestVO("123");
52+
String extra = testVO.toJson();
53+
List<String> authorities = Collections.singletonList("ADMIN");
54+
55+
Token token =jwt.create(username,authorities,extra);
56+
token.verify();
57+
58+
Token data = jwt.parser(token.getToken());
59+
assertEquals(data.parseExtra(TestVO.class).getName(), testVO.getName());
60+
assertEquals(data.getAuthorities(),authorities);
3061
}
3162
}

0 commit comments

Comments
 (0)