增加启动时修改密码配置项

This commit is contained in:
xiaozzzi
2023-12-17 21:40:09 +08:00
parent 9e5a95b446
commit 362bd52a89
5 changed files with 37 additions and 2 deletions

View File

@@ -53,6 +53,10 @@ public class AuthProperties implements EnvironmentAware, ApplicationContextAware
* 默认密码
*/
private String defaultPassword = "123456";
/**
* 重置密码
*/
private Boolean passwordReset = Boolean.FALSE;
/**
* 日志类型
*/

View File

@@ -4,16 +4,18 @@ import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.blossom.backend.base.auth.exception.AuthException;
import com.blossom.backend.base.auth.exception.AuthRCode;
import com.blossom.backend.base.auth.pojo.LoginDTO;
import com.blossom.backend.base.auth.security.PasswordEncoder;
import com.blossom.backend.base.auth.pojo.AccessToken;
import com.blossom.backend.base.auth.pojo.LoginDTO;
import com.blossom.backend.base.auth.repo.TokenRepository;
import com.blossom.backend.base.auth.security.PasswordEncoder;
import com.blossom.backend.base.auth.token.TokenEncoder;
import com.blossom.backend.base.user.UserService;
import com.blossom.backend.base.user.pojo.UserEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;
/**
@@ -72,4 +74,18 @@ public class AuthService extends AbstractAuthService {
public AccessToken check() {
return AuthContext.getContext();
}
/**
* 启动时重置密码
*/
@EventListener(ApplicationStartedEvent.class)
public void refresh() {
if (properties.getPasswordReset()) {
log.warn("[AUTHORIZ] 重置用户密码");
for (UserEntity user : userService.listAll()) {
log.warn("[AUTHORIZ] 重置用户[{}]密码", user.getId());
userService.resetPassword(user.getId(), properties.getDefaultPassword(), user.getSalt());
}
}
}
}

View File

@@ -93,5 +93,18 @@ public class UserService extends ServiceImpl<UserMapper, UserEntity> {
baseMapper.updPwd(req.getUserId(), newPwd);
}
/**
* 重置用户密码
*
* @param userId 用户ID
* @param password 密码
* @param salt 加盐
* @since 1.11.0
*/
@Transactional(rollbackFor = Exception.class)
public void resetPassword(Long userId, String password, String salt) {
String newPwd = passwordEncoder.encode(password + salt);
baseMapper.updPwd(userId, newPwd);
}
}

View File

@@ -42,6 +42,7 @@ project:
type: caffeine
default-password: 123456 # 默认密码
password-encoder: bcrypt # 加密方式
password-reset: false # 启动时重置密码
clients: # 客户端
- client-id: blossom
grant-type: 'password'

View File

@@ -42,6 +42,7 @@ project:
type: caffeine
default-password: 123456 # 默认密码
password-encoder: bcrypt # 加密方式
password-reset: false # 启动时重置密码
clients: # 客户端
- client-id: blossom
grant-type: 'password'