Skip to content
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

fix 修复当第一次绑定密码后,再次绑定密码时存入数据库的密码变成明文的bug feat 将解密密码重复的部分合并 #85

Merged
merged 1 commit into from
Nov 29, 2023
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
2 changes: 1 addition & 1 deletion src/main/java/sast/evento/common/enums/ErrorEnum.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public enum ErrorEnum {
EVENT_NOT_EXIST(1008,"event not exist"),
/* 登录异常 */
LOGIN_ERROR(1009,"login failed"),
LOGIN_EXPIRE(1010,"login expire"),
LOGIN_EXPIRE(1010,"login expired"),
NOT_REGISTER(1011,"user info not exist please register first"),

/* 其他服务错误 */
Expand Down
36 changes: 16 additions & 20 deletions src/main/java/sast/evento/service/impl/LoginServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,21 +184,13 @@ public void checkTicket(String studentId, String ticket) {
@Transactional(rollbackFor = Exception.class)
public void bindPassword(String studentId, String password) {
studentId = studentId.toLowerCase();
String privateKeyStr = (String) redisUtil.get(LOGIN_KEY + studentId);
if (privateKeyStr == null || privateKeyStr.isEmpty()) {
throw new LocalRunTimeException(ErrorEnum.LOGIN_EXPIRE, "login failed please try again");
}
try {
password = RSAUtil.decryptByPrivateKey(password, privateKeyStr);
} catch (Exception e) {
throw new LocalRunTimeException(ErrorEnum.LOGIN_ERROR, "login failed please try again");
}
password = decryptPassword(studentId,password);
String salt = MD5Util.getSalt(5);
UserPassword userPassword = userPasswordMapper.selectOne(Wrappers.lambdaQuery(UserPassword.class)
.eq(UserPassword::getStudentId, studentId)
.last("for update"));
if (userPassword != null) {
userPassword.setPassword(password);
userPassword.setPassword(MD5Util.md5Encode(password, salt));
userPassword.setSalt(salt);
userPasswordMapper.updateById(userPassword);
} else {
Expand Down Expand Up @@ -281,15 +273,7 @@ private void setCommonInfo(User local, UserInfo userInfo) {

private void checkPassword(String studentId, String password) {
studentId = studentId.toLowerCase();
String privateKeyStr = (String) redisUtil.get(LOGIN_KEY + studentId);
if (privateKeyStr == null || privateKeyStr.isEmpty()) {
throw new LocalRunTimeException(ErrorEnum.LOGIN_EXPIRE, "login failed please try again");
}
try {
password = RSAUtil.decryptByPrivateKey(password, privateKeyStr);
} catch (Exception e) {
throw new LocalRunTimeException(ErrorEnum.LOGIN_ERROR, "login failed please try again");
}
password = decryptPassword(studentId,password);
UserPassword userPassword = userPasswordMapper.selectOne(Wrappers.lambdaQuery(UserPassword.class)
.eq(UserPassword::getStudentId, studentId));
if (userPassword == null) {
Expand Down Expand Up @@ -320,5 +304,17 @@ private String generateToken(UserModel user) {
return jwtUtil.generateToken(payload);
}


// 使用密钥解密密码
private String decryptPassword(String studentId, String password){
String privateKeyStr = (String) redisUtil.get(LOGIN_KEY + studentId);
if (privateKeyStr == null || privateKeyStr.isEmpty()) {
throw new LocalRunTimeException(ErrorEnum.LOGIN_EXPIRE, "login expired please try again");
}
try {
password = RSAUtil.decryptByPrivateKey(password, privateKeyStr);
} catch (Exception e) {
throw new LocalRunTimeException(ErrorEnum.LOGIN_ERROR, "login failed please try again");
}
return password;
}
}