<style></style> <style></style>
一、创建权限管理服务
1、在service模块下创建子模块service-acl 2、在service_acl模块中引入依赖 1<dependencies>2
<dependency>3
<groupId>com.atguigu</groupId>4
<artifactId>spring_security</artifactId>5
<version>0.0.1-SNAPSHOT</version>6
</dependency>7
<dependency>8
<groupId>com.alibaba</groupId>9
<artifactId>fastjson</artifactId>10
</dependency>11
</dependencies>3、创建权限管理相关的表
4、复制权限管理接口代码
5、复制整合Spring Security代码 (1)在common模块下创建子模块spring_security
6、编写application.properties配置文件 1
# 服务端口2
server.port=80093
# 服务名4
spring.application.name=service-acl5
6
# mysql数据库连接7
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver8
spring.datasource.url=jdbc:mysql://localhost:3306/guli?serverTimezone=GMT%2B89
spring.datasource.username=root10
spring.datasource.password=root11
12
#返回json的全局时间格式13
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss14
spring.jackson.time-zone=GMT+815
16
spring.redis.host=192.168.44.13217
spring.redis.port=637918
spring.redis.database= 019
spring.redis.timeout=180000020
21
spring.redis.lettuce.pool.max-active=2022
spring.redis.lettuce.pool.max-wait=-123
#最大阻塞等待时间(负数表示没限制)24
spring.redis.lettuce.pool.max-idle=525
spring.redis.lettuce.pool.min-idle=026
#最小空闲27
28
#配置mapper xml文件的路径29
mybatis-plus.mapper-locations=classpath:com/atguigu/aclservice/mapper/xml/*.xml30
31
#指定注册中心地址32
eureka.client.service-url.defaultZone=http://127.0.0.1:8761/eureka/33
#eureka服务器上获取的是服务器的ip地址,否则是主机名34
eureka.instance.prefer-ip-address=true35
36
#mybatis日志37
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
二、开发权限管理接口
1、获取所有菜单 (1)controller 1@RestController2
@RequestMapping("/admin/acl/permission")3
@CrossOrigin4
public class PermissionController {5
6
@Autowired7
private PermissionService permissionService;8
9
//获取全部菜单10
@GetMapping11
public R indexAllPermission() {12
List<Permission> list = permissionService.queryAllMenu();13
return R.ok().data("children",list);14
}15
}
(2)service
1//获取全部菜单2
@Override3
public List<Permission> queryAllMenu() {4
5
QueryWrapper<Permission> wrapper = new QueryWrapper<>();6
wrapper.orderByDesc("id");7
List<Permission> permissionList = baseMapper.selectList(wrapper);8
9
List<Permission> result = bulid(permissionList);10
return result;11
}
(3)在Permission实体类添加属性
1@ApiModelProperty(value = "层级")2
@TableField(exist = false)3
private Integer level;4
5
@ApiModelProperty(value = "下级")6
@TableField(exist = false)7
private List<Permission> children;8
9
@ApiModelProperty(value = "是否选中")10
@TableField(exist = false)11
private boolean isSelect;
(4)编写工具类,根据菜单构建数据
11public class PermissionHelper {12
13
/**14
* 使用递归方法建菜单15
* @param treeNodes16
* @return17
*/18
public static List<Permission> bulid(List<Permission> treeNodes) {19
List<Permission> trees = new ArrayList<>();20
for (Permission treeNode : treeNodes) {21
if ("0".equals(treeNode.getPid())) {22
treeNode.setLevel(1);23
trees.add(findChildren(treeNode,treeNodes));24
}25
}26
return trees;27
}28
29
/**30
* 递归查找子节点31
* @param treeNodes32
* @return33
*/34
public static Permission findChildren(Permission treeNode,List<Permission> treeNodes) {35
treeNode.setChildren(new ArrayList<Permission>());36
37
for (Permission it : treeNodes) {38
if(treeNode.getId().equals(it.getPid())) {39
int level = treeNode.getLevel() + 1;40
it.setLevel(level);41
if (treeNode.getChildren() == null) {42
treeNode.setChildren(new ArrayList<>());43
}44
treeNode.getChildren().add(findChildren(it,treeNodes));45
}46
}47
return treeNode;48
}
2、递归删除菜单
(1)controller 1@ApiOperation(value = "递归删除菜单")2
@DeleteMapping("remove/{id}")3
public R remove(@PathVariable String id) {4
permissionService.removeChildById(id);5
return R.ok();6
}
(2)service
1//递归删除菜单2
@Override3
public void removeChildById(String id) {4
List<String> idList = new ArrayList<>();5
this.selectChildListById(id, idList);6
//把根据节点id放到list中7
idList.add(id);8
baseMapper.deleteBatchIds(idList);9
}10
11
/**12
*递归获取子节点13
* @param id14
* @param idList15
*/16
private void selectChildListById(String id, List<String> idList) {17
List<Permission> childList = baseMapper.selectList(new QueryWrapper<Permission>().eq("pid", id).select("id"));18
childList.stream().forEach(item -> {19
idList.add(item.getId());20
this.selectChildListById(item.getId(), idList);21
});22
}
3、给角色分配权限
(1)controller 1@ApiOperation(value = "给角色分配权限")2
@PostMapping("/doAssign")3
public R doAssign(String roleId,String[] permissionId) {4
permissionService.saveRolePermissionRealtionShip(roleId,permissionId);5
return R.ok();6
}
(2)service
1//给角色分配权限2
@Override3
public void saveRolePermissionRealtionShip(String roleId, String[] permissionIds) {4
5
rolePermissionService.remove(new QueryWrapper<RolePermission>().eq("role_id", roleId));6
7
List<RolePermission> rolePermissionList = new ArrayList<>();8
for(String permissionId : permissionIds) {9
if(StringUtils.isEmpty(permissionId)) continue;10
RolePermission rolePermission = new RolePermission();11
rolePermission.setRoleId(roleId);12
rolePermission.setPermissionId(permissionId);13
rolePermissionList.add(rolePermission);14
}15
rolePermissionService.saveBatch(rolePermissionList);16
}