feat:用户注册

This commit is contained in:
xiang
2025-08-24 16:23:29 +08:00
parent 7e6d5c07a3
commit c3da7f7c35
5 changed files with 52 additions and 11 deletions

View File

@@ -52,7 +52,7 @@ public class AuthorizationServerConfig {
http
.csrf().disable() // 禁用 CSRF
.authorizeRequests(authorizeRequests -> authorizeRequests
.antMatchers("/public/auth/login").permitAll()
.antMatchers("/public/**").permitAll()
.anyRequest().authenticated()
)
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS));

View File

@@ -1,5 +1,6 @@
package com.xiang.xservice.auth.service.service.impl;
import com.xiang.xservice.auth.api.code.UserErrorCode;
import com.xiang.xservice.auth.api.dto.req.LoginRequest;
import com.xiang.xservice.auth.api.dto.req.RegisterRequest;
import com.xiang.xservice.auth.api.dto.resp.LoginResp;
@@ -10,6 +11,7 @@ import com.xiang.xservice.auth.service.enums.UserStatusEnum;
import com.xiang.xservice.auth.service.repository.mapper.XUserMapper;
import com.xiang.xservice.auth.service.service.XUserService;
import com.xiang.xservice.basic.enums.DelStatusEnum;
import com.xiang.xservice.basic.exception.BusinessException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.AuthenticationManager;
@@ -45,8 +47,8 @@ public class XUserServiceImpl implements XUserService {
);
SecurityContextHolder.getContext().setAuthentication(authenticate);
} catch (Exception e) {
log.error(e.getMessage());
throw new RuntimeException(e.getMessage());
log.error("【用户登录】用户登录校验失败,{}", e.getMessage(), e);
throw new BusinessException(e.getMessage());
}
// 生成 token
@@ -73,7 +75,7 @@ public class XUserServiceImpl implements XUserService {
public UserResp getUserByUsername(String username) {
XUser user = userMapper.selectByUsername(username);
if (Objects.isNull(user)) {
return null;
throw new BusinessException(UserErrorCode.USER_NOT_EXISTS);
}
UserResp userResp = new UserResp();
@@ -94,7 +96,7 @@ public class XUserServiceImpl implements XUserService {
// todo 手机号验证码校验
XUser user = userMapper.selectByUsername(request.getUsername());
if (Objects.nonNull(user)) {
throw new RuntimeException("用户名已存在!");
throw new BusinessException(UserErrorCode.USER_EXISTS);
}
user = new XUser();
user.setName(request.getName());
@@ -112,10 +114,10 @@ public class XUserServiceImpl implements XUserService {
if (userMapper.insert(user) > 0) {
RegisterResp registerResp = new RegisterResp();
registerResp.setName(registerResp.getName());
registerResp.setUsername(registerResp.getUsername());
registerResp.setEmail(registerResp.getEmail());
registerResp.setPhone(registerResp.getPhone());
registerResp.setName(user.getName());
registerResp.setUsername(user.getUsername());
registerResp.setEmail(user.getEmail());
registerResp.setPhone(user.getPhone());
return registerResp;
}
return null;