This commit is contained in:
@ -2,15 +2,15 @@ package co.jp.app.common;
|
||||
|
||||
|
||||
public class ApiResponse<T> {
|
||||
//成功状况判定
|
||||
private boolean success;
|
||||
//状态码
|
||||
private int code;
|
||||
//状态信息
|
||||
private String message;
|
||||
//数据
|
||||
private T data;
|
||||
|
||||
//空构造函数
|
||||
private ApiResponse() {
|
||||
}
|
||||
|
||||
private ApiResponse(ResultCode resultCode, T data) {
|
||||
this.code = resultCode.getCode();
|
||||
this.message = resultCode.getMessage();
|
||||
@ -18,12 +18,13 @@ public class ApiResponse<T> {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public static <T> ApiResponse<T> success(T data) {
|
||||
return new ApiResponse<>(ResultCode.SUCCESS, data);
|
||||
private ApiResponse() {
|
||||
}
|
||||
|
||||
public static <T> ApiResponse<T> success() {
|
||||
return success(null);
|
||||
public static <T> ApiResponse<T> fail() {return success(null);}
|
||||
|
||||
public static <T> ApiResponse<T> success(T data) {
|
||||
return new ApiResponse<>(ResultCode.SUCCESS, data);
|
||||
}
|
||||
|
||||
public static <T> ApiResponse<T> fail(ResultCode resultCode) {
|
||||
|
@ -28,7 +28,11 @@ public enum ResultCode {
|
||||
// 注册相关
|
||||
// USER_REGISTRATION_SUCCESS(3000, "用户注册成功"),
|
||||
USER_EMAIL_ALREADY_EXISTS(3001, "Email already exists"),
|
||||
|
||||
USER_USERNAME_ALREADY_EXISTS(3002, "Username"),
|
||||
|
||||
USER_EMAIL_NOT_VALID(3006, "Email is not valid"),
|
||||
|
||||
USER_PASSWORD_TOO_SHORT(3003, "password too short"),
|
||||
USER_PASSWORD_TOO_WEAK(3004, "password too weak"),
|
||||
USER_REGISTRATION_FAILED(3005, "User registration failed"),
|
||||
|
@ -24,7 +24,6 @@ import co.jp.app.service.UserService;
|
||||
import jakarta.validation.Valid;
|
||||
|
||||
@RestController
|
||||
|
||||
public class UserController {
|
||||
|
||||
private final UserService userService;
|
||||
|
@ -1,9 +1,17 @@
|
||||
package co.jp.app.dto;
|
||||
|
||||
import jakarta.validation.constraints.Email;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.Size;
|
||||
|
||||
public class LoginDto {
|
||||
|
||||
@NotBlank(message = "邮箱不能为空")
|
||||
@Email(message = "邮箱格式不正确,请输入有效的邮箱地址")
|
||||
private String email;
|
||||
|
||||
@NotBlank(message = "密码不能为空")
|
||||
@Size(min = 6, max = 30, message = "密码长度必须在6到30位之间")
|
||||
private String password;
|
||||
|
||||
public String getEmail() {
|
||||
|
@ -1,11 +1,26 @@
|
||||
package co.jp.app.dto;
|
||||
|
||||
import jakarta.validation.constraints.Email;
|
||||
import jakarta.validation.constraints.NotBlank; // 或 javax.validation.constraints.NotBlank
|
||||
import jakarta.validation.constraints.Pattern;
|
||||
import jakarta.validation.constraints.Size;
|
||||
|
||||
public class RegistrationDto {
|
||||
|
||||
//注:username可以为空
|
||||
@Size(min = 2, max = 20, message = "用户名长度必须在2到20个字符之间")
|
||||
@Pattern(
|
||||
regexp = "^[a-zA-Z\\p{script=Han}]+$",
|
||||
message = "用户名只能包含大小写英文字母和汉字,不能使用数字和标点符号"
|
||||
)
|
||||
private String name;
|
||||
|
||||
@NotBlank(message = "邮箱不能为空")
|
||||
@Email(message = "邮箱格式不正确,请输入有效的邮箱地址")
|
||||
private String email;
|
||||
|
||||
@NotBlank(message = "密码不能为空")
|
||||
@Size(min = 6, max = 30, message = "密码长度必须在6到30位之间")
|
||||
private String password;
|
||||
|
||||
public String getName() {
|
||||
|
@ -34,7 +34,7 @@ public class UserService implements UserDetailsService {
|
||||
|
||||
public UserEntity registerNewUser(@NotNull RegistrationDto registrationDto) throws BusinessException {
|
||||
|
||||
if (userRepository.existsByName(registrationDto.getName())) {
|
||||
if (userRepository.existsByEmail(registrationDto.getEmail())) {
|
||||
throw new BusinessException(ResultCode.USER_USERNAME_ALREADY_EXISTS,"error: Name" + registrationDto.getName() + " had been used");
|
||||
}
|
||||
|
||||
@ -55,15 +55,15 @@ public class UserService implements UserDetailsService {
|
||||
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
public UserDetails loadUserByUsername(String name) throws UsernameNotFoundException {
|
||||
UserEntity userEntity = userRepository.findByName(name)
|
||||
.orElseThrow(() -> new UsernameNotFoundException(name + " not found"));
|
||||
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
|
||||
UserEntity userEntity = userRepository.findByEmail(email)
|
||||
.orElseThrow(() -> new UsernameNotFoundException(email + " not found"));
|
||||
|
||||
Collection<? extends GrantedAuthority> authorities = Collections
|
||||
.singletonList(new SimpleGrantedAuthority("ROLE_USER"));
|
||||
|
||||
return new User(
|
||||
userEntity.getName(),
|
||||
userEntity.getEmail(),
|
||||
userEntity.getPassword(),
|
||||
true, // enabled
|
||||
true, // accountNonExpired
|
||||
|
Reference in New Issue
Block a user