Compare commits
No commits in common. "ed65fd0b360a5bced01200dc676242688b07a01c" and "3295a33aacbaf0f84554996c4ce72444ba6eced2" have entirely different histories.
ed65fd0b36
...
3295a33aac
@ -22,9 +22,10 @@ public class UsersController {
|
||||
//添加新用户
|
||||
@PreAuthorize("hasRole('管理员') or hasAuthority('添加用户')")
|
||||
@PostMapping("/add")
|
||||
public Users addUsers(@RequestBody @NotNull Users user,
|
||||
@RequestParam(name = "password") @NotNull String password) {
|
||||
return usersService.addUser(user, password);
|
||||
public Users addUsers(@RequestBody @NotNull wrapperUserAndPassword wrapperUserAndPassword) {
|
||||
return usersService.addUser(
|
||||
wrapperUserAndPassword.user,
|
||||
wrapperUserAndPassword.userPassword);
|
||||
}
|
||||
|
||||
@Data
|
||||
|
@ -10,15 +10,15 @@ public interface UsersPasswordRepository
|
||||
extends JpaRepository<UsersPassword, Long> {
|
||||
|
||||
//修改密码
|
||||
@Query("update UsersPassword UsersPwd set UsersPwd.password = ?1 where UsersPwd.user.id = ?2")
|
||||
@Query("update UsersPassword UsersPwd set UsersPwd.password = ?1 where UsersPwd.users.id = ?2")
|
||||
int updatePassword(String password, Long userID);
|
||||
|
||||
//删除账户时删除密码
|
||||
void deleteByUserId(Long userID);
|
||||
void deleteByUsersId(Long userID);
|
||||
|
||||
//根据用户ID查找密码
|
||||
UsersPassword findByUserId(Long userID);
|
||||
UsersPassword findByUsersId(Long userID);
|
||||
|
||||
//根据用户名查找密码
|
||||
UsersPassword findByUserName(String userName);
|
||||
UsersPassword findByUsersName(String userName);
|
||||
}
|
||||
|
@ -18,12 +18,7 @@ public class UsersPassword {
|
||||
|
||||
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "usersId", referencedColumnName = "id")
|
||||
private Users user;
|
||||
private Users users;
|
||||
|
||||
private String password;
|
||||
|
||||
public UsersPassword(Users user, String password) {
|
||||
this.user = user;
|
||||
this.password = password;
|
||||
}
|
||||
}
|
||||
|
@ -9,7 +9,7 @@ import java.util.Optional;
|
||||
|
||||
public interface UsersService extends UserDetailsService {
|
||||
|
||||
Users addUser(Users user, String password);
|
||||
Users addUser(Users user, UsersPassword userPassword);
|
||||
|
||||
void deleteUserById(Long userId);
|
||||
|
||||
|
@ -15,6 +15,7 @@ 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.UsernameNotFoundException;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@ -24,6 +25,7 @@ import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
@Component
|
||||
public class UsersServiceImpl implements UsersService {
|
||||
@Resource
|
||||
private UsersRepository usersRepository;
|
||||
@ -37,9 +39,8 @@ public class UsersServiceImpl implements UsersService {
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
//添加用户时同步添加密码
|
||||
public Users addUser(Users user, String password) {
|
||||
public Users addUser(Users user, UsersPassword userPassword) {
|
||||
Users u = usersRepository.save(user);
|
||||
UsersPassword userPassword = new UsersPassword(u, password);
|
||||
usersPasswordRepository.save(userPassword);
|
||||
return u;
|
||||
}
|
||||
@ -48,8 +49,8 @@ public class UsersServiceImpl implements UsersService {
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
//删除用户时同步删除密码
|
||||
public void deleteUserById(Long userId) {
|
||||
usersPasswordRepository.deleteByUserId(userId);
|
||||
usersRepository.deleteById(userId);
|
||||
usersPasswordRepository.deleteByUsersId(userId);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -63,7 +64,7 @@ public class UsersServiceImpl implements UsersService {
|
||||
//获取用户ID
|
||||
userId = usersRepository.findByName(name).get().getId();
|
||||
//删除密码
|
||||
usersPasswordRepository.deleteByUserId(userId);
|
||||
usersPasswordRepository.deleteByUsersId(userId);
|
||||
}
|
||||
}
|
||||
|
||||
@ -108,7 +109,7 @@ public class UsersServiceImpl implements UsersService {
|
||||
throw new UsernameNotFoundException("用户名不存在");
|
||||
}
|
||||
// 取出密码
|
||||
String password = usersPasswordRepository.findByUserId(user.getId()).getPassword();
|
||||
String password = usersPasswordRepository.findByUsersId(user.getId()).getPassword();
|
||||
|
||||
// 用户角色
|
||||
Roles role = user.getRoles();
|
||||
|
51
src/main/resources/hibernate.cfg.xml
Normal file
51
src/main/resources/hibernate.cfg.xml
Normal file
@ -0,0 +1,51 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE hibernate-configuration PUBLIC
|
||||
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
|
||||
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
|
||||
<hibernate-configuration>
|
||||
<session-factory>
|
||||
<!-- <!– 连接数据库的基本参数 –>-->
|
||||
<!-- <property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>-->
|
||||
<!-- <property name="hibernate.connection.url">jdbc:sqlserver://intpointer.com:1433;\-->
|
||||
<!-- databaseName=Elm;\-->
|
||||
<!-- encrypt=true;\-->
|
||||
<!-- trustServerCertificate=true;\-->
|
||||
<!-- loginTimeout=30;-->
|
||||
<!-- </property>-->
|
||||
<!-- <property name="hibernate.connection.username">myh</property>-->
|
||||
<!-- <property name="hibernate.connection.password">20231103#MS_Sql</property>-->
|
||||
<!-- <!– 配置Hibernate的方言 –>-->
|
||||
<!-- <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>-->
|
||||
|
||||
<!-- <!– 可选配置================ –>-->
|
||||
<!-- <!– 打印SQL –>-->
|
||||
<!-- <property name="hibernate.show_sql">true</property>-->
|
||||
<!-- <!– 格式化SQL –>-->
|
||||
<!-- <property name="hibernate.format_sql">true</property>-->
|
||||
<!-- <!– 自动创建表 –>-->
|
||||
<!-- <property name="hibernate.hbm2ddl.auto">update</property>-->
|
||||
|
||||
<!-- <!– 配置C3P0连接池 –>-->
|
||||
<!-- <property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>-->
|
||||
<!-- <!–在连接池中可用的数据库连接的最少数目 –>-->
|
||||
<!-- <property name="c3p0.min_size">5</property>-->
|
||||
<!-- <!–在连接池中所有数据库连接的最大数目 –>-->
|
||||
<!-- <property name="c3p0.max_size">20</property>-->
|
||||
<!-- <!–设定数据库连接的过期时间,以秒为单位,-->
|
||||
<!-- 如果连接池中的某个数据库连接处于空闲状态的时间超过了timeout时间,就会从连接池中清除 –>-->
|
||||
<!-- <property name="c3p0.timeout">120</property>-->
|
||||
<!-- <!–每3000秒检查所有连接池中的空闲连接 以秒为单位–>-->
|
||||
<!-- <property name="c3p0.idle_test_period">3000</property>-->
|
||||
|
||||
<!-- <!– 设置事务隔离级别 –>-->
|
||||
<!-- <property name="hibernate.connection.isolation">4</property>-->
|
||||
<!-- <!– 配置当前线程绑定的Session –>-->
|
||||
<!-- <property name="hibernate.current_session_context_class">thread</property>-->
|
||||
|
||||
<!-- 引入映射 -->
|
||||
<!-- <mapping resource="com/itheima/hibernate/domain/Customer.hbm.xml"/>
|
||||
<mapping resource="com/itheima/hibernate/domain/LinkMan.hbm.xml"/> -->
|
||||
<!-- <mapping resource="./"/>-->
|
||||
<!-- <mapping resource="./"/>-->
|
||||
</session-factory>
|
||||
</hibernate-configuration>
|
@ -103,7 +103,7 @@ class UsersControllerTests {
|
||||
user.setAddress("test address");
|
||||
UsersPassword userPassword = new UsersPassword();
|
||||
userPassword.setPassword("test1234567");
|
||||
userPassword.setUser(user);
|
||||
userPassword.setUsers(user);
|
||||
|
||||
UsersController.wrapperUserAndPassword usersAndPassword = new UsersController.wrapperUserAndPassword();
|
||||
usersAndPassword.setUser(user);
|
||||
@ -139,7 +139,7 @@ class UsersControllerTests {
|
||||
user.setAddress("test address");
|
||||
UsersPassword userPassword = new UsersPassword();
|
||||
userPassword.setPassword("test1234567");
|
||||
userPassword.setUser(user);
|
||||
userPassword.setUsers(user);
|
||||
|
||||
// Convert to JSON
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
|
@ -61,7 +61,7 @@ class UsersServiceTests {
|
||||
user1.setAddress("test address user1");
|
||||
|
||||
user1Password = new UsersPassword();
|
||||
user1Password.setUser(user1);
|
||||
user1Password.setUsers(user1);
|
||||
user1Password.setPassword("TestPassword1");
|
||||
|
||||
user2 = new Users();
|
||||
@ -71,7 +71,7 @@ class UsersServiceTests {
|
||||
user2.setAddress("test address user2");
|
||||
|
||||
user2Password = new UsersPassword();
|
||||
user2Password.setUser(user2);
|
||||
user2Password.setUsers(user2);
|
||||
user2Password.setPassword("TestPassword2");
|
||||
|
||||
usersList.add(user1);
|
||||
@ -99,14 +99,14 @@ class UsersServiceTests {
|
||||
@Rollback(value = true)
|
||||
void testSaveUser() {
|
||||
when(usersRepository.save(any())).thenReturn(user1);
|
||||
usersService.addUser(user1, user1Password.getPassword());
|
||||
usersService.addUser(user1, user1Password);
|
||||
verify(usersRepository, times(1)).save(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Rollback(value = true)
|
||||
void testGetAllUsers() {
|
||||
usersService.addUser(user1, user1Password.getPassword());
|
||||
usersService.addUser(user1, user1Password);
|
||||
when(usersRepository.findAll()).thenReturn(usersList);
|
||||
List<Users> usersList1 = usersService.findAllUsers();
|
||||
Assertions.assertEquals(usersList1, usersList);
|
||||
|
Loading…
Reference in New Issue
Block a user