Compare commits

..

No commits in common. "ed65fd0b360a5bced01200dc676242688b07a01c" and "3295a33aacbaf0f84554996c4ce72444ba6eced2" have entirely different histories.

8 changed files with 73 additions and 25 deletions

View File

@ -22,9 +22,10 @@ public class UsersController {
//添加新用户 //添加新用户
@PreAuthorize("hasRole('管理员') or hasAuthority('添加用户')") @PreAuthorize("hasRole('管理员') or hasAuthority('添加用户')")
@PostMapping("/add") @PostMapping("/add")
public Users addUsers(@RequestBody @NotNull Users user, public Users addUsers(@RequestBody @NotNull wrapperUserAndPassword wrapperUserAndPassword) {
@RequestParam(name = "password") @NotNull String password) { return usersService.addUser(
return usersService.addUser(user, password); wrapperUserAndPassword.user,
wrapperUserAndPassword.userPassword);
} }
@Data @Data

View File

@ -10,15 +10,15 @@ public interface UsersPasswordRepository
extends JpaRepository<UsersPassword, Long> { 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); int updatePassword(String password, Long userID);
//删除账户时删除密码 //删除账户时删除密码
void deleteByUserId(Long userID); void deleteByUsersId(Long userID);
//根据用户ID查找密码 //根据用户ID查找密码
UsersPassword findByUserId(Long userID); UsersPassword findByUsersId(Long userID);
//根据用户名查找密码 //根据用户名查找密码
UsersPassword findByUserName(String userName); UsersPassword findByUsersName(String userName);
} }

View File

@ -18,12 +18,7 @@ public class UsersPassword {
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY) @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "usersId", referencedColumnName = "id") @JoinColumn(name = "usersId", referencedColumnName = "id")
private Users user; private Users users;
private String password; private String password;
public UsersPassword(Users user, String password) {
this.user = user;
this.password = password;
}
} }

View File

