零碎权限治理

1、前言

在理论开发中,开发任何一套零碎,根本都少不了权限治理这一块。这些足以阐明权限治理的重要性。其实SpringSecurity去年就学了,始终没有工夫整顿,用了一年多工夫了,给我的印象始终都挺好,实用,安全性高(Security能够对明码进行加密)。而且这一块在理论开发中也确实很重要,所以这里整顿了一套基于SpringSecurity的权限治理。

案例代码上面有下载链接。

2、案例技术栈

如果对于SpringSecurity还不理解的话能够先理解一下SpringSecurity平安控件的学习,页面采纳的是Bootstrap写的(页面就简略的写了一下,能够依据本人的需要更改),其实后端了解了,前台就是显示作用,大家能够自行更换前台页面显示框架,长久层应用的是Spring-Data-Jpa。

并且对后端长久层和控制器进行了一下小封装,Java长久层和控制器的封装。页面应用的Thymeleaf模板,SpringBoot整合Thymeleaf模板。

数据库设计

1、表关系

  • 菜单(TbMenu)=====> 页面上须要显示的所有菜单
  • 角色(SysRole)=====> 角色及角色对应的菜单
  • 用户(SysUser)=====> 用户及用户对应的角色
  • 用户和角色两头表(sys_user_role)====> 用户和角色两头表

2、数据库表构造

菜单表tb_menu

角色及菜单权限表sys_role,其中父节点parent 为null时为角色,不为null时为对应角色的菜单权限。

用户表sys_user

用户和角色多对多关系,用户和角色两头表sys_user_role(有Spring-Data-Jpa主动生成)。

新建我的项目

1、新建springboot我的项目

新建springboot我的项目,在我的项目中增加SpringSecurity相干Maven依赖,pom.map文件

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">    <modelVersion>4.0.0</modelVersion>    <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>2.2.2.RELEASE</version>        <relativePath/> <!-- lookup parent from repository -->    </parent>    <groupId>com.mcy</groupId>    <artifactId>springboot-security</artifactId>    <version>0.0.1-SNAPSHOT</version>    <name>springboot-security</name>    <description>Demo project for Spring Boot</description>    <properties>        <java.version>1.8</java.version>    </properties>    <dependencies>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-data-jpa</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-security</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-thymeleaf</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>        </dependency>        <dependency>            <groupId>mysql</groupId>            <artifactId>mysql-connector-java</artifactId>            <scope>runtime</scope>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-test</artifactId>            <scope>test</scope>            <exclusions>                <exclusion>                    <groupId>org.junit.vintage</groupId>                    <artifactId>junit-vintage-engine</artifactId>                </exclusion>            </exclusions>        </dependency>        <dependency>            <groupId>org.springframework.security</groupId>            <artifactId>spring-security-test</artifactId>            <scope>test</scope>        </dependency>        <dependency>            <groupId>org.thymeleaf.extras</groupId>            <artifactId>thymeleaf-extras-springsecurity5</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-devtools</artifactId>            <scope>runtime</scope>            <optional>true</optional>        </dependency>        <dependency>            <groupId>org.webjars.bower</groupId>            <artifactId>bootstrap-select</artifactId>            <version>2.0.0-beta1</version>        </dependency>        <dependency>            <groupId>org.webjars</groupId>            <artifactId>bootbox</artifactId>            <version>4.4.0</version>        </dependency>    </dependencies>    <build>        <plugins>            <plugin>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-maven-plugin</artifactId>            </plugin>        </plugins>    </build></project>

2、我的项目构造

编写代码

1、编写实体类

菜单表实体类TbMenu,Spring-Data-Jpa能够依据实体类去数据库新建或更新对应的表构造,详情能够拜访Spring-Data-Jpa入门:

https://blog.csdn.net/qq_4020...
import com.fasterxml.jackson.annotation.JsonIgnore;import com.mcy.springbootsecurity.custom.BaseEntity;import org.springframework.data.annotation.CreatedBy;import javax.persistence.*;import java.util.ArrayList;import java.util.List;/** * 菜单表 * @author * */@Entitypublic class TbMenu extends BaseEntity<Integer> { private String name; private String url; private Integer idx; @JsonIgnore private TbMenu parent; @JsonIgnore private List<TbMenu> children=new ArrayList<>(); @Column(unique=true) public String getName() {  return name; } public void setName(String name) {  this.name = name; } public String getUrl() {  return url; } public void setUrl(String url) {  this.url = url; } public Integer getIdx() {  return idx; } public void setIdx(Integer idx) {  this.idx = idx; } @ManyToOne @CreatedBy public TbMenu getParent() {  return parent; } public void setParent(TbMenu parent) {  this.parent = parent; } @OneToMany(cascade=CascadeType.ALL,mappedBy="parent") @OrderBy(value="idx") public List<TbMenu> getChildren() {  return children; } public void setChildren(List<TbMenu> children) {  this.children = children; } public TbMenu(Integer id) {  super(id); } public TbMenu(){  super(); } public TbMenu(String name, String url, Integer idx, TbMenu parent, List<TbMenu> children) {  this.name = name;  this.url = url;  this.idx = idx;  this.parent = parent;  this.children = children; } public TbMenu(Integer integer, String name, String url, Integer idx, TbMenu parent, List<TbMenu> children) {  super(integer);  this.name = name;  this.url = url;  this.idx = idx;  this.parent = parent;  this.children = children; } @Transient public Integer getParentId() {  return parent==null?null:parent.getId(); }}

