mirror of https://github.com/elunez/eladmin
修复在用户管理中修改用户的角色时缓存不刷新的问题
parent
5f43a5dabc
commit
0baad04445
|
@ -74,7 +74,6 @@ public class RedisServiceImpl implements RedisService {
|
|||
jedis.close(); // 释放资源还给连接池
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -20,6 +20,11 @@ public class JwtPermissionService {
|
|||
@Autowired
|
||||
private RoleRepository roleRepository;
|
||||
|
||||
/**
|
||||
* key的名称如有修改,请同步修改 UserServiceImpl 中的 update 方法
|
||||
* @param user
|
||||
* @return
|
||||
*/
|
||||
@Cacheable(key = "'loadPermissionByUser:' + #p0.username")
|
||||
public Collection<GrantedAuthority> mapToGrantedAuthorities(User user) {
|
||||
|
||||
|
|
|
@ -10,6 +10,7 @@ import javax.validation.constraints.NotBlank;
|
|||
import javax.validation.constraints.NotNull;
|
||||
import java.io.Serializable;
|
||||
import java.sql.Timestamp;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
|
@ -69,5 +70,18 @@ public class Role implements Serializable {
|
|||
'}';
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
Role role = (Role) o;
|
||||
return Objects.equals(id, role.id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id);
|
||||
}
|
||||
|
||||
public interface Update{}
|
||||
}
|
||||
|
|
|
@ -48,11 +48,12 @@ public interface RoleService {
|
|||
void delete(Long id);
|
||||
|
||||
/**
|
||||
* key的名称如有修改,请同步修改 UserServiceImpl 中的 update 方法
|
||||
* findByUsers_Id
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
@Cacheable(keyGenerator = "keyGenerator")
|
||||
@Cacheable(key = "'findByUsers_Id:' + #p0")
|
||||
List<Role> findByUsers_Id(Long id);
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package me.zhengjie.modules.system.service.impl;
|
||||
|
||||
import me.zhengjie.modules.monitor.service.RedisService;
|
||||
import me.zhengjie.modules.system.domain.User;
|
||||
import me.zhengjie.exception.EntityExistException;
|
||||
import me.zhengjie.exception.EntityNotFoundException;
|
||||
|
@ -29,6 +30,9 @@ public class UserServiceImpl implements UserService {
|
|||
@Autowired
|
||||
private UserMapper userMapper;
|
||||
|
||||
@Autowired
|
||||
private RedisService redisService;
|
||||
|
||||
@Override
|
||||
public UserDTO findById(long id) {
|
||||
Optional<User> user = userRepository.findById(id);
|
||||
|
@ -73,6 +77,14 @@ public class UserServiceImpl implements UserService {
|
|||
throw new EntityExistException(User.class,"email",resources.getEmail());
|
||||
}
|
||||
|
||||
// 如果用户的角色改变了,需要手动清理下缓存
|
||||
if (!resources.getRoles().equals(user.getRoles())) {
|
||||
String key = "role::loadPermissionByUser:" + user.getUsername();
|
||||
redisService.delete(key);
|
||||
key = "role::findByUsers_Id:" + user.getId();
|
||||
redisService.delete(key);
|
||||
}
|
||||
|
||||
user.setUsername(resources.getUsername());
|
||||
user.setEmail(resources.getEmail());
|
||||
user.setEnabled(resources.getEnabled());
|
||||
|
|
|
@ -1,13 +1,17 @@
|
|||
package me.zhengjie;
|
||||
|
||||
import me.zhengjie.modules.monitor.service.RedisService;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class EladminSystemApplicationTests {
|
||||
@Autowired
|
||||
private RedisService redisService;
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
|
|
Loading…
Reference in New Issue