移除autowired

This commit is contained in:
2025-05-09 15:35:13 +09:00
parent 320a344c04
commit f335896f4f
4 changed files with 16 additions and 22 deletions

View File

@ -7,7 +7,7 @@ import java.util.List;
import co.jp.app.dto.RegistrationDto;
import co.jp.app.entity.UserEntity;
import co.jp.app.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.jetbrains.annotations.NotNull;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
@ -27,17 +27,16 @@ public class UserService implements UserDetailsService {
private final UserRepository userRepository;
private final PasswordEncoder passwordEncoder;
@Autowired
public UserService(UserRepository userRepository, PasswordEncoder passwordEncoder) {
this.userRepository = userRepository;
this.passwordEncoder = passwordEncoder;
}
@Transactional
public UserEntity registerNewUser(RegistrationDto registrationDto) throws Exception {
public UserEntity registerNewUser(@NotNull RegistrationDto registrationDto) throws Exception {
if (userRepository.existsByEmail(registrationDto.getEmail())) {
throw new Exception("错误:邮箱 " + registrationDto.getEmail() + " 已被注册!");
throw new Exception("エラー:メール:" + registrationDto.getEmail() + " はすでに登録されました。");
}
UserEntity newUser = new UserEntity();
@ -52,7 +51,7 @@ public class UserService implements UserDetailsService {
@Transactional(readOnly = true)
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
UserEntity userEntity = userRepository.findByEmail(email)
.orElseThrow(() -> new UsernameNotFoundException("未找到邮箱为: " + email + " 的用户"));
.orElseThrow(() -> new UsernameNotFoundException(email + " not found"));
Collection<? extends GrantedAuthority> authorities = Collections.singletonList(new SimpleGrantedAuthority("ROLE_USER")); // 示例给所有用户一个ROLE_USER权限
@ -66,9 +65,4 @@ public class UserService implements UserDetailsService {
authorities // 用户的权限集合
);
}
public boolean checkPassword(UserEntity user, String rawPassword) {
return passwordEncoder.matches(rawPassword, user.getPassword());
}
}