SpringSecurity权限管理系统实战—一、项目简介和开发环境准备

目录

SpringSecurity权限管理系统实战—一、项目简介和开发环境准备
SpringSecurity权限管理系统实战—二、日志、接口文档等实现
SpringSecurity权限管理系统实战—三、主要页面及接口实现
SpringSecurity权限管理系统实战—四、整合SpringSecurity(上)
SpringSecurity权限管理系统实战—五、整合SpringSecurity(下)
SpringSecurity权限管理系统实战—六、SpringSecurity整合jwt
SpringSecurity权限管理系统实战—七、处理一些问题
SpringSecurity权限管理系统实战—八、AOP 记录用户日志、异常日志

前言

博主的文笔有些差,大家多担待

一、简介

​ 在企业应用中,认证和授权是非常重要的一部分内容,业界最出名的两个框架就是大名鼎鼎的 Shiro和Spring Security。本次我选取的是和SpringBoot更好兼容的SpringSecurity。

二、什么是RBAC

​ RBAC是Role Based Access Control的缩写,是基于角色的访问控制。一般都是分为用户(user), 角色(role),权限(permission)三个实体,角色(role)和权限(permission)是多对多的 关系,用户(user)和角色(role)也是多对多的关系。用户(user)和权限(permission) 之间没有直接的关系,都是通过角色作为代理,才能获取到用户(user)拥有的权限。

​ 以下是RBAC0的模型

详细解释见

三、系统功能

  • 用户管理:提供用户的相关配置
  • 角色管理:对权限与菜单进行分配
  • 菜单管理:已实现菜单动态路由,后端可配置化,支持多级菜单
  • 字典管理:可维护常用一些固定的数据
  • 系统日志:记录用户操作日志与异常日志
  • SQL监控:采用druid 监控数据库访问性能
  • 代码生成:高灵活度生成前后端代码,减少大量重复的工作任务
  • 接口管理:方便统一查看管理接口

由于本系统是边开发写此文档的,所以可能上述的功能在后续的开发中会有删改。

四、环境搭建

本次系统非前后端分离项目,基于SpringBoot+Layui,后台模板选用的是Pear Admin Layui (我目前见过最漂亮的layui后台模板,放张图片让大家感受一下)

数据库设计

目前先这样,之后还会扩展。

在idea中新建SpringBoot项目,导入所需依赖

  
    
        
            org.springframework.boot
            spring-boot-starter-data-redis
        
        




        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        
        
        
            io.springfox
            springfox-swagger2
            2.9.2
        
        
        
            io.springfox
            springfox-swagger-ui
            2.9.2
        
        
            mysql
            mysql-connector-java
            runtime
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        
        
            org.springframework.security
            spring-security-test
            test
        
        
            org.springframework
            spring-web
            5.2.7.RELEASE
            compile
        
    

  

在项目中把架构搭好,创建对应数据库表的eneity、dao、service、controller,非常简单不想占用篇幅。需要注意的就是实体类中的create_time,和update_time,由于这两个和id是在多张表中都有出现,所以我们可以把它们抽离出来,有需要的实体类直接继承就可以了

@Data
public abstract class BaseEntity implements Serializable {

    private static final long serialVersionUID = 8925514045582235838L;
    private ID id;
    private Date createTime = new Date();
    @JsonFormat(pattern = "yyyy-MM-dd  HH:mm:ss")
    private Date updateTime = new Date();

}

再插一嘴,@Data是lambok提供的一个注解,可以用于生成实体类的get和set方法。使用需要导入maven依赖,在idea中也要安装相应插件。如何安装使用使用详见

然后将PearAdmin的资源放入templates下

那么接下来我们先把项目运行起来,在index.html中加入thymeleaf的命名空间,通过thymeleaf的语法重新引入下资源。



    
        
        
        
        
        
        
        
        
        
        
        

        
    
    
        
        
        
        

因为这里默认的index界面是console1.html,所以console1.html里的资源和json也要重新引入,这里就不放出代码了。

Pear自带了一些json数据,这里我们先用他的,把路径改成自己项目的。新建一个HelloController,在里面配置下路由

@Controller
public class HelloController {
    @GetMapping(value = "/console/console1")
    @ApiOperation(value = "转发console1请求")
    public String console1(){
        return "console/console1";
    }

    @GetMapping(value = "/system/organization")
    public String organization(){
        return "system/organization";
    }

    @GetMapping(value = "/system/user")
    public String user(){
        return "system/user";
    }

