Home > Java > javaTutorial > What is the method of permission management in Java front-end and back-end separation?

What is the method of permission management in Java front-end and back-end separation?

WBOY
Release: 2023-05-13 15:28:15
forward
1696 people have browsed it

1. Front-end interface

1.1 Button

With the help of the table part in elementui

<template slot-scope="scope">
    <el-button @click="permissionClick(scope.row)" type="primary" size="mini">修改权限222</el-button>
</template>
Copy after login

1.2 Dialog tree control

With the help of the table part in elementui Dialog and tree structure

 <!--自写权限222-->
        <el-dialog
                title="提示"
                :visible.sync="dialogPerVisible"
                width="30%"
                >
            <!--
                :default-expanded-keys="[2, 3]"默认展开项
                :default-checked-keys="[5]"默认选中项
            -->
            <el-tree
                    :data="treeData"
                    show-checkbox
                    node-key="id"
                    :props="defaultProps">
            </el-tree>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogPerVisible = false">取 消</el-button>
                <el-button type="primary" @click="dialogPerVisible = false">确 定</el-button>
            </span>
        </el-dialog>
Copy after login
 data(){
            return{
                //自写权限树遮罩层
                dialogPerVisible:false,
                treeData:[],
                defaultProps: {
                    children: &#39;children&#39;,
                //如果不显示命名,注意看请求结果是否为label不是则修改‘label&#39;中的内容
                    label: &#39;label&#39;
                }
            }
        }
Copy after login
methods:{
            //自写权限点击
            permissionClick(row){
              this.dialogPerVisible=true;
              this.$http.get("/system/permission/findPermessionByRoleId/"+row.id).then(result=>{
                  this.treeData=result.data.data.treeData;
              })
            }
        }
Copy after login

2. Back-end operation

2.1 controller layer

//自写权限树
    @GetMapping("findPermessionByRoleId/{roleId}")
    public CommonResult findPermessionByRoleId(@PathVariable String roleId){
        return iPermissionService.findPermessionByRoleId(roleId);
    }
Copy after login
Copy after login

2.2 serviceImpl layer

Use TODO here to do it later Search whether this business is completed

What is the method of permission management in Java front-end and back-end separation?

 //自写树结构的获取
    @Override
    public CommonResult findPermessionByRoleId(String roleId) {
        //查询所有的权限
        QueryWrapper<Permission> wrapper=new QueryWrapper<>();
        //逻辑删除列考虑在内 还有一个状态列数据库暂未考虑
        wrapper.eq("is_deleted",0);
        List<Permission> permissionList = permissionMapper.selectList(wrapper);
        //设置层级关系
        List<Permission> firstMenus=new ArrayList<>();
        for (Permission first:permissionList) {
            //int
            if(first.getPid().equals("1")){
                firstMenus.add(first);
            }
        }
        //为一级菜单设置二级菜单
        for (Permission first : firstMenus) {
            //根据一级菜单id 查询 该菜单的二级菜单,如果出现不确定有几级菜单 那么我们可以使用方法的递归调用
            first.setChildren(findChildren(permissionList,first.getId()));
        }
        //TODO根据角色查询该角色具有的权限id
        Map<String,Object> map=new HashMap<>();
        //treeData为前端要接收的值
        map.put("treeData",firstMenus);
        return new CommonResult(2000,"查询成功",map);
    }
    //方法递归
    public void getCheckKey(Permission p,List<String> list){
        if(p.getChildren() == null || p.getChildren().size() == 0){
            list.add(p.getId());
            return;
        }
        List<Permission> children = p.getChildren();
        for (Permission per : children){
            getCheckKey(per, list);
        }
    }
Copy after login

2.3 Result display

What is the method of permission management in Java front-end and back-end separation?

What is the method of permission management in Java front-end and back-end separation?

2.4 Check the corresponding permission menu (using an intermediate table)

2.4.1 Back-end processing (permission echo)

Use mybatis-plus to generate an intermediate table (rolePermission)

Relative Previously added the content of querying the permission id of the role based on the role

