|
|
|
@ -3,8 +3,6 @@ package co.jp.app.service;
|
|
|
|
|
import java.util.Collection;
|
|
|
|
|
import java.util.Collections;
|
|
|
|
|
|
|
|
|
|
import co.jp.app.common.ResultCode;
|
|
|
|
|
import co.jp.app.exception.BusinessException;
|
|
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
|
import org.springframework.security.core.GrantedAuthority;
|
|
|
|
|
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.transaction.annotation.Transactional;
|
|
|
|
|
|
|
|
|
|
import co.jp.app.common.ResultCode;
|
|
|
|
|
import co.jp.app.dto.RegistrationDto;
|
|
|
|
|
import co.jp.app.entity.UserEntity;
|
|
|
|
|
import co.jp.app.exception.BusinessException;
|
|
|
|
|
import co.jp.app.repository.UserRepository;
|
|
|
|
|
|
|
|
|
|
@Service
|
|
|
|
@ -31,11 +31,11 @@ public class UserService implements UserDetailsService {
|
|
|
|
|
this.passwordEncoder = passwordEncoder;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Transactional
|
|
|
|
|
|
|
|
|
|
public UserEntity registerNewUser(@NotNull RegistrationDto registrationDto) throws BusinessException {
|
|
|
|
|
|
|
|
|
|
if (userRepository.existsByEmail(registrationDto.getEmail())) {
|
|
|
|
|
throw new BusinessException(ResultCode.USER_EMAIL_ALREADY_EXISTS,"error: Email" + registrationDto.getEmail() + " had been used");
|
|
|
|
|
if (userRepository.existsByName(registrationDto.getName())) {
|
|
|
|
|
throw new BusinessException(ResultCode.USER_USERNAME_ALREADY_EXISTS,"error: Name" + registrationDto.getName() + " had been used");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//密码最短6位限制
|
|
|
|
@ -51,17 +51,19 @@ public class UserService implements UserDetailsService {
|
|
|
|
|
return userRepository.save(newUser);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
@Transactional(readOnly = true)
|
|
|
|
|
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
|
|
|
|
|
UserEntity userEntity = userRepository.findByEmail(email)
|
|
|
|
|
.orElseThrow(() -> new UsernameNotFoundException(email + " not found"));
|
|
|
|
|
public UserDetails loadUserByUsername(String name) throws UsernameNotFoundException {
|
|
|
|
|
UserEntity userEntity = userRepository.findByName(name)
|
|
|
|
|
.orElseThrow(() -> new UsernameNotFoundException(name + " not found"));
|
|
|
|
|
|
|
|
|
|
Collection<? extends GrantedAuthority> authorities = Collections
|
|
|
|
|
.singletonList(new SimpleGrantedAuthority("ROLE_USER"));
|
|
|
|
|
|
|
|
|
|
return new User(
|
|
|
|
|
userEntity.getEmail(),
|
|
|
|
|
userEntity.getName(),
|
|
|
|
|
userEntity.getPassword(),
|
|
|
|
|
true, // enabled
|
|
|
|
|
true, // accountNonExpired
|
|
|
|
|