@ -9,7 +9,7 @@ import java.util.Optional;
public interface UsersService extends UserDetailsService { public interface UsersService extends UserDetailsService {
Users addUser(Users user, String password); Users addUser(Users user, UsersPassword userPassword);
void deleteUserById(Long userId); void deleteUserById(Long userId);

View File

@ -15,6 +15,7 @@ import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User; import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@ -24,6 +25,7 @@ import java.util.List;
import java.util.Optional; import java.util.Optional;
@Service @Service
@Component
public class UsersServiceImpl implements UsersService { public class UsersServiceImpl implements UsersService {
@Resource @Resource
private UsersRepository usersRepository; private UsersRepository usersRepository;
@ -37,9 +39,8 @@ public class UsersServiceImpl implements UsersService {
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
//添加用户时同步添加密码 //添加用户时同步添加密码
public Users addUser(Users user, String password) { public Users addUser(Users user, UsersPassword userPassword) {
Users u = usersRepository.save(user); Users u = usersRepository.save(user);
UsersPassword userPassword = new UsersPassword(u, password);
usersPasswordRepository.save(userPassword); usersPasswordRepository.save(userPassword);
return u; return u;
} }
@ -48,8 +49,8 @@ public class UsersServiceImpl implements UsersService {
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
//删除用户时同步删除密码 //删除用户时同步删除密码
public void deleteUserById(Long userId) { public void deleteUserById(Long userId) {
usersPasswordRepository.deleteByUserId(userId);
usersRepository.deleteById(userId); usersRepository.deleteById(userId);
usersPasswordRepository.deleteByUsersId(userId);
} }
@Override @Override
@ -63,7 +64,7 @@ public class UsersServiceImpl implements UsersService {
//获取用户ID //获取用户ID
userId = usersRepository.findByName(name).get().getId(); userId = usersRepository.findByName(name).get().getId();
//删除密码 //删除密码
usersPasswordRepository.deleteByUserId(userId); usersPasswordRepository.deleteByUsersId(userId);
} }
} }
@ -108,7 +109,7 @@ public class UsersServiceImpl implements UsersService {
throw new UsernameNotFoundException("用户名不存在"); throw new UsernameNotFoundException("用户名不存在");
} }
// 取出密码 // 取出密码
String password = usersPasswordRepository.findByUserId(user.getId()).getPassword(); String password = usersPasswordRepository.findByUsersId(user.getId()).getPassword();
// 用户角色 // 用户角色
Roles role = user.getRoles(); Roles role = user.getRoles();

View 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>
<!-- &lt;!&ndash; 连接数据库的基本参数 &ndash;&gt;-->
<!-- <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>-->
<!-- &lt;!&ndash; 配置Hibernate的方言 &ndash;&gt;-->
<!-- <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>-->
<!-- &lt;!&ndash; 可选配置================ &ndash;&gt;-->
<!-- &lt;!&ndash; 打印SQL &ndash;&gt;-->
<!-- <property name="hibernate.show_sql">true</property>-->
<!-- &lt;!&ndash; 格式化SQL &ndash;&gt;-->
<!-- <property name="hibernate.format_sql">true</property>-->
<!-- &lt;!&ndash; 自动创建表 &ndash;&gt;-->
<!-- <property name="hibernate.hbm2ddl.auto">update</property>-->
<!-- &lt;!&ndash; 配置C3P0连接池 &ndash;&gt;-->
<!-- <property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>-->
<!-- &lt;!&ndash;在连接池中可用的数据库连接的最少数目 &ndash;&gt;-->
<!-- <property name="c3p0.min_size">5</property>-->
<!-- &lt;!&ndash;在连接池中所有数据库连接的最大数目 &ndash;&gt;-->
<!-- <property name="c3p0.max_size">20</property>-->
<!-- &lt;!&ndash;设定数据库连接的过期时间,以秒为单位,-->
<!-- 如果连接池中的某个数据库连接处于空闲状态的时间超过了timeout时间,就会从连接池中清除 &ndash;&gt;-->
<!-- <property name="c3p0.timeout">120</property>-->
<!-- &lt;!&ndash;每3000秒检查所有连接池中的空闲连接 以秒为单位&ndash;&gt;-->
<!-- <property name="c3p0.idle_test_period">3000</property>-->
<!-- &lt;!&ndash; 设置事务隔离级别 &ndash;&gt;-->
<!-- <property name="hibernate.connection.isolation">4</property>-->
<!-- &lt;!&ndash; 配置当前线程绑定的Session &ndash;&gt;-->
<!-- <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>

View File

@ -103,7 +103,7 @@ class UsersControllerTests {
user.setAddress("test address"); user.setAddress("test address");
UsersPassword userPassword = new UsersPassword(); UsersPassword userPassword = new UsersPassword();
userPassword.setPassword("test1234567"); userPassword.setPassword("test1234567");
userPassword.setUser(user); userPassword.setUsers(user);
UsersController.wrapperUserAndPassword usersAndPassword = new UsersController.wrapperUserAndPassword(); UsersController.wrapperUserAndPassword usersAndPassword = new UsersController.wrapperUserAndPassword();
usersAndPassword.setUser(user); usersAndPassword.setUser(user);
@ -139,7 +139,7 @@ class UsersControllerTests {
user.setAddress("test address"); user.setAddress("test address");
UsersPassword userPassword = new UsersPassword(); UsersPassword userPassword = new UsersPassword();
userPassword.setPassword("test1234567"); userPassword.setPassword("test1234567");
userPassword.setUser(user); userPassword.setUsers(user);
// Convert to JSON // Convert to JSON
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();

View File

@ -61,7 +61,7 @@ class UsersServiceTests {
user1.setAddress("test address user1"); user1.setAddress("test address user1");
user1Password = new UsersPassword(); user1Password = new UsersPassword();
user1Password.setUser(user1); user1Password.setUsers(user1);
user1Password.setPassword("TestPassword1"); user1Password.setPassword("TestPassword1");
user2 = new Users(); user2 = new Users();
@ -71,7 +71,7 @@ class UsersServiceTests {
user2.setAddress("test address user2"); user2.setAddress("test address user2");
user2Password = new UsersPassword(); user2Password = new UsersPassword();
user2Password.setUser(user2); user2Password.setUsers(user2);
user2Password.setPassword("TestPassword2"); user2Password.setPassword("TestPassword2");
usersList.add(user1); usersList.add(user1);
@ -99,14 +99,14 @@ class UsersServiceTests {
@Rollback(value = true) @Rollback(value = true)
void testSaveUser() { void testSaveUser() {
when(usersRepository.save(any())).thenReturn(user1); when(usersRepository.save(any())).thenReturn(user1);
usersService.addUser(user1, user1Password.getPassword()); usersService.addUser(user1, user1Password);
verify(usersRepository, times(1)).save(any()); verify(usersRepository, times(1)).save(any());
} }
@Test @Test
@Rollback(value = true) @Rollback(value = true)
void testGetAllUsers() { void testGetAllUsers() {
usersService.addUser(user1, user1Password.getPassword()); usersService.addUser(user1, user1Password);
when(usersRepository.findAll()).thenReturn(usersList); when(usersRepository.findAll()).thenReturn(usersList);
List<Users> usersList1 = usersService.findAllUsers(); List<Users> usersList1 = usersService.findAllUsers();
Assertions.assertEquals(usersList1, usersList); Assertions.assertEquals(usersList1, usersList);