重大修改
This commit is contained in:
@ -1,15 +0,0 @@
|
||||
package co.jp.app.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
|
||||
@Configuration
|
||||
public class SecurityConfig {
|
||||
|
||||
@Bean
|
||||
public PasswordEncoder passwordEncoder() {
|
||||
return new BCryptPasswordEncoder();
|
||||
}
|
||||
}
|
@ -1,37 +0,0 @@
|
||||
package co.jp.app.controller;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.CrossOrigin;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import co.jp.app.entity.ErrorEntity;
|
||||
import co.jp.app.entity.UserEntity;
|
||||
import co.jp.app.service.ErraService;
|
||||
import co.jp.app.service.UserService;
|
||||
|
||||
|
||||
@CrossOrigin("http://192.168.1.50:5173")
|
||||
@RestController("/api/login")
|
||||
public class LoginController {
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@Autowired
|
||||
private ErraService erraService;
|
||||
|
||||
@GetMapping("/status")
|
||||
public String getStatusByNameOrEmail() {
|
||||
String input="aaa";
|
||||
|
||||
if (userByName == null && userByEmail == null) {
|
||||
return "全項目に入力してください";
|
||||
}
|
||||
|
||||
// 如果有找到,就固定使用 ID 1001 去查 erraEntity
|
||||
ErrorEntity erra = erraService.getStatusById(1001);
|
||||
|
||||
return erra.getStatus();
|
||||
}
|
||||
}
|
@ -1,23 +0,0 @@
|
||||
package co.jp.app.service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import co.jp.app.entity.PetEntity;
|
||||
import co.jp.app.repository.UploadRepository;
|
||||
|
||||
@Service
|
||||
public class UploadService {
|
||||
|
||||
@Autowired
|
||||
private UploadRepository uploadDao;
|
||||
|
||||
public List<PetEntity> saveAll(Iterable<PetEntity> entities) {
|
||||
|
||||
return uploadDao.saveAll(entities);
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,52 +1,74 @@
|
||||
package co.jp.app.service;
|
||||
|
||||
import jakarta.transaction.Transactional;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
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.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;
|
||||
|
||||
import co.jp.app.entity.UserEntity;
|
||||
import co.jp.app.repository.UserRepository;
|
||||
|
||||
import co.jp.app.entity.PetEntity;
|
||||
import co.jp.app.repository.UploadRepository;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Service
|
||||
public class UserService {
|
||||
public class UserService implements UserDetailsService {
|
||||
|
||||
private final UserRepository userEntityRepository;
|
||||
private final UserRepository userRepository;
|
||||
private final PasswordEncoder passwordEncoder;
|
||||
|
||||
@Autowired
|
||||
public UserService(UserRepository userEntityRepository, PasswordEncoder passwordEncoder ) {
|
||||
this.userEntityRepository = userEntityRepository;
|
||||
public UserService(UserRepository userRepository, PasswordEncoder passwordEncoder) {
|
||||
this.userRepository = userRepository;
|
||||
this.passwordEncoder = passwordEncoder;
|
||||
}
|
||||
|
||||
@Transactional // 整个注册过程应该是一个事务
|
||||
public UserEntity registerNewUser(String name, String email, String rawPassword) throws Exception {
|
||||
// 1. 检查邮箱是否已被注册
|
||||
if (userEntityRepository.existsByEmail(email)) {
|
||||
throw new Exception("错误:该邮箱地址已被注册!"); // 或者自定义异常
|
||||
@Transactional
|
||||
public UserEntity registerNewUser(RegistrationDto registrationDto) throws Exception {
|
||||
|
||||
if (userRepository.existsByEmail(registrationDto.getEmail())) {
|
||||
throw new Exception("错误:邮箱 " + registrationDto.getEmail() + " 已被注册!");
|
||||
}
|
||||
|
||||
// (可选) 检查用户名是否已被注册 (如果您有用户名字段)
|
||||
// if (userEntityRepository.existsByUsername(username)) {
|
||||
// throw new Exception("错误:该用户名已被注册!");
|
||||
// }
|
||||
|
||||
// 2. 创建新的 UserEntity 对象
|
||||
UserEntity newUser = new UserEntity();
|
||||
newUser.setName(name);
|
||||
newUser.setEmail(email);
|
||||
newUser.setName(registrationDto.getName());
|
||||
newUser.setEmail(registrationDto.getEmail());
|
||||
newUser.setPassword(passwordEncoder.encode(registrationDto.getPassword()));
|
||||
|
||||
// 3. 对密码进行哈希加密 (非常重要!)
|
||||
// String hashedPassword = passwordEncoder.encode(rawPassword);
|
||||
// newUser.setPassword(hashedPassword);
|
||||
newUser.setPassword(rawPassword); // 实际项目中必须加密!这里为了简化先直接赋值
|
||||
|
||||
// 4. 设置其他默认属性,例如账户状态、角色等 (如果需要)
|
||||
// newUser.setActive(true);
|
||||
// newUser.setRoles(...);
|
||||
|
||||
// 5. 保存新用户到数据库
|
||||
return userEntityRepository.save(newUser);
|
||||
return userRepository.save(newUser);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
|
||||
UserEntity userEntity = userRepository.findByEmail(email)
|
||||
.orElseThrow(() -> new UsernameNotFoundException("未找到邮箱为: " + email + " 的用户"));
|
||||
|
||||
Collection<? extends GrantedAuthority> authorities = Collections.singletonList(new SimpleGrantedAuthority("ROLE_USER")); // 示例:给所有用户一个ROLE_USER权限
|
||||
|
||||
return new User(
|
||||
userEntity.getEmail(),
|
||||
userEntity.getPassword(),
|
||||
true, // enabled
|
||||
true, // accountNonExpired
|
||||
true, // credentialsNonExpired
|
||||
true, // accountNonLocked
|
||||
authorities // 用户的权限集合
|
||||
);
|
||||
}
|
||||
|
||||
public boolean checkPassword(UserEntity user, String rawPassword) {
|
||||
return passwordEncoder.matches(rawPassword, user.getPassword());
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user