表新建好了,上面就是实现增删改查就能够了,实现成果如下。

新增和批改菜单。

对于Bootstrap的树形表格,能够移步到:BootStrap-bable-treegrid树形表格的应用。

https://blog.csdn.net/qq_4020...

菜单治理实现了,下一步就是实现角色及角色对应的权限治理了。

角色及权限表SysRole,parent 为null时为角色,不为null时为权限。

package com.mcy.springbootsecurity.entity;import com.fasterxml.jackson.annotation.JsonIgnore;import com.mcy.springbootsecurity.custom.BaseEntity;import org.springframework.data.annotation.CreatedBy;import javax.persistence.*;import java.util.ArrayList;import java.util.List;@Entity/*** * 角色及角色对应的菜单权限 * @author *parent 为null时为角色,不为null时为权限 */public class SysRole extends BaseEntity<Integer> { private String name; //名称 private String code; //代码 @JsonIgnore private SysRole parent; private Integer idx; //排序 @JsonIgnore private List<SysRole> children = new ArrayList<>(); @Column(length=20) public String getName() {  return name; } public void setName(String name) {  this.name = name; } public String getCode() {  return code; } public void setCode(String code) {  this.code = code; } @ManyToOne @CreatedBy public SysRole getParent() {  return parent; } public void setParent(SysRole parent) {  this.parent = parent; } @OneToMany(cascade=CascadeType.ALL,mappedBy="parent") public List<SysRole> getChildren() {  return children; } public void setChildren(List<SysRole> children) {  this.children = children; } //获取父节点id @Transient public Integer getParentId() {  return parent==null?null:parent.getId(); } public Integer getIdx() {  return idx; } public void setIdx(Integer idx) {  this.idx = idx; } public SysRole(String name, String code, SysRole parent, Integer idx, List<SysRole> children) {  this.name = name;  this.code = code;  this.parent = parent;  this.idx = idx;  this.children = children; } public SysRole(Integer id, String name, String code, SysRole parent, Integer idx, List<SysRole> children) {  super(id);  this.name = name;  this.code = code;  this.parent = parent;  this.idx = idx;  this.children = children; } public SysRole(Integer id) {  super(id); } public SysRole(){}}

首先须要实现角色治理,之后在角色中增加对应的菜单权限。

实现成果(也能够和菜单治理一样,用树形表格展现,依据集体需要。这里用的是树形菜单展现的)。

给角色调配权限。

最初实现的就是用户治理了,只须要对增加的用户调配对应的角色就能够了,用户登录时,显示角色对应的权限。

用户表SysUser,继承的BaseEntity类中就一个ID字段。

import com.fasterxml.jackson.annotation.JsonIgnore;import com.mcy.springbootsecurity.custom.BaseEntity;import javax.persistence.*;import java.util.ArrayList;import java.util.List;/** * 用户表 */@Entitypublic class SysUser extends BaseEntity<Integer> { private String username; //账号 private String password; //明码 private String name;  //姓名 private String address;  //地址 @JsonIgnore private List<SysRole> roles=new ArrayList<>(); //角色 @Column(length=20,unique=true) public String getUsername() {  return username; } public void setUsername(String username) {  this.username = username; } @Column(length=100) public String getPassword() {  return password; } public void setPassword(String password) {  this.password = password; } @Column(length=20) public String getName() {  return name; } public void setName(String name) {  this.name = name; } @ManyToMany(cascade=CascadeType.REFRESH,fetch=FetchType.EAGER) @JoinTable(name="sys_user_role",joinColumns=@JoinColumn(name="user_id"),inverseJoinColumns=@JoinColumn(name="role_id")) public List<SysRole> getRoles() {  return roles; } public void setRoles(List<SysRole> roles) {  this.roles = roles; } public String getAddress() {  return address; } public void setAddress(String address) {  this.address = address; } //角色名称 @Transient public String getRoleNames() {  String str="";  for (SysRole role : getRoles()) {   str+=role.getName()+",";  }  if(str.length()>0) {   str=str.substring(0, str.length()-1);  }  return str; } //角色代码 @Transient public String getRoleCodes() {  String str="";  for (SysRole role : getRoles()) {   str+=role.getCode()+",";  }  if(str.indexOf(",")>0) {   str=str.substring(0,str.length()-1);  }  return str; }}

用户治理就是根本的数据表格,成果如图。

2、Security配置文件

Security相干配置文件,上面两个文件如果看不懂,能够拜访SpringSecurity平安控件的学习中有具体解说。

https://blog.csdn.net/qq_4020...
package com.mcy.springbootsecurity.security;import com.mcy.springbootsecurity.service.SysUserService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Configuration;import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;import org.springframework.security.config.annotation.web.builders.HttpSecurity;import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;@Configurationpublic class WebSecurityConfig extends WebSecurityConfigurerAdapter {    @Autowired    private SysUserService userService;    /**     * 用户认证操作     * @param auth     * @throws Exception     */    @Override    protected void configure(AuthenticationManagerBuilder auth) throws Exception {        //增加用户,并给予权限        auth.inMemoryAuthentication().withUser("aaa").password("{noop}1234").roles("DIY");        //设置认证形式        auth.userDetailsService(userService).passwordEncoder(new BCryptPasswordEncoder());    }    /**     * 用户受权操作     * @param http     * @throws Exception     */    @Override    protected void configure(HttpSecurity http) throws Exception {        http.csrf().disable();    //安全器令牌        http.formLogin()                //登录申请被拦挡                .loginPage("/login").permitAll()                //设置默认登录胜利跳转页面                .successForwardUrl("/main")                .failureUrl("/login?error");   //登录失败的页面        http.authorizeRequests().antMatchers("/static/**", "/assets/**").permitAll();    //文件下的所有都能拜访        http.authorizeRequests().antMatchers("/webjars/**").permitAll();        http.logout().logoutUrl("/logout").permitAll();     //退出        http.authorizeRequests().anyRequest().authenticated();    //除此之外的都必须通过申请验证能力拜访    }}

获取登录者相干信息,工具类。

import com.mcy.springbootsecurity.entity.SysUser;import com.mcy.springbootsecurity.service.SysUserService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.security.core.GrantedAuthority;import org.springframework.security.core.context.SecurityContextHolder;import org.springframework.security.core.userdetails.UserDetails;import org.springframework.stereotype.Component;import java.util.ArrayList;import java.util.List;//创立会话,获取以后登录对象@Componentpublic class UserUtils { @Autowired private SysUserService userService; /**  * 获取以后登录者的信息  * @return 当前者信息  */ public SysUser getUser() {  //获取以后用户的用户名  String username = SecurityContextHolder.getContext().getAuthentication().getName();  SysUser user = userService.findByUsername(username);  return user; } /**  * 判断此用户中是否蕴含roleName菜单权限  * @param roleName  * @return  */ public Boolean hasRole(String roleName) {  //获取UserDetails类,  UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();  List<String> roleCodes=new ArrayList<>();  for (GrantedAuthority authority : userDetails.getAuthorities()) {   //getAuthority()返回用户对应的菜单权限   roleCodes.add(authority.getAuthority());  }  return roleCodes.contains(roleName); }}

3、动静权限菜单加载相干办法

用户表的SysUserService须要实现UserDetailsService接口,因为在SpringSecurity中配置的相干参数须要是UserDetailsService类的数据。

重写UserDetailsService接口中的loadUserByUsername办法,通过该办法查问对应的用户,返回对象UserDetails是SpringSecurity的一个外围接口。其中定义了一些能够获取用户名,明码,权限等与认证相干信息的办法。

重写的loadUserByUsername办法。

@Overridepublic UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {    //调用长久层接口findByUsername办法查问用户。    SysUser user = userRepository.findByUsername(username);    if(user == null){        throw new UsernameNotFoundException("用户名不存在");    }    //创立List汇合,用来保留用户菜单权限,GrantedAuthority对象代表赋予以后用户的权限    List<GrantedAuthority> authorities = new ArrayList<>();    //取得以后用户角色汇合    List<SysRole> roles = user.getRoles();    List<SysRole> haveRoles=new ArrayList<>();    for (SysRole role : roles) {        haveRoles.add(role);        List<SysRole> children = roleService.findByParent(role);        children.removeAll(haveRoles);        haveRoles.addAll(children);    }    for(SysRole role: haveRoles){        //将关联对象role的name属性保留为用户的认证权限        authorities.add(new SimpleGrantedAuthority(role.getName()));    }    //此处返回的是org.springframework.security.core.userdetails.User类,该类是SpringSecurity外部的实现    //org.springframework.security.core.userdetails.User类实现了UserDetails接口    return new User(user.getUsername(), user.getPassword(), authorities);}

所有性能实现了,最初就是依据角色去显示对应的菜单了。

在TbMenuService类中的findAuditMenu办法,查问以后用户所领有的权限菜单。

/** * 获取用户所领有的权限对应的菜单项 * @return */public List<TbMenu> findAuditMenu() {    List<TbMenu> menus;    //判断是否是后门用户    if(userUtils.hasRole("ROLE_DIY")){        //查问所有菜单,子菜单能够通过父级菜单的映射失去        menus = menuRepository.findByParentIsNullOrderByIdx();    }else{        //获取此用户对应的菜单权限        menus = auditMenu(menuRepository.findByParentIsNullOrderByIdx());    }    return menus;}//依据用户的菜单权限对菜单进行过滤private List<TbMenu> auditMenu(List<TbMenu> menus) {    List<TbMenu> list = new ArrayList<>();    for(TbMenu menu: menus){        String name = menu.getName();        //判断此用户是否有此菜单权限        if(userUtils.hasRole(name)){            list.add(menu);            //递归判断子菜单            if(menu.getChildren() != null && !menu.getChildren().isEmpty()) {                menu.setChildren(auditMenu(menu.getChildren()));            }        }    }    return list;}

在UserUtils工具类中的hasRole办法,判断此用户中是否蕴含roleName菜单权限。

public Boolean hasRole(String roleName) { //获取UserDetails类, UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); List<String> roleCodes=new ArrayList<>(); for (GrantedAuthority authority : userDetails.getAuthorities()) {  //getAuthority()返回用户对应的菜单权限  roleCodes.add(authority.getAuthority()); } return roleCodes.contains(roleName);}

之后在控制器中返回用户对应的菜单权限,之后在前台页面遍历就能够了。

@RequestMapping(value = "/main")public String main(ModelMap map){    //加载菜单    List<TbMenu> menus = menuService.findAuditMenu();    map.put("menus", menus);    if (menus.isEmpty()) {        return "main/main";    }    return "main/main1";}

4、首页菜单遍历

首页菜单遍历,这里应用的是LayUI菜单,如果其余框架能够自行依据页面标签法则遍历,因为页面应用的是Thymeleaf模板,不是JSP,应用遍历菜单时不是采纳的EL表达式,而是应用的Thymeleaf自带的标签表达式。

<div id="main">    <div id="main_nav">        <div class="panel-group" id="accordion" style="margin-bottom: 0;">            <div th:each="menu, menuStat: ${menus}" th:if="${menu.children.size() != 0 && menu.children != null}" class="panel panel-default">                <div class="panel-heading">                    <h4 class="panel-title">                        <p data-toggle="collapse" data-parent="#accordion" th:href="|#collapseOne${menuStat.index}|">                            <span th:text="${menu.name}">零碎设置</span><span class="caret"></span>                        </p>                    </h4>                </div>                <div th:if="${menuStat.first}" th:id="|collapseOne${menuStat.index}|" class="panel-collapse collapse collapse in">                    <div class="panel-body">                        <p th:each="subMenu:${menu.children}" th:src="${subMenu.url}" th:text="${subMenu.name}">菜单治理</p>                    </div>                </div>                <div th:if="${!menuStat.first}" th:id="|collapseOne${menuStat.index}|" class="panel-collapse collapse collapse">                    <div class="panel-body">                        <p th:each="subMenu:${menu.children}" th:src="${subMenu.url}" th:text="${subMenu.name}">菜单治理</p>                    </div>                </div>            </div>        </div>        <div id="nav_p">            <p th:each="menu:${menus}" th:if="${menu.children.size() == 0}" th:src="${menu.url}" th:text="${menu.name}">问题治理</p>        </div>    </div>    <div id="main_home">        首页内容    </div></div>

测试利用

1、对应成果展现

用户数据及对应的角色

管理员对应的菜单权限。

用户角色对应的菜单权限。

测试用户角色对应的菜单权限。

2、测试利用

用户名为admin1有管理员角色的用户登录,菜单显示。

用户名为admin2有用户角色的用户登录,菜单显示。

用户名为admin3有测试用户角色的用户登录,菜单显示。

3、案例代码下载

下载地址:https://github.com/machaoyin/...