Compare commits
6 Commits
6c1f6d824e
...
master
Author | SHA1 | Date | |
---|---|---|---|
a663a73ff1 | |||
2d27ebea35 | |||
d8b7943646 | |||
f58c1813ff | |||
57253951d3 | |||
6aec72e6ab |
@ -1,4 +1,4 @@
|
|||||||
activeProfiles=pom.xml
|
activeProfiles=
|
||||||
eclipse.preferences.version=1
|
eclipse.preferences.version=1
|
||||||
resolveWorkspaceProjects=true
|
resolveWorkspaceProjects=true
|
||||||
version=1
|
version=1
|
||||||
|
@ -2,15 +2,15 @@ package co.jp.app.common;
|
|||||||
|
|
||||||
|
|
||||||
public class ApiResponse<T> {
|
public class ApiResponse<T> {
|
||||||
|
//成功状况判定
|
||||||
private boolean success;
|
private boolean success;
|
||||||
|
//状态码
|
||||||
private int code;
|
private int code;
|
||||||
|
//状态信息
|
||||||
private String message;
|
private String message;
|
||||||
|
//数据
|
||||||
private T data;
|
private T data;
|
||||||
|
|
||||||
//空构造函数
|
|
||||||
private ApiResponse() {
|
|
||||||
}
|
|
||||||
|
|
||||||
private ApiResponse(ResultCode resultCode, T data) {
|
private ApiResponse(ResultCode resultCode, T data) {
|
||||||
this.code = resultCode.getCode();
|
this.code = resultCode.getCode();
|
||||||
this.message = resultCode.getMessage();
|
this.message = resultCode.getMessage();
|
||||||
@ -18,12 +18,13 @@ public class ApiResponse<T> {
|
|||||||
this.data = data;
|
this.data = data;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T> ApiResponse<T> success(T data) {
|
private ApiResponse() {
|
||||||
return new ApiResponse<>(ResultCode.SUCCESS, data);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T> ApiResponse<T> success() {
|
public static <T> ApiResponse<T> fail() {return success(null);}
|
||||||
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) {
|
public static <T> ApiResponse<T> fail(ResultCode resultCode) {
|
||||||
|
@ -28,7 +28,11 @@ public enum ResultCode {
|
|||||||
// 注册相关
|
// 注册相关
|
||||||
// USER_REGISTRATION_SUCCESS(3000, "用户注册成功"),
|
// USER_REGISTRATION_SUCCESS(3000, "用户注册成功"),
|
||||||
USER_EMAIL_ALREADY_EXISTS(3001, "Email already exists"),
|
USER_EMAIL_ALREADY_EXISTS(3001, "Email already exists"),
|
||||||
// USER_USERNAME_ALREADY_EXISTS(3002, "Username"),
|
|
||||||
|
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_SHORT(3003, "password too short"),
|
||||||
USER_PASSWORD_TOO_WEAK(3004, "password too weak"),
|
USER_PASSWORD_TOO_WEAK(3004, "password too weak"),
|
||||||
USER_REGISTRATION_FAILED(3005, "User registration failed"),
|
USER_REGISTRATION_FAILED(3005, "User registration failed"),
|
||||||
|
@ -24,7 +24,6 @@ import co.jp.app.service.UserService;
|
|||||||
import jakarta.validation.Valid;
|
import jakarta.validation.Valid;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
|
|
||||||
public class UserController {
|
public class UserController {
|
||||||
|
|
||||||
private final UserService userService;
|
private final UserService userService;
|
||||||
|
@ -1,9 +1,17 @@
|
|||||||
package co.jp.app.dto;
|
package co.jp.app.dto;
|
||||||
|
|
||||||
|
import jakarta.validation.constraints.Email;
|
||||||
|
import jakarta.validation.constraints.NotBlank;
|
||||||
|
import jakarta.validation.constraints.Size;
|
||||||
|
|
||||||
public class LoginDto {
|
public class LoginDto {
|
||||||
|
|
||||||
|
@NotBlank(message = "邮箱不能为空")
|
||||||
|
@Email(message = "邮箱格式不正确,请输入有效的邮箱地址")
|
||||||
private String email;
|
private String email;
|
||||||
|
|
||||||
|
@NotBlank(message = "密码不能为空")
|
||||||
|
@Size(min = 6, max = 30, message = "密码长度必须在6到30位之间")
|
||||||
private String password;
|
private String password;
|
||||||
|
|
||||||
public String getEmail() {
|
public String getEmail() {
|
||||||
|
@ -1,11 +1,26 @@
|
|||||||
package co.jp.app.dto;
|
package co.jp.app.dto;
|
||||||
|
|
||||||
|
import jakarta.validation.constraints.Email;
|
||||||
|
import jakarta.validation.constraints.NotBlank;
|
||||||
|
import jakarta.validation.constraints.Pattern;
|
||||||
|
import jakarta.validation.constraints.Size;
|
||||||
|
|
||||||
public class RegistrationDto {
|
public class RegistrationDto {
|
||||||
|
|
||||||
|
//注:username可以为空
|
||||||
|
@Size(min = 2, max = 20, message = "用户名长度必须在2到20个字符之间")
|
||||||
|
@Pattern(
|
||||||
|
regexp = "^[a-zA-Z\\p{script=Han}]+$",
|
||||||
|
message = "用户名只能包含大小写英文字母和汉字,不能使用数字和标点符号"
|
||||||
|
)
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
|
@NotBlank(message = "邮箱不能为空")
|
||||||
|
@Email(message = "邮箱格式不正确,请输入有效的邮箱地址")
|
||||||
private String email;
|
private String email;
|
||||||
|
|
||||||
|
@NotBlank(message = "密码不能为空")
|
||||||
|
@Size(min = 6, max = 30, message = "密码长度必须在6到30位之间")
|
||||||
private String password;
|
private String password;
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
|
@ -14,4 +14,8 @@ public interface UserRepository extends JpaRepository<UserEntity, Integer> {
|
|||||||
boolean existsByEmail(String email);
|
boolean existsByEmail(String email);
|
||||||
|
|
||||||
Optional<UserEntity> findByEmail(String email);
|
Optional<UserEntity> findByEmail(String email);
|
||||||
|
|
||||||
|
boolean existsByName(String name);
|
||||||
|
|
||||||
|
Optional<UserEntity> findByName(String name);
|
||||||
}
|
}
|
@ -3,8 +3,6 @@ package co.jp.app.service;
|
|||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
|
||||||
import co.jp.app.common.ResultCode;
|
|
||||||
import co.jp.app.exception.BusinessException;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.springframework.security.core.GrantedAuthority;
|
import org.springframework.security.core.GrantedAuthority;
|
||||||
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
||||||
@ -16,8 +14,10 @@ import org.springframework.security.crypto.password.PasswordEncoder;
|
|||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import co.jp.app.common.ResultCode;
|
||||||
import co.jp.app.dto.RegistrationDto;
|
import co.jp.app.dto.RegistrationDto;
|
||||||
import co.jp.app.entity.UserEntity;
|
import co.jp.app.entity.UserEntity;
|
||||||
|
import co.jp.app.exception.BusinessException;
|
||||||
import co.jp.app.repository.UserRepository;
|
import co.jp.app.repository.UserRepository;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@ -31,18 +31,12 @@ public class UserService implements UserDetailsService {
|
|||||||
this.passwordEncoder = passwordEncoder;
|
this.passwordEncoder = passwordEncoder;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
|
||||||
public UserEntity registerNewUser(@NotNull RegistrationDto registrationDto) throws BusinessException {
|
public UserEntity registerNewUser(@NotNull RegistrationDto registrationDto) throws BusinessException {
|
||||||
|
|
||||||
if (userRepository.existsByEmail(registrationDto.getEmail())) {
|
if (userRepository.existsByEmail(registrationDto.getEmail())) {
|
||||||
throw new BusinessException(ResultCode.USER_EMAIL_ALREADY_EXISTS,"error: Email" + registrationDto.getEmail() + " had been used");
|
throw new BusinessException(ResultCode.USER_EMAIL_ALREADY_EXISTS,"error: Email" + registrationDto.getEmail() + " had been used");
|
||||||
}
|
}
|
||||||
|
|
||||||
//密码最短6位限制
|
|
||||||
if (registrationDto.getPassword() == null || registrationDto.getPassword().length() < 6) {
|
|
||||||
throw new BusinessException(ResultCode.USER_PASSWORD_TOO_SHORT);
|
|
||||||
}
|
|
||||||
|
|
||||||
UserEntity newUser = new UserEntity();
|
UserEntity newUser = new UserEntity();
|
||||||
newUser.setName(registrationDto.getName());
|
newUser.setName(registrationDto.getName());
|
||||||
newUser.setEmail(registrationDto.getEmail());
|
newUser.setEmail(registrationDto.getEmail());
|
||||||
|
@ -1,7 +0,0 @@
|
|||||||
#Generated by Maven Integration for Eclipse
|
|
||||||
#Fri May 16 11:13:59 JST 2025
|
|
||||||
m2e.projectLocation=C\:\\Users\\ichbi\\git\\Dog-1
|
|
||||||
m2e.projectName=dog-1
|
|
||||||
groupId=co.jp.app
|
|
||||||
artifactId=dog-2
|
|
||||||
version=0.0.1-SNAPSHOT
|
|
Reference in New Issue
Block a user