Skip to content

Commit faccb4b

Browse files
gadfly3173colorful3
authored andcommitted
fix: 增加aes配置校验,避免输入参数不合法
1 parent a814104 commit faccb4b

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

src/main/java/io/github/talelin/latticy/common/configuration/LoginCaptchaProperties.java

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
import io.github.talelin.latticy.common.util.CaptchaUtil;
44
import lombok.Getter;
55
import lombok.Setter;
6+
import lombok.extern.slf4j.Slf4j;
67
import org.springframework.boot.context.properties.ConfigurationProperties;
78
import org.springframework.stereotype.Component;
9+
import org.springframework.util.StringUtils;
810

911
/**
1012
* @author Gadfly
1113
*/
14+
@Slf4j
1215
@Getter
1316
@Setter
1417
@Component
@@ -17,13 +20,38 @@ public class LoginCaptchaProperties {
1720
/**
1821
* aes 密钥
1922
*/
20-
private String secret = CaptchaUtil.getRandomString(32);
23+
private String secret;
2124
/**
2225
* aes 偏移量
2326
*/
24-
private String iv = CaptchaUtil.getRandomString(16);
27+
private String iv;
2528
/**
2629
* 启用验证码
2730
*/
2831
private Boolean enabled = Boolean.FALSE;
32+
33+
public void setSecret(String secret) {
34+
this.secret = CaptchaUtil.getRandomString(32);
35+
if (StringUtils.hasText(secret)) {
36+
byte[] bytes = secret.getBytes();
37+
if (bytes.length == 16 || bytes.length == 24 || bytes.length == 32) {
38+
this.secret = secret;
39+
} else {
40+
log.warn("AES密钥必须为128/192/256bit,输入的密钥为{}bit,已启用随机密钥{}", bytes.length * 8, this.secret);
41+
}
42+
}
43+
}
44+
45+
public void setIv(String iv) {
46+
this.iv = CaptchaUtil.getRandomString(16);
47+
if (StringUtils.hasText(iv)) {
48+
byte[] bytes = iv.getBytes();
49+
if (bytes.length == 16) {
50+
this.iv = iv;
51+
} else {
52+
log.warn("AES初始向量必须为128bit,输入的密钥为{}bit,已启用随机向量{}", bytes.length * 8, this.iv);
53+
}
54+
}
55+
}
56+
2957
}

0 commit comments

Comments
 (0)