userName
This commit is contained in:
@ -1,4 +1,4 @@
|
|||||||
activeProfiles=pom.xml
|
activeProfiles=
|
||||||
eclipse.preferences.version=1
|
eclipse.preferences.version=1
|
||||||
resolveWorkspaceProjects=true
|
resolveWorkspaceProjects=true
|
||||||
version=1
|
version=1
|
||||||
|
@ -28,7 +28,7 @@ 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_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"),
|
||||||
|
@ -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,11 +31,11 @@ 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.existsByName(registrationDto.getName())) {
|
||||||
throw new BusinessException(ResultCode.USER_EMAIL_ALREADY_EXISTS,"error: Email" + registrationDto.getEmail() + " had been used");
|
throw new BusinessException(ResultCode.USER_USERNAME_ALREADY_EXISTS,"error: Name" + registrationDto.getName() + " had been used");
|
||||||
}
|
}
|
||||||
|
|
||||||
//密码最短6位限制
|
//密码最短6位限制
|
||||||
@ -51,17 +51,19 @@ public class UserService implements UserDetailsService {
|
|||||||
return userRepository.save(newUser);
|
return userRepository.save(newUser);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Transactional(readOnly = true)
|
@Transactional(readOnly = true)
|
||||||
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
|
public UserDetails loadUserByUsername(String name) throws UsernameNotFoundException {
|
||||||
UserEntity userEntity = userRepository.findByEmail(email)
|
UserEntity userEntity = userRepository.findByName(name)
|
||||||
.orElseThrow(() -> new UsernameNotFoundException(email + " not found"));
|
.orElseThrow(() -> new UsernameNotFoundException(name + " not found"));
|
||||||
|
|
||||||
Collection<? extends GrantedAuthority> authorities = Collections
|
Collection<? extends GrantedAuthority> authorities = Collections
|
||||||
.singletonList(new SimpleGrantedAuthority("ROLE_USER"));
|
.singletonList(new SimpleGrantedAuthority("ROLE_USER"));
|
||||||
|
|
||||||
return new User(
|
return new User(
|
||||||
userEntity.getEmail(),
|
userEntity.getName(),
|
||||||
userEntity.getPassword(),
|
userEntity.getPassword(),
|
||||||
true, // enabled
|
true, // enabled
|
||||||
true, // accountNonExpired
|
true, // accountNonExpired
|
||||||
|
@ -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