1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
| @Slf4j @Service public class ShiroServiceImpl implements ShiroService {
@Autowired private MenuMapper menuMapper; @Autowired private UserMapper userMapper; @Autowired private RoleMapper roleMapper;
@Override public Map<String, String> loadFilterChainDefinitionMap() { Map<String, String> filterChainDefinitionMap = new LinkedHashMap<>(); filterChainDefinitionMap.put("/swagger-ui.html","anon"); filterChainDefinitionMap.put("/swagger/**","anon"); filterChainDefinitionMap.put("/webjars/**", "anon"); filterChainDefinitionMap.put("/swagger-resources/**","anon"); filterChainDefinitionMap.put("/v2/**","anon"); filterChainDefinitionMap.put("/static/**", "anon");
filterChainDefinitionMap.put("/api/auth/login/**", "anon"); filterChainDefinitionMap.put("/api/auth/loginByQQ", "anon"); filterChainDefinitionMap.put("/api/auth/afterlogin.do", "anon"); filterChainDefinitionMap.put("/api/auth/logout", "anon"); filterChainDefinitionMap.put("/api/auth/unauth", "anon"); filterChainDefinitionMap.put("/api/auth/tokenExpired", "anon"); filterChainDefinitionMap.put("/api/auth/downline", "anon");
List<Menu> permissionList = menuMapper.selectList( null ); if ( !CollectionUtils.isEmpty( permissionList ) ) { permissionList.forEach( e -> { if ( StringUtils.isNotBlank( e.getUrl() ) ) { List<Role> roleList = roleMapper.selectRoleByMenuId( e.getId() ); StringJoiner zqRoles = new StringJoiner(",", "zqRoles[", "]"); if ( !CollectionUtils.isEmpty( roleList ) ){ roleList.forEach( f -> { zqRoles.add( f.getCode() ); }); }
filterChainDefinitionMap.put( "/api" + e.getUrl(),"authc,token,"+ zqRoles.toString() +",zqPerms[" + e.getResources() + "]" );
} }); } filterChainDefinitionMap.put("/**", "authc"); return filterChainDefinitionMap; }
@Override public void updatePermission(ShiroFilterFactoryBean shiroFilterFactoryBean, Integer roleId, Boolean isRemoveSession) { synchronized (this) { AbstractShiroFilter shiroFilter; try { shiroFilter = (AbstractShiroFilter) shiroFilterFactoryBean.getObject(); } catch (Exception e) { throw new MyException("get ShiroFilter from shiroFilterFactoryBean error!"); } PathMatchingFilterChainResolver filterChainResolver = (PathMatchingFilterChainResolver) shiroFilter.getFilterChainResolver(); DefaultFilterChainManager manager = (DefaultFilterChainManager) filterChainResolver.getFilterChainManager();
manager.getFilterChains().clear(); shiroFilterFactoryBean.getFilterChainDefinitionMap().clear(); shiroFilterFactoryBean.setFilterChainDefinitionMap(loadFilterChainDefinitionMap()); Map<String, String> chains = shiroFilterFactoryBean.getFilterChainDefinitionMap(); for (Map.Entry<String, String> entry : chains.entrySet()) { manager.createChain(entry.getKey(), entry.getValue()); } log.info("--------------- 动态生成url权限成功! ---------------");
if(roleId != null){ updatePermissionByRoleId(roleId,isRemoveSession); } } }
@Override public void updatePermissionByRoleId(Integer roleId, Boolean isRemoveSession) { List<User> userList = userMapper.selectUserByRoleId(roleId); if ( !CollectionUtils.isEmpty( userList ) ) { for (User user : userList) { ShiroUtils.deleteCache(user.getUsername(), isRemoveSession); } } log.info("--------------- 动态修改用户权限成功! ---------------"); }
}
|