2025-05-07 17:56:35 +09:00
|
|
|
|
package co.jp.app.service;
|
|
|
|
|
|
2025-05-08 15:54:59 +09:00
|
|
|
|
import java.util.Collection;
|
|
|
|
|
import java.util.Collections;
|
2025-05-07 17:56:35 +09:00
|
|
|
|
|
2025-05-08 15:54:59 +09:00
|
|
|
|
import co.jp.app.dto.RegistrationDto;
|
2025-05-07 17:56:35 +09:00
|
|
|
|
import co.jp.app.entity.UserEntity;
|
|
|
|
|
import co.jp.app.repository.UserRepository;
|
2025-05-09 15:35:13 +09:00
|
|
|
|
import org.jetbrains.annotations.NotNull;
|
2025-05-08 15:54:59 +09:00
|
|
|
|
import org.springframework.security.core.GrantedAuthority;
|
|
|
|
|
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
|
|
|
|
import org.springframework.security.core.userdetails.User;
|
|
|
|
|
import org.springframework.security.core.userdetails.UserDetails;
|
|
|
|
|
import org.springframework.security.core.userdetails.UserDetailsService;
|
|
|
|
|
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
|
|
|
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
2025-05-07 17:56:35 +09:00
|
|
|
|
|
2025-05-08 15:54:59 +09:00
|
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
2025-05-07 17:56:35 +09:00
|
|
|
|
|
|
|
|
|
@Service
|
2025-05-08 15:54:59 +09:00
|
|
|
|
public class UserService implements UserDetailsService {
|
2025-05-07 17:56:35 +09:00
|
|
|
|
|
2025-05-08 15:54:59 +09:00
|
|
|
|
private final UserRepository userRepository;
|
|
|
|
|
private final PasswordEncoder passwordEncoder;
|
2025-05-07 17:56:35 +09:00
|
|
|
|
|
2025-05-08 15:54:59 +09:00
|
|
|
|
public UserService(UserRepository userRepository, PasswordEncoder passwordEncoder) {
|
|
|
|
|
this.userRepository = userRepository;
|
2025-05-08 11:26:28 +09:00
|
|
|
|
this.passwordEncoder = passwordEncoder;
|
2025-05-07 17:56:35 +09:00
|
|
|
|
}
|
|
|
|
|
|
2025-05-08 15:54:59 +09:00
|
|
|
|
@Transactional
|
2025-05-09 15:35:13 +09:00
|
|
|
|
public UserEntity registerNewUser(@NotNull RegistrationDto registrationDto) throws Exception {
|
2025-05-07 17:56:35 +09:00
|
|
|
|
|
2025-05-08 15:54:59 +09:00
|
|
|
|
if (userRepository.existsByEmail(registrationDto.getEmail())) {
|
2025-05-09 15:35:13 +09:00
|
|
|
|
throw new Exception("エラー:メール:" + registrationDto.getEmail() + " はすでに登録されました。");
|
2025-05-08 15:54:59 +09:00
|
|
|
|
}
|
2025-05-07 17:56:35 +09:00
|
|
|
|
|
|
|
|
|
UserEntity newUser = new UserEntity();
|
2025-05-08 15:54:59 +09:00
|
|
|
|
newUser.setName(registrationDto.getName());
|
|
|
|
|
newUser.setEmail(registrationDto.getEmail());
|
|
|
|
|
newUser.setPassword(passwordEncoder.encode(registrationDto.getPassword()));
|
|
|
|
|
|
|
|
|
|
return userRepository.save(newUser);
|
|
|
|
|
}
|
2025-05-07 17:56:35 +09:00
|
|
|
|
|
2025-05-08 15:54:59 +09:00
|
|
|
|
@Override
|
|
|
|
|
@Transactional(readOnly = true)
|
|
|
|
|
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
|
|
|
|
|
UserEntity userEntity = userRepository.findByEmail(email)
|
2025-05-09 15:35:13 +09:00
|
|
|
|
.orElseThrow(() -> new UsernameNotFoundException(email + " not found"));
|
2025-05-07 17:56:35 +09:00
|
|
|
|
|
2025-05-08 15:54:59 +09:00
|
|
|
|
Collection<? extends GrantedAuthority> authorities = Collections.singletonList(new SimpleGrantedAuthority("ROLE_USER")); // 示例:给所有用户一个ROLE_USER权限
|
2025-05-07 17:56:35 +09:00
|
|
|
|
|
2025-05-08 15:54:59 +09:00
|
|
|
|
return new User(
|
|
|
|
|
userEntity.getEmail(),
|
|
|
|
|
userEntity.getPassword(),
|
|
|
|
|
true, // enabled
|
|
|
|
|
true, // accountNonExpired
|
|
|
|
|
true, // credentialsNonExpired
|
|
|
|
|
true, // accountNonLocked
|
|
|
|
|
authorities // 用户的权限集合
|
|
|
|
|
);
|
2025-05-07 17:56:35 +09:00
|
|
|
|
}
|
|
|
|
|
}
|