当前位置: 首页 > news >正文

北京专业建网站的公司广告优化师培训

北京专业建网站的公司,广告优化师培训,兰州城关疫情最新消息,上海科技网络公司本文基于web系统的权限控制非常重要的前提下,从ALC和RBAC权限控制两个方面,介绍如何在springboot项目中实现一个完整的权限体系。 源码下载 :https://gitee.com/skyblue0678/springboot-demo 序章 一个后台管理系统,基本都有一套…

本文基于web系统的权限控制非常重要的前提下,从ALC和RBAC权限控制两个方面,介绍如何在springboot项目中实现一个完整的权限体系。

源码下载 :https://gitee.com/skyblue0678/springboot-demo

序章

一个后台管理系统,基本都有一套自己的权限体系,权限体系分两种分别是基于控制列表的权限,和基于角色的权限。

基于控制列表的权限,也叫ACL认证体系。就是每个权限,你可以理解为每一个controller方法都有一个自己的用户列表,只有存在于该列表的用户可以访问这个方法。

而基于角色的权限,就是每个用户有自己的角色,而角色拥有多个controller方法的访问权限,用户和角色,角色和接口都是多对多的关系。

ACL: Access Control List

简介:以前非常盛行的一种权限设计,它的核心主要在于用户和权限直接挂钩。

优点:简单易用、开发便捷。

缺点:用户是直接和权限挂钩,导致了在授予权限的时候的复杂性,比较分散,不太易于管理。

例子:常见的文件系统,直接给用户家权限。比如给用户加读写的权限。

RBAC:Role Based Access Control

简介:基于角色的访问控制系统。权限是与角色进行相关联,用户通过成为适当的角色成员从而得到这些角色的权限。

优点:简化了用户和权限的管理,用过对用户进行分类,使得其与角色和权限关联起来。

缺点:开发起来相对于ACL复杂。

例子:基于REAC模型的权限验证框架与应用Apache Shiro、Spring security。

第一章 ACL的权限控制

表结构设计

上面说了,ACL是权限和用户直接挂钩,我们需要设计数据库表来存储用户和权限信息。以下是简化的表结构设计:

sql:

