init
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
package com.ski.lichuan.initializer;
|
||||
|
||||
import com.ski.lichuan.mapper.UserMapper;
|
||||
import com.ski.lichuan.model.SysUser;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class AdminUserInitializer implements CommandLineRunner {
|
||||
|
||||
@Autowired
|
||||
private UserMapper userMapper;
|
||||
|
||||
@Autowired
|
||||
private BCryptPasswordEncoder passwordEncoder;
|
||||
|
||||
@Override
|
||||
public void run(String... args) throws Exception {
|
||||
// 检查是否已存在管理员用户
|
||||
SysUser existingAdmin = userMapper.selectByUsername("admin");
|
||||
if (existingAdmin == null) {
|
||||
// 创建默认管理员用户
|
||||
SysUser adminUser = new SysUser();
|
||||
adminUser.setUsername("admin");
|
||||
// 默认密码为 admin123,实际使用时应修改为更安全的密码
|
||||
adminUser.setPassword(passwordEncoder.encode("admin123"));
|
||||
adminUser.setNickname("系统管理员");
|
||||
adminUser.setStatus(1); // 启用状态
|
||||
// 插入用户到数据库
|
||||
userMapper.insertUser(adminUser);
|
||||
System.out.println("默认管理员用户已创建,用户名: admin, 密码: admin123 (请登录后及时修改密码)");
|
||||
} else {
|
||||
System.out.println("管理员用户已存在");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.ski.lichuan.services;
|
||||
|
||||
import com.ski.lichuan.mapper.UserMapper;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.ski.lichuan.model.SysUser;
|
||||
|
||||
@Service
|
||||
public class UserDetailsServiceImpl implements UserDetailsService {
|
||||
@Autowired
|
||||
private UserMapper userMapper; // MyBatis-Plus 映射接口
|
||||
|
||||
@Override
|
||||
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
||||
// 从数据库查询用户
|
||||
SysUser user = userMapper.selectByUsername(username);
|
||||
if (user == null) {
|
||||
throw new UsernameNotFoundException("用户名不存在");
|
||||
}
|
||||
return user;
|
||||
}
|
||||
}
|
||||
13
ski-dashboard-service/src/main/resources/sql/sys_user.sql
Normal file
13
ski-dashboard-service/src/main/resources/sql/sys_user.sql
Normal file
@@ -0,0 +1,13 @@
|
||||
CREATE TABLE sys_user (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
username VARCHAR(50) NOT NULL UNIQUE,
|
||||
password VARCHAR(100) NOT NULL,
|
||||
nickname VARCHAR(50),
|
||||
status SMALLINT DEFAULT 1,
|
||||
CONSTRAINT uk_username UNIQUE (username)
|
||||
);
|
||||
|
||||
COMMENT ON COLUMN sys_user.username IS '用户名';
|
||||
COMMENT ON COLUMN sys_user.password IS '加密后的密码(BCrypt)';
|
||||
COMMENT ON COLUMN sys_user.nickname IS '昵称';
|
||||
COMMENT ON COLUMN sys_user.status IS '状态(1-正常,0-禁用)';
|
||||
Reference in New Issue
Block a user