Compare commits
3 Commits
3295a33aac
...
ed65fd0b36
Author | SHA1 | Date | |
---|---|---|---|
ed65fd0b36 | |||
aab93e0141 | |||
dbf5e8760f |
@ -22,10 +22,9 @@ public class UsersController {
|
||||
//添加新用户
|
||||
@PreAuthorize("hasRole('管理员') or hasAuthority('添加用户')")
|
||||
@PostMapping("/add")
|
||||
public Users addUsers(@RequestBody @NotNull wrapperUserAndPassword wrapperUserAndPassword) {
|
||||
return usersService.addUser(
|
||||
wrapperUserAndPassword.user,
|
||||
wrapperUserAndPassword.userPassword);
|
||||
public Users addUsers(@RequestBody @NotNull Users user,
|
||||
@RequestParam(name = "password") @NotNull String password) {
|
||||
return usersService.addUser(user, password);
|
||||
}
|
||||
|
||||
@Data
|
||||
|
@ -10,15 +10,15 @@ public interface UsersPasswordRepository
|
||||
extends JpaRepository<UsersPassword, Long> {
|
||||
|
||||
//修改密码
|
||||
@Query("update UsersPassword UsersPwd set UsersPwd.password = ?1 where UsersPwd.users.id = ?2")
|
||||
@Query("update UsersPassword UsersPwd set UsersPwd.password = ?1 where UsersPwd.user.id = ?2")
|
||||
int updatePassword(String password, Long userID);
|
||||
|
||||
//删除账户时删除密码
|
||||
void deleteByUsersId(Long userID);
|
||||
void deleteByUserId(Long userID);
|
||||
|
||||
//根据用户ID查找密码
|
||||
UsersPassword findByUsersId(Long userID);
|
||||
UsersPassword findByUserId(Long userID);
|
||||
|
||||
//根据用户名查找密码
|
||||
UsersPassword findByUsersName(String userName);
|
||||
UsersPassword findByUserName(String userName);
|
||||
}
|
||||
|
@ -18,7 +18,12 @@ public class UsersPassword {
|
||||
|
||||
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "usersId", referencedColumnName = "id")
|
||||
private Users users;
|
||||
private Users user;
|
||||
|
||||
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, UsersPassword userPassword);
|
||||
Users addUser(Users user, String password);
|
||||
|
||||
void deleteUserById(Long userId);
|
||||
|
||||
|
@ -15,7 +15,6 @@ 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;
|
||||
|
||||
@ -25,7 +24,6 @@ import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
@Component
|
||||
public class UsersServiceImpl implements UsersService {
|
||||
@Resource
|
||||
private UsersRepository usersRepository;
|
||||
@ -39,8 +37,9 @@ public class UsersServiceImpl implements UsersService {
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
//添加用户时同步添加密码
|
||||
public Users addUser(Users user, UsersPassword userPassword) {
|
||||
public Users addUser(Users user, String password) {
|
||||
Users u = usersRepository.save(user);
|
||||
UsersPassword userPassword = new UsersPassword(u, password);
|
||||
usersPasswordRepository.save(userPassword);
|
||||
return u;
|
||||
}
|
||||
@ -49,8 +48,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
|
||||
@ -64,7 +63,7 @@ public class UsersServiceImpl implements UsersService {
|
||||
//获取用户ID
|
||||
userId = usersRepository.findByName(name).get().getId();
|
||||
//删除密码
|
||||
usersPasswordRepository.deleteByUsersId(userId);
|
||||
usersPasswordRepository.deleteByUserId(userId);
|
||||
}
|
||||
}
|
||||
|
||||
@ -109,7 +108,7 @@ public class UsersServiceImpl implements UsersService {
|
||||
throw new UsernameNotFoundException("用户名不存在");
|
||||
}
|
||||
// 取出密码
|
||||
String password = usersPasswordRepository.findByUsersId(user.getId()).getPassword();
|
||||
String password = usersPasswordRepository.findByUserId(user.getId()).getPassword();
|
||||
|
||||
// 用户角色
|
||||
Roles role = user.getRoles();
|
||||
|
@ -1,51 +0,0 @@
|
||||
<?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.setUsers(user);
|
||||
userPassword.setUser(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.setUsers(user);
|
||||
userPassword.setUser(user);
|
||||
|
||||
// Convert to JSON
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
|
@ -61,7 +61,7 @@ class UsersServiceTests {
|
||||
user1.setAddress("test address user1");
|
||||
|
||||
user1Password = new UsersPassword();
|
||||
user1Password.setUsers(user1);
|
||||
user1Password.setUser(user1);
|
||||
user1Password.setPassword("TestPassword1");
|
||||
|
||||
user2 = new Users();
|
||||
@ -71,7 +71,7 @@ class UsersServiceTests {
|
||||
user2.setAddress("test address user2");
|
||||
|
||||
user2Password = new UsersPassword();
|
||||
user2Password.setUsers(user2);
|
||||
user2Password.setUser(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);
|
||||
usersService.addUser(user1, user1Password.getPassword());
|
||||
verify(usersRepository, times(1)).save(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Rollback(value = true)
|
||||
void testGetAllUsers() {
|
||||
usersService.addUser(user1, user1Password);
|
||||
usersService.addUser(user1, user1Password.getPassword());
|
||||
when(usersRepository.findAll()).thenReturn(usersList);
|
||||
List<Users> usersList1 = usersService.findAllUsers();
|
||||
Assertions.assertEquals(usersList1, usersList);
|
||||
|
Loading…
Reference in New Issue
Block a user