    @GetMapping(value = "/system/role")
    public String role(){
        return "system/role";
    }

    @GetMapping(value = "/system/power")
    public String power(){
        return "system/power";
    }

    @GetMapping(value = "/page/comment")
    public String comment(){
        return "page/comment";
    }
}

我们启动项目,看一下效果

Pear的菜单是通过menu.json来动态生成的。之后的这个数据需要后端返回,但是我先用这个假数据。我把我修改过的menu.json贴上来,避免有些同学页面出不来。

[{
        "id": 1,
        "title": "工作空间",
        "type": 0,
        "icon": "layui-icon layui-icon-console",
        "href": "",
        "children": [{
            "id": 0,
            "title": "控制后台",
            "icon": "layui-icon layui-icon-console",
            "type": 1,
            "openType": "_iframe",
            "href": "console/console1"
        }]
    },
    {
        "id": 4,
        "title": "系统管理",
        "icon": "layui-icon layui-icon-set-fill",
        "type": 0,
        "href": "",
        "children": [{
                "id": 44,
                "title": "部门管理",
                "icon": "layui-icon layui-icon-username",
                "type": 1,
                "openType": "_iframe",
                "href": "system/organization"
            },{
                "id": 41,
                "title": "用户管理",
                "icon": "layui-icon layui-icon-username",
                "type": 1,
                "openType": "_iframe",
                "href": "system/user"
            },
            {
                "id": 42,
                "title": "角色管理",
                "icon": "layui-icon layui-icon-user",
                "type": 1,
                "openType": "_iframe",
                "href": "system/role"
            },
            {
                "id": 43,
                "title": "权限管理",
                "icon": "layui-icon layui-icon-user",
                "type": 1,
                "openType": "_iframe",
                "href": "system/power"
            }
        ]
    },
    {
        "id": 2,
        "title": "扩展组件",
        "icon": "layui-icon layui-icon-component",
        "type": 0,
        "href": "",
        "children": [
            {
                "id": 22,
                "title": "进阶组件",
                "icon": "layui-icon layui-icon-face-smile",
                "type": 0,
                "href": "view/common/message.html",
                "children": [
                    {
                            "id": 225,
                            "title": "卡片列表",
                            "icon": "layui-icon layui-icon-face-smile",
                            "type": 1,
                            "openType": "_iframe",
                            "href": "view/common/senior/card.html"
                        },
                    {
                        "id": 224,
                        "title": "树状结构",
                        "icon": "layui-icon layui-icon-face-smile",
                        "type": 1,
                        "openType": "_iframe",
                        "href": "view/common/senior/dtree.html"
                    }
                ]
            }
        ]
    },
    {
        "id": 3,
        "title": "常用页面",
        "icon": "layui-icon layui-icon-face-cry",
        "type": 0,
        "href": "",
        "children": [{
            "id": 302,
            "title": "登录页面",
            "icon": "layui-icon layui-icon-face-smile",
            "type": 1,
            "openType": "_iframe",
            "href": "login"
        },
        {
            "id": 303,
            "title": "留言板",
            "icon": "layui-icon layui-icon-face-smile",
            "type": 1,
            "openType": "_iframe",
            "href": "page/comment"
        }
        ]
    },
    {
        "id": "error",
        "title": "错误页面",
        "icon": "layui-icon layui-icon-auz",
        "type": 0,
        "href": "",
        "children": [{
                "id": 403,
                "title": "403",
                "icon": "layui-icon layui-icon-face-smile",
                "type": 1,
                "openType": "_iframe",
                "href": "view/error/403.html"
            },
            {
                "id": 404,
                "title": "404",
                "icon": "layui-icon layui-icon-face-cry",
                "type": 1,
                "openType": "_iframe",
                "href": "view/error/404.html"
            },

            {
                "id": 500,
                "title": "500",
                "icon": "layui-icon layui-icon-face-cry",
                "type": 1,
                "openType": "_iframe",
                "href": "view/error/500.html"
            }

        ]
    }
]

五、技术栈

将会涉及到的技术栈(待完善)

1、SpringBoot

2、SpringSecurity

3、MyBatis

4、Apache Log4j2

5、JWT

6、Druid

7、Swagger

8、Redis

9、Layui

10、Pear Admin Layui

六、说明

以上源代码同步在giteegithub中,如果可以的话,请给我一个star,谢谢

七、项目截图

Admin端

你可能感兴趣的:(springboot,springsecurity,java,权限控制)