mirror of
https://github.com/blossom-editor/blossom.git
synced 2026-03-12 17:41:26 +08:00
增加启动时修改密码配置项
This commit is contained in:
@@ -53,6 +53,10 @@ public class AuthProperties implements EnvironmentAware, ApplicationContextAware
|
||||
* 默认密码
|
||||
*/
|
||||
private String defaultPassword = "123456";
|
||||
/**
|
||||
* 重置密码
|
||||
*/
|
||||
private Boolean passwordReset = Boolean.FALSE;
|
||||
/**
|
||||
* 日志类型
|
||||
*/
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -42,6 +42,7 @@ project:
|
||||
type: caffeine
|
||||
default-password: 123456 # 默认密码
|
||||
password-encoder: bcrypt # 加密方式
|
||||
password-reset: false # 启动时重置密码
|
||||
clients: # 客户端
|
||||
- client-id: blossom
|
||||
grant-type: 'password'
|
||||
|
||||
@@ -42,6 +42,7 @@ project:
|
||||
type: caffeine
|
||||
default-password: 123456 # 默认密码
|
||||
password-encoder: bcrypt # 加密方式
|
||||
password-reset: false # 启动时重置密码
|
||||
clients: # 客户端
|
||||
- client-id: blossom
|
||||
grant-type: 'password'
|
||||
|
||||
Reference in New Issue
Block a user