在线教育项目-day18【权限控制】

 

<style></style> <style></style>

一、创建权限管理服务

1、在service模块下创建子模块service-acl 在线教育项目-day18【权限控制】   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、创建权限管理相关的表 在线教育项目-day18【权限控制】

 

 在线教育项目-day18【权限控制】

在线教育项目-day18【权限控制】4、复制权限管理接口代码 在线教育项目-day18【权限控制】

 

 


 

5、复制整合Spring Security代码 (1)在common模块下创建子模块spring_security  在线教育项目-day18【权限控制】

 

 

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

二、开发权限管理接口

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

(2)service

                      1
//获取全部菜单
2
@Override
3
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)编写工具类,根据菜单构建数据

        11
public class PermissionHelper {
12
13
    /**
14
     * 使用递归方法建菜单
15
     * @param treeNodes
16
     * @return
17
     */
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 treeNodes
32
     * @return
33
     */
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
@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
     *递归获取子节点
13
     * @param id
14
     * @param idList
15
     */
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
@Override
3
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
}
   

 

 

上一篇:在线教育项目-day18【GATEWAY网关概念】


下一篇:Day18:polymorphic and reflection