Skip to content

Commit 30e205c

Browse files
committed
fix #175
1 parent 7d85a38 commit 30e205c

File tree

8 files changed

+26
-10
lines changed

8 files changed

+26
-10
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
<groupId>com.codingapi.springboot</groupId>
1717
<artifactId>springboot-parent</artifactId>
18-
<version>2.10.28</version>
18+
<version>2.10.29</version>
1919

2020
<url>https://github.com/codingapi/springboot-framewrok</url>
2121
<name>springboot-parent</name>

springboot-starter-data-authorization/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<artifactId>springboot-parent</artifactId>
88
<groupId>com.codingapi.springboot</groupId>
9-
<version>2.10.28</version>
9+
<version>2.10.29</version>
1010
</parent>
1111

1212
<name>springboot-starter-data-authorization</name>

springboot-starter-data-fast/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<artifactId>springboot-parent</artifactId>
77
<groupId>com.codingapi.springboot</groupId>
8-
<version>2.10.28</version>
8+
<version>2.10.29</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

springboot-starter-flow/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<artifactId>springboot-parent</artifactId>
88
<groupId>com.codingapi.springboot</groupId>
9-
<version>2.10.28</version>
9+
<version>2.10.29</version>
1010
</parent>
1111

1212
<name>springboot-starter-flow</name>

springboot-starter-security/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<artifactId>springboot-parent</artifactId>
88
<groupId>com.codingapi.springboot</groupId>
9-
<version>2.10.28</version>
9+
<version>2.10.29</version>
1010
</parent>
1111

1212
<artifactId>springboot-starter-security</artifactId>

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
5353
}
5454

5555
Token token = tokenGateway.parser(sign);
56+
if(token==null) {
57+
writeResponse(response, Response.buildFailure("token.error", "token error."));
58+
return;
59+
}
5660
if (token.canRestToken()) {
5761
Token newSign = tokenGateway.create(token.getUsername(), token.decodeIv(), token.getAuthorities(), token.getExtra());
5862
log.info("reset token ");

springboot-starter-security/src/main/java/com/codingapi/springboot/security/redis/RedisTokenGateway.java

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

33
import com.alibaba.fastjson2.JSONObject;
4+
import com.codingapi.springboot.framework.crypto.AESUtils;
45
import com.codingapi.springboot.security.gateway.Token;
6+
import lombok.SneakyThrows;
57
import org.springframework.data.redis.core.RedisTemplate;
68

79
import java.util.ArrayList;
@@ -25,7 +27,8 @@ public RedisTokenGateway(RedisTemplate<String, String> redisTemplate, SecurityRe
2527

2628
public Token create(String username, String iv, List<String> authorities, String extra) {
2729
Token token = new Token(username, iv, extra, authorities, validTime, restTime);
28-
String key = String.format("%s:%s", username, UUID.randomUUID().toString().replaceAll("-", ""));
30+
String usernameEncode = this.encodeUsername(username);
31+
String key = String.format("%s:%s", usernameEncode, UUID.randomUUID().toString().replaceAll("-", ""));
2932
token.setToken(key);
3033
redisTemplate.opsForValue().set(key, token.toJson(), validTime, TimeUnit.MILLISECONDS);
3134
return token;
@@ -69,21 +72,29 @@ public void resetToken(Token token) {
6972
* @param username 用户名
7073
*/
7174
public void removeUsername(String username) {
72-
Set<String> keys = redisTemplate.keys(username + ":*");
75+
String usernameEncode = this.encodeUsername(username);
76+
Set<String> keys = redisTemplate.keys(usernameEncode + ":*");
7377
if (!keys.isEmpty()) {
7478
redisTemplate.delete(keys);
7579
}
7680
}
7781

7882

83+
@SneakyThrows
84+
private String encodeUsername(String username){
85+
return AESUtils.getInstance().encode(username);
86+
}
87+
88+
7989
/**
8090
* 获取用户的所有token
8191
*
8292
* @param username 用户名
8393
* @return token列表
8494
*/
8595
public List<String> getTokensByUsername(String username) {
86-
Set<String> keys = redisTemplate.keys(username + ":*");
96+
String usernameEncode = this.encodeUsername(username);
97+
Set<String> keys = redisTemplate.keys(usernameEncode + ":*");
8798
return new ArrayList<>(keys);
8899
}
89100

@@ -95,7 +106,8 @@ public List<String> getTokensByUsername(String username) {
95106
* @param predicate 条件
96107
*/
97108
public void removeUsername(String username, Predicate<Token> predicate) {
98-
Set<String> keys = redisTemplate.keys(username + ":*");
109+
String usernameEncode = this.encodeUsername(username);
110+
Set<String> keys = redisTemplate.keys(usernameEncode + ":*");
99111
if (!keys.isEmpty()) {
100112
for (String key : keys) {
101113
Token token = getToken(key);

springboot-starter/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>com.codingapi.springboot</groupId>
77
<artifactId>springboot-parent</artifactId>
8-
<version>2.10.28</version>
8+
<version>2.10.29</version>
99
</parent>
1010
<artifactId>springboot-starter</artifactId>
1111

0 commit comments

Comments
 (0)