SpringBoot+Shiro+LayUI权限管理系统项目-5.实现权限管理

1.说明

只讲解关键部分,详细看源码,文章下方捐赠或QQ联系捐赠获取。

2.功能展示

SpringBoot+Shiro+LayUI权限管理系统项目-5.实现权限管理_第1张图片

3.业务模型

@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("sys_menu")
public class SysMenu implements Serializable {
     

    private static final long serialVersionUID = 1L;

    /**
     * 权限id
     */
    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;

    /**
     * 权限父id
     */
    private Integer pid;

    /**
     * 权限名称
     */
    private String menuName;

    /**
     * 权限唯一标识
     */
    private String menuCode;

    /**
     * 权限类型,0:目录;1:菜单;2:按钮
     */
    private Integer menuType;

    /**
     * 权限url
     */
    private String url;

    /**
     * 图标
     */
    private String icon;
    /**
     * 是否外部打开 0否1是
     */
    @TableField("is_outside")
    private String outside;

    /**
     * 创建时间
     */
    @TableField(fill = FieldFill.INSERT)
    private Date createTime;

    /**
     * 创建人
     */
    private String createUser;

    /**
     * 修改时间
     */
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Date updateTime;

    /**
     * 修改人
     */
    private String updateUser;

    @TableField(exist=false)
    private boolean checked;

    @TableField(exist=false)
    private String parentName;

    @TableField(exist=false)
    private boolean last;

    @TableField(exist=false)
    private List<SysMenu> children = new ArrayList<>();

}

4.控制器

@Controller
@RequestMapping("/sysMenu")
public class SysMenuController{
     
    @Autowired
    private SysMenuService menuService;

    @GetMapping("listUI")
    public String listUI(Map<String,Object> map) {
     
        List<SysMenu> list = menuService.list(null);

        List<Select2Entity> select2List = new TreeUtil().getSelectTree(list, 0);
        map.put("menus", select2List);

        return "menu/list";
    }

    @GetMapping("list")
    @ResponseBody
    public Result<List<SysMenu>> list() {
     
        List<SysMenu> list = menuService.list(null);

        return ResultUtil.ok(list);
    }

    @OperLog(operModule = "权限管理",operType = "修改",operDesc = "修改权限")
    @PostMapping("save")
    @ResponseBody
    public Result<String> add(@RequestBody SysMenu entity){
     
        if(entity.getId()==null){
     
            entity.setCreateTime(new Date(System.currentTimeMillis()));
            entity.setUpdateTime(new Date(System.currentTimeMillis()));
        }else
        {
     
            entity.setUpdateTime(new Date(System.currentTimeMillis()));
        }

        if(!menuService.saveOrUpdate(entity)){
     
            return ResultUtil.fail("保存失败!");
        }
        return ResultUtil.ok();
    }

    @OperLog(operModule = "权限管理",operType = "删除",operDesc = "删除权限")
    @PostMapping("/remove")
    @ResponseBody
    public Result<String> remove(@RequestParam Integer id) {
     
        List<SysMenu> childrens = (List<SysMenu>) menuService.listByMap(ConvertUtil.toMap("pid",(Object)id));
        if(childrens!=null && childrens.size()>0){
     
            return ResultUtil.fail("权限下包含子权限!");
        }else{
     
            menuService.removeById(id);
        }
        return ResultUtil.ok();
    }

    @GetMapping(value="{id}/select")
    public String select(Map<String,Object> map,@PathVariable(required=true) Integer id) {
     
        SysMenu sysMenu = menuService.getById(id);
        List<SysMenu> list = (List<SysMenu>) menuService.listByMap(null);
        List<Select2Entity> select2List = new TreeUtil().getSelectTree(list, 0);
        map.put("sysMenus", select2List);
        map.put("sysMenu", sysMenu);
        return "sysMenu/edit";
    }

    @GetMapping("icon")
    public String icon() {
     
        return "menu/icon";
    }

}

5.前端页面


<html xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="utf-8">
    <title>权限列表title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/static/plugin/layui/css/layui.css}" media="all">
head>
<body>
<table class="layui-hide" id="SysMenu" lay-filter="SysMenu">table>

<input type="text" id="ctx" hidden="hidden" th:value="${#request.getContextPath()}">


<div class="layui-row" id="editForm" style="display:none;">
    <div class="layui-col-md10">
        <form class="layui-form layui-from-pane" action="" style="margin-top:20px">
            <input type="text" id="id" name="id" hidden="hidden">
            <div class="layui-form-item">
                <label class="layui-form-label">权限类型label>
                <div class="layui-input-block">
                    <select name="menuType" id="menuType" lay-filter="menuType">
                        <option value="0" selected="">目录option>
                        <option value="1">菜单option>
                        <option value="2">按钮option>
                    select>
                div>
            div>
            <div class="layui-form-item" id="parentDiv">
                    <label class="layui-form-label">上级名称label>
                    <div class="layui-input-block">
                        <select style="display: none;" id="pid" name="pid">
                            <option value="0">请选择option>
                            <option th:each="menu:${menus}" th:value="${menu.id}" th:utext="${menu.text}">option>
                        select>
                    div>
            div>
            <div class="layui-form-item">
                    <label class="layui-form-label">权限名称label>
                    <div class="layui-input-block">
                        <input type="text" id="menuName" name="menuName" required lay-verify="required" autocomplete="off"
                               placeholder="" class="layui-input">
                    div>
            div>
            <div class="layui-form-item">
                    <label class="layui-form-label">权限标识label>
                    <div class="layui-input-block">
                        <input type="text" id="menuCode" name="menuCode" required lay-verify="required" autocomplete="off"
                               placeholder="" class="layui-input">
                    div>
            div>
            <div class="layui-form-item" id="iconDiv">
                    <label class="layui-form-label">图标label>
                    <div class="layui-input-block">
                        <input type="text" id="icon" name="icon" autocomplete="off"
                               placeholder="填写layui图标如:layui-icon-404" class="layui-input">
                    div>
            div>
            <div class="layui-form-item" id="urlDiv">
                    <label class="layui-form-label">资源URLlabel>
                    <div class="layui-input-block">
                        <input type="text" id="url" name="url" autocomplete="off"
                               placeholder="" class="layui-input">
                    div>
            div>
            <div class="layui-form-item" id="outsideDiv">
                <label class="layui-form-label">是否外部打开label>
                <div class="layui-input-block">
                    <input type="checkbox" id="outside" name="outside" lay-skin="switch" lay-text="ON|OFF">
                div>
            div>

            <div class="layui-form-item" style="margin-top:40px">
                <div class="layui-input-block">
                    <button class="layui-btn  layui-btn-submit " lay-submit="" lay-filter="confirm">确认button>
                    <button type="button" class="layui-btn layui-btn-primary" id="closeBtn">关闭button>
                div>
            div>
        form>
    div>
div>

6.获取源码

打赏>1元留言数字3,评论区留下邮箱发送源码 :)

在这里插入图片描述

你可能感兴趣的:(#,Spring-Boot,Java项目实战,shiro权限管理,springboot权限管理,权限管理,RBAC)