Skip to content

Commit 327e542

Browse files
committed
add #40
1 parent 2e9092b commit 327e542

17 files changed

+166
-48
lines changed

springboot-starter-security/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@
3030
<artifactId>spring-boot-starter-web</artifactId>
3131
</dependency>
3232

33+
<dependency>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-starter-data-redis</artifactId>
36+
<scope>provided</scope>
37+
</dependency>
38+
3339
<dependency>
3440
<groupId>io.jsonwebtoken</groupId>
3541
<artifactId>jjwt-api</artifactId>

springboot-starter-security/src/main/java/com/codingapi/springboot/security/jwt/MyAES.java renamed to springboot-starter-security/src/main/java/com/codingapi/springboot/security/crypto/AESTools.java

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

33
import com.codingapi.springboot.framework.crypto.AES;
44
import lombok.SneakyThrows;
55

66
import java.nio.charset.StandardCharsets;
77
import java.util.Base64;
88

9-
public class MyAES {
9+
public class AESTools {
1010

11-
private final static MyAES instance = new MyAES();
11+
private final static AESTools instance = new AESTools();
1212

1313
private AES aes;
1414

15-
private MyAES() {
15+
private AESTools() {
1616
}
1717

1818
void init(AES aes) {
1919
this.aes = aes;
2020
}
2121

22-
public static MyAES getInstance() {
22+
public static AESTools getInstance() {
2323
return instance;
2424
}
2525

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.codingapi.springboot.security.crypto;
2+
3+
import com.codingapi.springboot.framework.crypto.AES;
4+
import com.codingapi.springboot.security.properties.CodingApiSecurityProperties;
5+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
6+
import org.springframework.context.annotation.Bean;
7+
import org.springframework.context.annotation.Configuration;
8+
9+
import java.util.Base64;
10+
11+
@Configuration
12+
public class SecurityCryptoConfiguration {
13+
14+
@Bean
15+
@ConditionalOnMissingBean
16+
public AES aes(CodingApiSecurityProperties properties) throws Exception {
17+
AES aes = new AES(Base64.getDecoder().decode(properties.getAseKey().getBytes()),
18+
Base64.getDecoder().decode(properties.getAseIv()));
19+
AESTools.getInstance().init(aes);
20+
return aes;
21+
}
22+
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import com.alibaba.fastjson.JSONObject;
44
import com.codingapi.springboot.framework.dto.response.Response;
55
import com.codingapi.springboot.security.exception.TokenExpiredException;
6-
import com.codingapi.springboot.security.gateway.TokenGateway;
76
import com.codingapi.springboot.security.gateway.Token;
7+
import com.codingapi.springboot.security.gateway.TokenGateway;
88
import com.codingapi.springboot.security.properties.CodingApiSecurityProperties;
99
import jakarta.servlet.FilterChain;
1010
import jakarta.servlet.ServletException;
@@ -45,7 +45,7 @@ public MyAuthenticationFilter(AuthenticationManager manager, CodingApiSecurityPr
4545
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
4646
log.debug("token authentication ~");
4747
for (String antUrl : securityJwtProperties.getAuthenticatedUrls()) {
48-
if(antPathMatcher.match(antUrl,request.getRequestURI())) {
48+
if (antPathMatcher.match(antUrl, request.getRequestURI())) {
4949

5050
String sign = request.getHeader(TOKEN_KEY);
5151
if (!StringUtils.hasLength(sign)) {
@@ -54,6 +54,10 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
5454
}
5555

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

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import com.alibaba.fastjson.JSONObject;
44
import com.codingapi.springboot.framework.serializable.JsonSerializable;
5-
import com.codingapi.springboot.security.jwt.MyAES;
5+
import com.codingapi.springboot.security.crypto.AESTools;
66
import com.codingapi.springboot.security.exception.TokenExpiredException;
77
import lombok.Getter;
88
import lombok.NoArgsConstructor;
@@ -34,7 +34,7 @@ public Token(String username, String iv,String extra, List<String> authorities,
3434
this.username = username;
3535
this.extra = extra;
3636
if(iv!=null) {
37-
this.iv = MyAES.getInstance().encode(iv);
37+
this.iv = AESTools.getInstance().encode(iv);
3838
}
3939
this.authorities = authorities;
4040
this.expireTime = System.currentTimeMillis() + expireValue;
@@ -56,7 +56,7 @@ public String decodeIv(){
5656
if(iv==null){
5757
return null;
5858
}
59-
return MyAES.getInstance().decode(iv);
59+
return AESTools.getInstance().decode(iv);
6060
}
6161

6262

springboot-starter-security/src/main/java/com/codingapi/springboot/security/gateway/TokenGateway.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
public interface TokenGateway {
66

7-
Token create(String username, String password, List<String> authorities, String extra);
7+
Token create(String username, String iv, List<String> authorities, String extra);
88

9-
default Token create(String username, String password, List<String> authorities) {
10-
return create(username, password, authorities, null);
9+
default Token create(String username, String iv, List<String> authorities) {
10+
return create(username, iv, authorities, null);
1111
}
1212

1313
default Token create(String username, List<String> authorities) {

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

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

3-
import com.codingapi.springboot.framework.crypto.AES;
43
import com.codingapi.springboot.security.gateway.TokenGateway;
54
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
65
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
76
import org.springframework.boot.context.properties.ConfigurationProperties;
87
import org.springframework.context.annotation.Bean;
98
import org.springframework.context.annotation.Configuration;
109

11-
import java.util.Base64;
12-
1310
@Configuration
1411
@ConditionalOnProperty(prefix = "codingapi.security.jwt", name = "enable", havingValue = "true", matchIfMissing = true)
1512
public class JWTSecurityConfiguration {
1613

17-
@Bean
18-
@ConditionalOnMissingBean
19-
public AES aes(SecurityJWTProperties properties) throws Exception {
20-
AES aes = new AES(Base64.getDecoder().decode(properties.getAseKey().getBytes()),
21-
Base64.getDecoder().decode(properties.getAseIv()));
22-
MyAES.getInstance().init(aes);
23-
return aes;
24-
}
2514

2615
@Bean
2716
@ConfigurationProperties(prefix = "codingapi.security.jwt")

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class JWTTokenGatewayImpl implements TokenGateway {
1010
private final Jwt jwt;
1111

1212
public JWTTokenGatewayImpl(SecurityJWTProperties properties) {
13-
this.jwt = new Jwt(properties.getSecretKey(), properties.getJwtTime(), properties.getJwtRestTime());
13+
this.jwt = new Jwt(properties.getSecretKey(), properties.getValidTime(), properties.getRestTime());
1414
}
1515

1616
@Override

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515
public class Jwt {
1616

1717
private final SecretKey key;
18-
private final int jwtTime;
19-
private final int jwtRestTime;
18+
private final int validTime;
19+
private final int restTime;
2020

21-
public Jwt(String secretKey, int jwtTime, int jwtRestTime) {
21+
public Jwt(String secretKey, int validTime, int restTime) {
2222
this.key = Keys.hmacShaKeyFor(secretKey.getBytes(StandardCharsets.UTF_8));
23-
this.jwtTime = jwtTime;
24-
this.jwtRestTime = jwtRestTime;
23+
this.validTime = validTime;
24+
this.restTime = restTime;
2525
}
2626

2727
public Token create(String username, List<String> authorities, String extra){
@@ -37,7 +37,7 @@ public Token create(String username, String iv, List<String> authorities){
3737
}
3838

3939
public Token create(String username, String iv,List<String> authorities,String extra){
40-
Token token = new Token(username, iv,extra, authorities, jwtTime, jwtRestTime);
40+
Token token = new Token(username, iv,extra, authorities, validTime, restTime);
4141
String jwt = Jwts.builder().subject(token.toJson()).signWith(key).compact();
4242
token.setToken(jwt);
4343
return token;

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

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,16 @@ public class SecurityJWTProperties {
2020
private String secretKey = "codingapi.security.jwt.secretkey";
2121

2222

23-
/**
24-
* aes key
25-
*/
26-
private String aseKey = "QUNEWCQlXiYqJCNYQ1phc0FDRFgkJV4mKiQjWENaYXM=";
27-
28-
/**
29-
* aes iv
30-
*/
31-
private String aseIv = "QUNYRkdIQEVEUyNYQ1phcw==";
32-
33-
3423
/**
3524
* JWT 有效时间(毫秒)
3625
* 15分钟有效期 1000*60*15=900000
3726
*/
38-
private int jwtTime = 900000;
27+
private int validTime = 900000;
3928

4029
/**
4130
* JWT 更换令牌时间(毫秒)
4231
* 10分钟后更换令牌 1000*60*10=600000
4332
*/
44-
private int jwtRestTime = 600000;
33+
private int restTime = 600000;
4534

4635
}

0 commit comments

Comments
 (0)