08 权限管理-后端接口整合.png

一、创建权限管理服务

1、在service模块下创建子模块service-acl

c95daf5d-e202-4192-b8d9-e4d9939c36c9.png

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、创建权限管理相关的表

a61fda92-3d24-47b6-8861-b9923e82a3f4.png
0fa35326-b294-4a7c-b1fc-12e32e714d63.jpg

4、复制权限管理接口代码

4a7f8a4f-548d-4c06-844e-54654c4fa686.png

5、复制整合Spring Security代码

(1)在common模块下创建子模块spring_security
e08063de-10fe-4e92-a675-584deb4efb1f.png**

6、编写application.properties配置文件

  1. # 服务端口
  2. server.port=8009
  3. # 服务名
  4. spring.application.name=service-acl
  5. # mysql数据库连接
  6. spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
  7. spring.datasource.url=jdbc:mysql://localhost:3306/guli?serverTimezone=GMT%2B8
  8. spring.datasource.username=root
  9. spring.datasource.password=root
  10. #返回json的全局时间格式
  11. spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
  12. spring.jackson.time-zone=GMT+8
  13. spring.redis.host=192.168.44.132
  14. spring.redis.port=6379
  15. spring.redis.database= 0
  16. spring.redis.timeout=1800000
  17. spring.redis.lettuce.pool.max-active=20
  18. spring.redis.lettuce.pool.max-wait=-1
  19. #最大阻塞等待时间(负数表示没限制)
  20. spring.redis.lettuce.pool.max-idle=5
  21. spring.redis.lettuce.pool.min-idle=0
  22. #最小空闲
  23. #配置mapper xml文件的路径
  24. mybatis-plus.mapper-locations=classpath:com/atguigu/aclservice/mapper/xml/*.xml
  25. #指定注册中心地址
  26. eureka.client.service-url.defaultZone=http://127.0.0.1:8761/eureka/
  27. #eureka服务器上获取的是服务器的ip地址,否则是主机名
  28. eureka.instance.prefer-ip-address=true
  29. #mybatis日志
  30. mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

二、开发权限管理接口

1、获取所有菜单

09 权限管理-递归查询菜单.png

(1)controller

  1. @RestController
  2. @RequestMapping("/admin/acl/permission")
  3. @CrossOrigin
  4. public class PermissionController {
  5. @Autowired
  6. private PermissionService permissionService;
  7. //获取全部菜单
  8. @GetMapping
  9. public R indexAllPermission() {
  10. List<Permission> list = permissionService.queryAllMenu();
  11. return R.ok().data("children",list);
  12. }
  13. }

(2)service

  1. //获取全部菜单
  2. @Override
  3. public List<Permission> queryAllMenu() {
  4. QueryWrapper<Permission> wrapper = new QueryWrapper<>();
  5. wrapper.orderByDesc("id");
  6. List<Permission> permissionList = baseMapper.selectList(wrapper);
  7. List<Permission> result = bulid(permissionList);
  8. return result;
  9. }

(3)在Permission实体类添加属性

  1. @ApiModelProperty(value = "层级")
  2. @TableField(exist = false)
  3. private Integer level;
  4. @ApiModelProperty(value = "下级")
  5. @TableField(exist = false)
  6. private List<Permission> children;
  7. @ApiModelProperty(value = "是否选中")
  8. @TableField(exist = false)
  9. private boolean isSelect;

(4)编写工具类,根据菜单构建数据

  1. package com.atguigu.aclservice.helper;
  2. import com.atguigu.aclservice.entity.Permission;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. /**
  6. * <p>
  7. * 根据权限数据构建菜单数据
  8. * </p>
  9. */
  10. public class PermissionHelper {
  11. /**
  12. * 使用递归方法建菜单
  13. * @param treeNodes
  14. * @return
  15. */
  16. public static List<Permission> bulid(List<Permission> treeNodes) {
  17. List<Permission> trees = new ArrayList<>();
  18. for (Permission treeNode : treeNodes) {
  19. if ("0".equals(treeNode.getPid())) {
  20. treeNode.setLevel(1);
  21. trees.add(findChildren(treeNode,treeNodes));
  22. }
  23. }
  24. return trees;
  25. }
  26. /**
  27. * 递归查找子节点
  28. * @param treeNodes
  29. * @return
  30. */
  31. public static Permission findChildren(Permission treeNode,List<Permission> treeNodes) {
  32. treeNode.setChildren(new ArrayList<Permission>());
  33. for (Permission it : treeNodes) {
  34. if(treeNode.getId().equals(it.getPid())) {
  35. int level = treeNode.getLevel() + 1;
  36. it.setLevel(level);
  37. if (treeNode.getChildren() == null) {
  38. treeNode.setChildren(new ArrayList<>());
  39. }
  40. treeNode.getChildren().add(findChildren(it,treeNodes));
  41. }
  42. }
  43. return treeNode;
  44. }
  45. }

2、递归删除菜单


10 权限管理-递归删除菜单.png

(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. @Override
  3. 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. * @param id
  13. * @param idList
  14. */
  15. private void selectChildListById(String id, List<String> idList) {
  16. List<Permission> childList = baseMapper.selectList(new QueryWrapper<Permission>().eq("pid", id).select("id"));
  17. childList.stream().forEach(item -> {
  18. idList.add(item.getId());
  19. this.selectChildListById(item.getId(), idList);
  20. });
  21. }

3、给角色分配权限
image.png

(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. @Override
  3. public void saveRolePermissionRealtionShip(String roleId, String[] permissionIds) {
  4. rolePermissionService.remove(new QueryWrapper<RolePermission>().eq("role_id", roleId));
  5. List<RolePermission> rolePermissionList = new ArrayList<>();
  6. for(String permissionId : permissionIds) {
  7. if(StringUtils.isEmpty(permissionId)) continue;
  8. RolePermission rolePermission = new RolePermission();
  9. rolePermission.setRoleId(roleId);
  10. rolePermission.setPermissionId(permissionId);
  11. rolePermissionList.add(rolePermission);
  12. }
  13. rolePermissionService.saveBatch(rolePermissionList);
  14. }