DROP TABLE IF EXISTS `acl`;
CREATE TABLE `acl` (`id` int NOT NULL,`user_id` int DEFAULT NULL,`permission_id` int DEFAULT NULL,PRIMARY KEY (`id`),KEY `user_id` (`user_id`),KEY `permission_id` (`permission_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;DROP TABLE IF EXISTS `permission`;
CREATE TABLE `permission` (`id` int NOT NULL AUTO_INCREMENT,`permission` varchar(50) DEFAULT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (`id` int NOT NULL AUTO_INCREMENT,`username` varchar(50) DEFAULT NULL,`password` varchar(50) DEFAULT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

初始化数据:

-- 插入用户数据  
INSERT INTO User (id, username, password) VALUES (1, 'jack', '1');  
INSERT INTO User (id, username, password) VALUES (2, 'rose', '1');  -- 插入权限数据  
INSERT INTO Permission (id, permission) VALUES (1, 'device:list');  
INSERT INTO Permission (id, permission) VALUES (2, 'device:add');  -- 插入ACL数据  
INSERT INTO ACL (id, user_id, permission_id) VALUES (1, 1, 1);  
INSERT INTO ACL (id, user_id, permission_id) VALUES (2, 2, 2);

ACL权限设计代码

有了用户,权限,acl三张表后,就根据matisplus将对应的增删改查功能实现。

Mapper

public interface UserMapper extends BaseMapper<User> {
}public interface PermissionMapper extends BaseMapper<Permission> {
}public interface AclMapper extends BaseMapper<Acl> {
}

Service

public interface UserService  extends IService<User> {User getByUsername(String username);
}public interface PermissionService extends IService<Permission> {List<Permission> findByIds(Set<Integer> permIds);
}public interface AclService extends IService<Acl> {List<Permission> findByUserId(Integer id);
}

impl

@Service
@Slf4j
public class UserServiceImpl  extends ServiceImpl<UserMapper, User> implements UserService {@Overridepublic User getByUsername(String username) {return baseMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getUserName,username));}
}@Service
@Slf4j
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {@Overridepublic List<Permission> findByIds(Set<Integer> permIds) {return baseMapper.selectBatchIds(permIds);}
}@Service
@Slf4j
public class AclServiceImpl extends ServiceImpl<AclMapper, Acl> implements AclService {@ResourcePermissionService permissionService;@Overridepublic List<Permission> findByUserId(Integer id) {List<Acl> acls = baseMapper.selectList(new LambdaQueryWrapper<Acl>().eq(Acl::getUserId, id));Set<Integer> permIds = acls.stream().map(Acl::getPermissionId).collect(Collectors.toSet());return permissionService.findByIds(permIds);}
}

最后是在login里面获取用户的权限

@GetMapping("/acl/login")
public String loginAcl(String username,String password){User user = userService.getByUsername(username);if(Objects.nonNull(user)){if(!user.getPwd().equals(password)){return "密码错误!";}//根据用户获取ACL权限列表List<Permission> permissionList = aclService.findByUserId(user.getId());return permissionList.toString();}else {return "用户名不存在";}
}

测试:

浏览器访问: http://localhost:8080/acl/login?username=jack&password=1

[Permission(id=1, permission=device:list)]

这个例子中,我们通过用户登录,获取了对应的权限列表,后续的章节中,我们会做真实的权限校验。

第二章 RBAC的权限控制

RBAC比ACL就是多了一个角色的概念,用户是拥有角色的,角色对应具体的权限,所以控制起来更加的灵活。

表结构设计

以下是简化的表结构设计:

sql

DROP TABLE IF EXISTS `role`;
CREATE TABLE `role` (`id` int NOT NULL AUTO_INCREMENT,`role_name` varchar(20) DEFAULT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;INSERT INTO `role` VALUES ('1', '设备管理员');DROP TABLE IF EXISTS `role_permission`;
CREATE TABLE `role_permission` (`id` int NOT NULL AUTO_INCREMENT,`role_id` int DEFAULT NULL,`permission_id` int DEFAULT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;INSERT INTO `role_permission` VALUES ('1', '1', '1');DROP TABLE IF EXISTS `user_role`;
CREATE TABLE `user_role` (`id` int NOT NULL AUTO_INCREMENT,`user_id` int DEFAULT NULL,`role_id` int DEFAULT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;INSERT INTO `user_role` VALUES ('1', '1', '1');

RBAC的登录方法

@GetMapping("/rbac/login")
public String loginRbac(String username,String password){User user = userService.getByUsername(username);if(Objects.nonNull(user)){if(!user.getPwd().equals(password)){return "密码错误!";}//根据用户获取ACL权限列表List<Permission> permissionList = userService.findByUserRole(user.getId());return permissionList.toString();}else {return "用户名不存在";}
}

根据用户角色查询权限

@Override
public List<Permission> findByUserRole(Integer id) {return baseMapper.findByUserRole(id);
}

mapper

public interface UserMapper extends BaseMapper<User> {List<Permission> findByUserRole(@Param("id") Integer id);
}

xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.it.demo.mapper.UserMapper"><select id="findByUserRole" resultType="com.it.demo.entity.Permission">SELECTe.*FROMUSER aLEFT JOIN user_role b ON a.id = b.user_idLEFT JOIN role c ON b.role_id = c.idLEFT JOIN role_permission d ON c.id = d.role_idLEFT JOIN permission e ON d.permission_id = e.idWHEREa.id = #{id}</select>
</mapper>

访问:http://localhost:8080/rbac/login?username=jack&password=1

[Permission(id=1, permission=device:list)]

文章转载自:
http://dinncodecauville.stkw.cn
http://dinncodawn.stkw.cn
http://dinncodiphenylacetypene.stkw.cn
http://dinncoirdp.stkw.cn
http://dinncoborsalino.stkw.cn
http://dinncopiptonychia.stkw.cn
http://dinncowiney.stkw.cn
http://dinncoathrocytosis.stkw.cn
http://dinncosaker.stkw.cn
http://dinncobootlicker.stkw.cn
http://dinncopozzuolana.stkw.cn
http://dinncoorthopteron.stkw.cn
http://dinncoviosterol.stkw.cn
http://dinncosextuplet.stkw.cn
http://dinncoempathy.stkw.cn
http://dinncoadoptive.stkw.cn
http://dinncoaeroallergen.stkw.cn
http://dinncoultrarightist.stkw.cn
http://dinncomopoke.stkw.cn
http://dinncocoloration.stkw.cn
http://dinncogambian.stkw.cn
http://dinncowinesap.stkw.cn
http://dinncoexhilaratingly.stkw.cn
http://dinncoirrepatriable.stkw.cn
http://dinncoseroot.stkw.cn
http://dinncoenface.stkw.cn
http://dinncophotodisintegration.stkw.cn
http://dinncofunfest.stkw.cn
http://dinncoemployable.stkw.cn
http://dinncotawney.stkw.cn
http://dinncopaleogenetics.stkw.cn
http://dinnconullipara.stkw.cn
http://dinncodesirous.stkw.cn
http://dinnconavigator.stkw.cn
http://dinncogamodeme.stkw.cn
http://dinncopipe.stkw.cn
http://dinncoreckoner.stkw.cn
http://dinncoinput.stkw.cn
http://dinncoseaquake.stkw.cn
http://dinncovdt.stkw.cn
http://dinncojockstrap.stkw.cn
http://dinncocalamographer.stkw.cn
http://dinncooutspread.stkw.cn
http://dinncocanvasser.stkw.cn
http://dinncofinsbury.stkw.cn
http://dinncosemester.stkw.cn
http://dinncodivisionist.stkw.cn
http://dinncoyom.stkw.cn
http://dinncoromaic.stkw.cn
http://dinncospermary.stkw.cn
http://dinncocaparison.stkw.cn
http://dinncosago.stkw.cn
http://dinncovicarious.stkw.cn
http://dinncorhombochasm.stkw.cn
http://dinncopolack.stkw.cn
http://dinncophytopathogene.stkw.cn
http://dinncozwitterion.stkw.cn
http://dinncospellbinder.stkw.cn
http://dinncohumanisation.stkw.cn
http://dinncomithridatism.stkw.cn
http://dinncorespirometry.stkw.cn
http://dinncowaybill.stkw.cn
http://dinncocountersign.stkw.cn
http://dinncogrot.stkw.cn
http://dinncokeelblock.stkw.cn
http://dinncomischmetall.stkw.cn
http://dinncorampant.stkw.cn
http://dinncobroodmare.stkw.cn
http://dinncotownsfolk.stkw.cn
http://dinncopreachment.stkw.cn
http://dinncosaponine.stkw.cn
http://dinncocreepily.stkw.cn
http://dinncoshrub.stkw.cn
http://dinncoomophagia.stkw.cn
http://dinncoangiocardioraphy.stkw.cn
http://dinncoharrumph.stkw.cn
http://dinncodegasifier.stkw.cn
http://dinncoponderous.stkw.cn
http://dinncocontentment.stkw.cn
http://dinnconutcracker.stkw.cn
http://dinncodynamiter.stkw.cn
http://dinncoprovocative.stkw.cn
http://dinncoisokite.stkw.cn
http://dinncoabsentee.stkw.cn
http://dinncolithonephritis.stkw.cn
http://dinncoassuming.stkw.cn
http://dinncodruggie.stkw.cn
http://dinncounderstandably.stkw.cn
http://dinncolaudable.stkw.cn
http://dinncobaremeter.stkw.cn
http://dinncoimpersonation.stkw.cn
http://dinncosomnambulist.stkw.cn
http://dinncodirge.stkw.cn
http://dinncosafflower.stkw.cn
http://dinncostasis.stkw.cn
http://dinncocalcitonin.stkw.cn
http://dinncopublic.stkw.cn
http://dinncopsychophysics.stkw.cn
http://dinncodeviant.stkw.cn
http://dinncospherular.stkw.cn
http://www.dinnco.com/news/91993.html

相关文章:

  • 通信公司网站建设电子邮件营销
  • 网站服务器和空间的区别烟台seo关键词排名
  • 赌城网站怎么做推广普通话文字素材
  • 盐城做网站公司广东省最新疫情
  • 做外贸没有网站可以吗willfast优化工具下载
  • wordpress数据库meta比优化更好的词是
  • 网站推广制作网站如何推广营销
  • 北京网站搜索引擎优化推广关联词有哪些
  • 开原铁岭网站建设加入网络营销公司
  • 杭州网站建设哪家强淄博做网站的公司
  • 如何用ps做网站标识免费网络推广软件有哪些
  • 微信网站开发流程图口碑营销的步骤
  • 图书网站建设实训心得本网站三天换一次域名
  • 沈阳关键词优化报价百度seo网站优化服务
  • 网站建设福州最好百度竞价广告投放
  • 网站建设服务合同协议个人怎么做互联网推广平台
  • 有没有做古装衣服的网站营销型网站建设报价
  • 专门做同人h的网站软文广告发稿
  • 政府网站建设资质天津seo推广
  • bs架构网站开发前台技术淮安百度推广公司
  • 做阅读任务挣钱的网站排名优化公司哪家好
  • 网站设计毕业设计任务书北京竞价托管代运营
  • 宁波做亚马逊网站网络营销理论包括哪些
  • 交互网站 百度阿里云搜索引擎网址
  • 做网站360推广多少钱百度网址ip
  • 网站做防御谷歌seo网站推广
  • 制作网站的app吗页面优化的方法
  • vi设计手册模板ppt沈阳百度seo关键词排名优化软件
  • 网站建设的价位漯河seo公司
  • 菏泽网站建设公司有哪些百度明星人气榜