//调中间层
    @Autowired
    private IRolePermissionService iRolePermissionService;
    //自写树结构的获取
    @Override
    public CommonResult findPermessionByRoleId(String roleId) {
        //查询所有的权限
        QueryWrapper<Permission> wrapper=new QueryWrapper<>();
        //逻辑删除列考虑在内 还有一个状态列数据库暂未考虑
        wrapper.eq("is_deleted",0);
        List<Permission> permissionList = permissionMapper.selectList(wrapper);
        //设置层级关系
        List<Permission> firstMenus=new ArrayList<>();
        for (Permission first:permissionList) {
            //int
            if(first.getPid().equals("1")){
                firstMenus.add(first);
            }
        }
        //为一级菜单设置二级菜单
        for (Permission first : firstMenus) {
            //根据一级菜单id 查询 该菜单的二级菜单,如果出现不确定有几级菜单 那么我们可以使用方法的递归调用
            first.setChildren(findChildren(permissionList,first.getId()));
        }
        //根据角色查询该角色具有的权限id
        QueryWrapper<RolePermission> wrapper1=new QueryWrapper<>();
        //根据角色id获得权限
        wrapper1.eq("role_id",roleId);
        List<RolePermission> list = iRolePermissionService.list(wrapper1);
        //由集合转换为查询permissionId
        List<String> collect = list.stream().map(item -> item.getPermissionId()).distinct().collect(Collectors.toList());
        Map<String,Object> map=new HashMap<>();
        //treeData为前端要接收的值
        map.put("treeData",firstMenus);
        map.put("checkIds",collect);
        return new CommonResult(2000,"查询成功",map);
    }
    //方法递归
    public void getCheckKey(Permission p,List<String> list){
        if(p.getChildren() == null || p.getChildren().size() == 0){
            list.add(p.getId());
            return;
        }
        List<Permission> children = p.getChildren();
        for (Permission per : children){
            getCheckKey(per, list);
        }
    }
Copy after login
2.4.2 Front-end processing

What is the method of permission management in Java front-end and back-end separation?##

methods:{
            //自写权限点击
            permissionClick(row){
              this.dialogPerVisible=true;
              this.$http.get("/system/permission/findPermessionByRoleId/"+row.id).then(result=>{
                  this.treeData=result.data.data.treeData;
                  setTimeout(()=>{
                      result.data.data.checkIds.forEach(value=>{
                          this.$refs.rootTree.setChecked(value,true,false);
                      })
                  },100)
              })
            }
}
Copy after login

Click the confirmation processing of the mask layer

<el-button type="primary" @click="confirmFen()">确 定</el-button>
Copy after login

Add role id

What is the method of permission management in Java front-end and back-end separation?

When you click OK

 methods:{
            //自写权限遮罩层确定
            confirmFen(){
                //1.获取全选和半选的树 获取对象
               var checkedNodes = this.$refs.rootTree.getCheckedNodes(false,true);
               //console.log(checkedNodes)
                var ids=[];
                checkedNodes.forEach(item=>{
                    ids.push(item.id);
                })
                //console.log(ids)
                this.$http.post("/system/rolePermission/"+this.roleId,ids).then(result=>{
                    if(result.data.code===2000){
                        this.dialogPerVisible=false;
                        this.$message.success("分配权限成功");
                    }
                })
            }
}
Copy after login

The result printed by console.log (the second ids)

What is the method of permission management in Java front-end and back-end separation?

2.4.3 Back-end processing (determine modification permissions)
controller layer

//自写权限树
    @GetMapping("findPermessionByRoleId/{roleId}")
    public CommonResult findPermessionByRoleId(@PathVariable String roleId){
        return iPermissionService.findPermessionByRoleId(roleId);
    }
Copy after login
Copy after login

serviceImpl layer

 @Override
    @Transactional//事务
    public CommonResult fen(String roleId, List<String> ids) {
        //删除roleid对应的权限
        QueryWrapper<RolePermission> wrapper=new QueryWrapper<>();
        wrapper.eq("role_id",roleId);
        this.remove(wrapper);
        //添加
        List<RolePermission> collect = ids.stream().map(item -> new RolePermission(null, roleId, item, 0, LocalDateTime.now(), LocalDateTime.now())).collect(Collectors.toList());
        this.saveBatch(collect);
        return new CommonResult(2000,"分配成功",null);
    }
Copy after login
Entity class addition

What is the method of permission management in Java front-end and back-end separation?

Startup class added

What is the method of permission management in Java front-end and back-end separation?

The above is the detailed content of What is the method of permission management in Java front-end and back-end separation?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template