feat(config): 更新数据库配置和JWT设置

- 修改数据库连接地址和密码
- 调整MyBatis Plus主键生成策略为auto
- 移除文件上传临时目录及FFmpeg路径配置
-优化登录认证异常处理逻辑
- 完善用户注册与登录服务实现- 更新用户角色关联接口参数命名
This commit is contained in:
Chaos
2025-11-10 21:41:54 +08:00
parent c6d18d4979
commit ed48e05285
4 changed files with 38 additions and 29 deletions

View File

@@ -35,6 +35,6 @@ public interface UserMapper extends BaseMapper<User> {
* @param id 用户id * @param id 用户id
* @param i 角色id * @param i 角色id
*/ */
@Select("INSERT INTO t_user_role (user_id, role_id) VALUES (#{id}, #{i})") @Select("INSERT INTO t_user_role (user_id, role_id) VALUES (#{userId}, #{roleId})")
void insertUserRole(Long id, int i); void insertUserRole(@Param("userId") Long id, @Param("roleId") Long i);
} }

View File

@@ -6,6 +6,7 @@ import cn.nopj.chaos_api.model.ApiResult;
import cn.nopj.chaos_api.service.AuthService; import cn.nopj.chaos_api.service.AuthService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
@@ -44,13 +45,17 @@ public class AuthController {
*/ */
@PostMapping("/login") @PostMapping("/login")
public ApiResult<?> login(@RequestBody LoginRequest loginRequest) { public ApiResult<?> login(@RequestBody LoginRequest loginRequest) {
try {
String token = authService.login(loginRequest.getUsername(), loginRequest.getPassword()); String token = authService.login(loginRequest.getUsername(), loginRequest.getPassword());
if (token == null) {
return ApiResult.failed("用户名或密码错误");
}
Map<String, String> tokenMap = new HashMap<>(); Map<String, String> tokenMap = new HashMap<>();
tokenMap.put("token", token); tokenMap.put("token", token);
tokenMap.put("tokenHead", tokenHead); tokenMap.put("tokenHead", tokenHead);
return ApiResult.success(tokenMap); return ApiResult.success(tokenMap);
}catch (BadCredentialsException e){
return ApiResult.failed(e.getMessage());
}
} }
} }

View File

@@ -20,6 +20,7 @@ import org.springframework.stereotype.Service;
@Service @Service
public class AuthServiceImpl implements AuthService { public class AuthServiceImpl implements AuthService {
@Autowired @Autowired
private UserMapper userMapper; private UserMapper userMapper;
@Autowired @Autowired
@@ -42,19 +43,30 @@ public class AuthServiceImpl implements AuthService {
user.setPassword(passwordEncoder.encode(registerRequest.getPassword())); user.setPassword(passwordEncoder.encode(registerRequest.getPassword()));
userMapper.insert(user); userMapper.insert(user);
// 你可以在这里为新用户分配默认角色 // 你可以在这里为新用户分配默认角色
userMapper.insertUserRole(user.getId(), 2); userMapper.insertUserRole(user.getId(), 2L);
return user; return user;
} }
@Override @Override
public String login(String username, String password) { public String login(String username, String password) {
// 使用 Spring Security 的 AuthenticationManager 进行用户认证
Authentication authentication = authenticationManager.authenticate( Authentication authentication = authenticationManager.authenticate(
new UsernamePasswordAuthenticationToken(username, password) new UsernamePasswordAuthenticationToken(username, password)
); );
SecurityContextHolder.getContext().setAuthentication(authentication); SecurityContextHolder.getContext().setAuthentication(authentication);
// 获取用户详情
UserDetails userDetails = (UserDetails) authentication.getPrincipal(); UserDetails userDetails = (UserDetails) authentication.getPrincipal();
if (!userDetails.isEnabled()){
return null;
}
// 生成 JWT // 生成 JWT
return jwtTokenUtil.generateToken(userDetails); return jwtTokenUtil.generateToken(userDetails);
} }
} }

View File

@@ -6,9 +6,9 @@ spring:
name: chaos-api name: chaos-api
datasource: datasource:
driver-class-name: org.mariadb.jdbc.Driver driver-class-name: org.mariadb.jdbc.Driver
url: jdbc:mariadb://10.91.3.23:3306/chaos?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&allowPublicKeyRetrieval=true&useSSL=false url: jdbc:mariadb://10.91.3.253:3306/chaos?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&allowPublicKeyRetrieval=true&useSSL=false
username: chaos username: chaos
password: zx123456.. password: fH7NPTeRSSFFicsh
type: com.alibaba.druid.pool.DruidDataSource type: com.alibaba.druid.pool.DruidDataSource
druid: druid:
initial-size: 5 initial-size: 5
@@ -20,20 +20,16 @@ spring:
max-file-size: 100GB max-file-size: 100GB
max-request-size: 100GB max-request-size: 100GB
mybatis-plus: mybatis-plus:
mapper-locations: classpath*:/mapper/**/*.xml mapper-locations: classpath*:/mapper/**/*.xml
type-aliases-package: cn.nopj.chaos_api_domain.entity type-aliases-package: cn.nopj.chaos_api_domain.entity
global-config: global-config:
db-config: db-config:
id-type: assign_id id-type: auto
configuration: configuration:
map-underscore-to-camel-case: true map-underscore-to-camel-case: true
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
jwt: jwt:
tokenHeader: Authorization tokenHeader: Authorization
tokenHead: Chaos tokenHead: Chaos
@@ -41,7 +37,3 @@ jwt:
expiration: 604800 expiration: 604800
file:
upload:
temp-dir: ./chaos/upload
ffmpeg-path: C:\Users\Chaos\AppData\Local\Microsoft\WinGet\Packages\Gyan.FFmpeg_Microsoft.Winget.Source_8wekyb3d8bbwe\ffmpeg-7.1.1-full_build\bin\ffmpeg.exe