feat:用户注册
This commit is contained in:
@@ -0,0 +1,20 @@
|
|||||||
|
package com.xiang.xservice.auth.api.code;
|
||||||
|
|
||||||
|
import com.xiang.xservice.basic.exception.code.BaseErrorCode;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@AllArgsConstructor
|
||||||
|
public enum UserErrorCode implements BaseErrorCode {
|
||||||
|
|
||||||
|
USER_NOT_EXISTS("A1000101", "用户不存在"),
|
||||||
|
USER_EXISTS("A1000102", "用户已存在"),
|
||||||
|
USER_LOGIN_ERROR("A1000103", "用户登录失败!"),
|
||||||
|
USER_REGISTER_ERROR("1000104", "用户注册失败!"),
|
||||||
|
;
|
||||||
|
|
||||||
|
private final String code;
|
||||||
|
private final String message;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -4,7 +4,9 @@ import com.xiang.xservice.auth.api.dto.req.LoginRequest;
|
|||||||
import com.xiang.xservice.auth.api.dto.resp.LoginResp;
|
import com.xiang.xservice.auth.api.dto.resp.LoginResp;
|
||||||
import com.xiang.xservice.auth.service.service.XUserService;
|
import com.xiang.xservice.auth.service.service.XUserService;
|
||||||
import com.xiang.xservice.basic.common.resp.Result;
|
import com.xiang.xservice.basic.common.resp.Result;
|
||||||
|
import com.xiang.xservice.basic.exception.BusinessException;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
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.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
@@ -12,6 +14,7 @@ import org.springframework.web.bind.annotation.RestController;
|
|||||||
import javax.validation.Valid;
|
import javax.validation.Valid;
|
||||||
import javax.validation.constraints.NotNull;
|
import javax.validation.constraints.NotNull;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
@RestController
|
@RestController
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class TokenController {
|
public class TokenController {
|
||||||
@@ -20,6 +23,15 @@ public class TokenController {
|
|||||||
|
|
||||||
@PostMapping("/public/auth/login")
|
@PostMapping("/public/auth/login")
|
||||||
public Result<LoginResp> login(@RequestBody @NotNull(message = "请求参数不能为空") @Valid LoginRequest request) {
|
public Result<LoginResp> login(@RequestBody @NotNull(message = "请求参数不能为空") @Valid LoginRequest request) {
|
||||||
return Result.success("操作成功", userService.login(request));
|
try {
|
||||||
|
LoginResp login = userService.login(request);
|
||||||
|
return Result.success("操作成功", login);
|
||||||
|
} catch (BusinessException e) {
|
||||||
|
log.error("【用户登录】用户登录失败,{}", e.getMessage(), e);
|
||||||
|
return Result.error(e.getMessage());
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("【用户登录】用户登录失败,{}", e.getMessage(), e);
|
||||||
|
return Result.error();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,11 +4,15 @@ import com.xiang.xservice.auth.api.dto.req.RegisterRequest;
|
|||||||
import com.xiang.xservice.auth.api.dto.resp.RegisterResp;
|
import com.xiang.xservice.auth.api.dto.resp.RegisterResp;
|
||||||
import com.xiang.xservice.auth.service.service.XUserService;
|
import com.xiang.xservice.auth.service.service.XUserService;
|
||||||
import com.xiang.xservice.basic.common.resp.Result;
|
import com.xiang.xservice.basic.common.resp.Result;
|
||||||
|
import com.xiang.xservice.basic.exception.BusinessException;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
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.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import javax.validation.Valid;
|
||||||
|
import javax.validation.constraints.NotNull;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@@ -19,13 +23,16 @@ public class UserController {
|
|||||||
private final XUserService userService;
|
private final XUserService userService;
|
||||||
|
|
||||||
@PostMapping("/public/user/userRegister")
|
@PostMapping("/public/user/userRegister")
|
||||||
public Result<RegisterResp> register(RegisterRequest request) {
|
public Result<RegisterResp> register(@RequestBody @Valid @NotNull(message = "请求参数不能为空") RegisterRequest request) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
RegisterResp registerResp = userService.userRegister(request);
|
RegisterResp registerResp = userService.userRegister(request);
|
||||||
if (Objects.nonNull(registerResp)) {
|
if (Objects.nonNull(registerResp)) {
|
||||||
return Result.success("操作成功", registerResp);
|
return Result.success("操作成功", registerResp);
|
||||||
}
|
}
|
||||||
|
} catch (BusinessException e) {
|
||||||
|
log.error("【用户注册】用户注册失败:{}", e.getMessage(), e);
|
||||||
|
return Result.error(e.getMessage());
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("【用户注册】用户注册失败:{}", e.getMessage(), e);
|
log.error("【用户注册】用户注册失败:{}", e.getMessage(), e);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ public class AuthorizationServerConfig {
|
|||||||
http
|
http
|
||||||
.csrf().disable() // 禁用 CSRF
|
.csrf().disable() // 禁用 CSRF
|
||||||
.authorizeRequests(authorizeRequests -> authorizeRequests
|
.authorizeRequests(authorizeRequests -> authorizeRequests
|
||||||
.antMatchers("/public/auth/login").permitAll()
|
.antMatchers("/public/**").permitAll()
|
||||||
.anyRequest().authenticated()
|
.anyRequest().authenticated()
|
||||||
)
|
)
|
||||||
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS));
|
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS));
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.xiang.xservice.auth.service.service.impl;
|
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.LoginRequest;
|
||||||
import com.xiang.xservice.auth.api.dto.req.RegisterRequest;
|
import com.xiang.xservice.auth.api.dto.req.RegisterRequest;
|
||||||
import com.xiang.xservice.auth.api.dto.resp.LoginResp;
|
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.repository.mapper.XUserMapper;
|
||||||
import com.xiang.xservice.auth.service.service.XUserService;
|
import com.xiang.xservice.auth.service.service.XUserService;
|
||||||
import com.xiang.xservice.basic.enums.DelStatusEnum;
|
import com.xiang.xservice.basic.enums.DelStatusEnum;
|
||||||
|
import com.xiang.xservice.basic.exception.BusinessException;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.security.authentication.AuthenticationManager;
|
import org.springframework.security.authentication.AuthenticationManager;
|
||||||
@@ -45,8 +47,8 @@ public class XUserServiceImpl implements XUserService {
|
|||||||
);
|
);
|
||||||
SecurityContextHolder.getContext().setAuthentication(authenticate);
|
SecurityContextHolder.getContext().setAuthentication(authenticate);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error(e.getMessage());
|
log.error("【用户登录】用户登录校验失败,{}", e.getMessage(), e);
|
||||||
throw new RuntimeException(e.getMessage());
|
throw new BusinessException(e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
// 生成 token
|
// 生成 token
|
||||||
@@ -73,7 +75,7 @@ public class XUserServiceImpl implements XUserService {
|
|||||||
public UserResp getUserByUsername(String username) {
|
public UserResp getUserByUsername(String username) {
|
||||||
XUser user = userMapper.selectByUsername(username);
|
XUser user = userMapper.selectByUsername(username);
|
||||||
if (Objects.isNull(user)) {
|
if (Objects.isNull(user)) {
|
||||||
return null;
|
throw new BusinessException(UserErrorCode.USER_NOT_EXISTS);
|
||||||
}
|
}
|
||||||
|
|
||||||
UserResp userResp = new UserResp();
|
UserResp userResp = new UserResp();
|
||||||
@@ -94,7 +96,7 @@ public class XUserServiceImpl implements XUserService {
|
|||||||
// todo 手机号验证码校验
|
// todo 手机号验证码校验
|
||||||
XUser user = userMapper.selectByUsername(request.getUsername());
|
XUser user = userMapper.selectByUsername(request.getUsername());
|
||||||
if (Objects.nonNull(user)) {
|
if (Objects.nonNull(user)) {
|
||||||
throw new RuntimeException("用户名已存在!");
|
throw new BusinessException(UserErrorCode.USER_EXISTS);
|
||||||
}
|
}
|
||||||
user = new XUser();
|
user = new XUser();
|
||||||
user.setName(request.getName());
|
user.setName(request.getName());
|
||||||
@@ -112,10 +114,10 @@ public class XUserServiceImpl implements XUserService {
|
|||||||
|
|
||||||
if (userMapper.insert(user) > 0) {
|
if (userMapper.insert(user) > 0) {
|
||||||
RegisterResp registerResp = new RegisterResp();
|
RegisterResp registerResp = new RegisterResp();
|
||||||
registerResp.setName(registerResp.getName());
|
registerResp.setName(user.getName());
|
||||||
registerResp.setUsername(registerResp.getUsername());
|
registerResp.setUsername(user.getUsername());
|
||||||
registerResp.setEmail(registerResp.getEmail());
|
registerResp.setEmail(user.getEmail());
|
||||||
registerResp.setPhone(registerResp.getPhone());
|
registerResp.setPhone(user.getPhone());
|
||||||
return registerResp;
|
return registerResp;
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
Reference in New Issue
Block a user