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;
|
|
|
|
|
import java.util.List;
|
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-08 15:54:59 +09:00
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
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 co.jp.app.entity.PetEntity;
|
|
|
|
|
import co.jp.app.repository.UploadRepository;
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
@Autowired
|
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
|
|
|
|
|
public UserEntity registerNewUser(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())) {
|
|
|
|
|
throw new Exception("错误:邮箱 " + registrationDto.getEmail() + " 已被注册!");
|
|
|
|
|
}
|
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)
|
|
|
|
|
.orElseThrow(() -> new UsernameNotFoundException("未找到邮箱为: " + email + " 的用户"));
|
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
|
|
|
|
}
|
2025-05-08 15:54:59 +09:00
|
|
|
|
|
|
|
|
|
public boolean checkPassword(UserEntity user, String rawPassword) {
|
|
|
|
|
return passwordEncoder.matches(rawPassword, user.getPassword());
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-07 17:56:35 +09:00
|
|
|
|
}
|