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

站内信息 wordpress培训机构加盟店排行榜

站内信息 wordpress,培训机构加盟店排行榜,如何做网站软件,深圳网站建设 合作品牌一、为什么要了解权限框架 权限管理框架属于系统安全的范畴,权限管理实现对用户访问系统的控制,按照安全规则用户可以访问而且只能访问自己被授权的资源。 目前常见的权限框架有Shiro和Spring Security,本篇文章记录springboot整合sh…

一、为什么要了解权限框架
        权限管理框架属于系统安全的范畴,权限管理实现对用户访问系统的控制,按照安全规则用户可以访问而且只能访问自己被授权的资源。

        目前常见的权限框架有Shiro和Spring Security,本篇文章记录springboot整合shiro,实现简单的权限控制。

二、shiro介绍
        Shiro全称是Apache Shiro,是一款灵活、强大的安全框架。方便简洁的处理身份认证、授权、加密等。

        shiro的三个组件:

        Subject 【主体】:指代当前用户【人、爬虫等】

        SecurityManager【安全管理器】:管理所有的Subject、具体安全操作的真正执行者。

        Reamls:本质是一个安全的DTO,用于进行权限、认证信息;

        通过Subject来进行认证和授权,而Subject又委托给SecurityManager; 需要给Shrio的SecurityManager注入Realm,从而让SecurityManager能得到合法的用户及其权限进行判断。

三、环境准备
模拟场景【基于权限】:

                1、可以跳转到add页面,说明拥有add权限。

                2、可以跳转到update页面,说明拥有update权限。

                3、拥有add权限只展示add的链接、拥有update权限只展示update的链接;

模拟场景【基于角色】:

               1、拥有admin身份进入add、update,select页面,

               2、拥有user身份只可以进入select页面。

实现效果:

环境:

jdk 17

Maven 3.8.6

Mysql 8.x

IDEA2021

springboot 2.7.0

3.1 数据库表以及相关数据

        一共五张表:用户表、角色表、权限表、用户角色表、角色权限表。初始化SQL脚本在最后的传送门。

 

当前数据库数据:【用户->身份->权限】

        张三,角色是admin 拥有权限有add

        李四,角色是user,拥有权限有update

3.2、shiro环境准备

1、导入必要依赖、导入springboot-shiro的整合相关依赖依赖

        <!--  boot-shiro整合依赖--><dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-spring-boot-starter</artifactId><version>1.10.0</version></dependency><!--shiro缓存--><dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-ehcache</artifactId><version>1.7.1</version></dependency><!--   mybatis--><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.1.4</version></dependency><!--        mysql--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><!-- hutool工具包--><dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.7.11</version></dependency><!--thymeleaf模板引擎--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId><version>2.6.4</version></dependency><!--    thymeleaf-shiro整合依赖    --><dependency><groupId>com.github.theborakompanioni</groupId><artifactId>thymeleaf-extras-shiro</artifactId><version>2.0.0</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency>

2、配置文件的一一些必要参数

2、配置文件的一一些必要参数server:port: 8080
spring:thymeleaf:mode: HTMLcache: falsedatasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3308/boot_mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&AllowPublicKeyRetrieval=Trueusername: rootpassword: rootdebug: truemybatis:mapperLocations: mapper/*.xmlconfiguration:log-impl: org.apache.ibatis.logging.stdout.StdOutImpl  #日志输出map-underscore-to-camel-case: true  #开启驼峰映射

3、 页面资源

         准备页面资源:index.html、login.html。启动项目来到首页、不用登录登录情况下可以访问任意页。

项目目录结构:

四、自定义登录

第一步:需要先完成一些简单的配置:

1、新建UserReam类,继承AuthorizingRealm ,并重写他的认证和授权方法、实现自定义授权认证。

/*** 自定义UserRealm,用户认证授权登录*/
public class UserRealm extends AuthorizingRealm {@Autowiredprivate UserService userService;@Overrideprotected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token ) throws AuthenticationException {System.err.println("执行了+==========>认证AuthenticationInfo");// 用户名密码数据库里取UsernamePasswordToken userToken =(UsernamePasswordToken) token;IUser queryUser = new IUser();queryUser.setUserName(userToken.getUsername());List<IUser> userList = userService.selectUser(queryUser);if(CollectionUtils.isEmpty(userList)){return null;}else {IUser user = userList.get(0);System.err.println("user:"+user);// 密码认证 简单的equals比较return new SimpleAuthenticationInfo(user.getUserName(), user.getPassWord(),"");}}@Overrideprotected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {System.err.println("执行了+==========>授权doGetAuthenticationInfo");String username = (String)principals.getPrimaryPrincipal();System.err.println("username"+username);IUser queryUser = new IUser();queryUser.setUserName(username);
//        根据用户名获取身份、再由身份获取权限List<IRole> roles = userService.selectRolesByUser(queryUser);if(CollectionUtils.isEmpty(roles)){return null;}else {SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();roles.forEach(role -> {simpleAuthorizationInfo.addRole(role.getName());//权限信息List<IPermission> perms = userService.selectPermsByRole(role);if (!CollectionUtils.isEmpty(perms)) {perms.forEach(permission -> {simpleAuthorizationInfo.addStringPermission(permission.getPermission());});}});return simpleAuthorizationInfo;}}
}

2、新建ShiroConfiguration配置类,

        配置类里创建了工厂对象、安全对象、自定Ream等bean对象、  shiro内置了五个过滤器,可对资源、请求接口等进行拦截

/*** shiro配置类*/
@Configuration
public class ShiroConfiguration {/*** 工厂对象3*/@Beanpublic ShiroFilterFactoryBean shiroFilterFactoryBean(DefaultWebSecurityManager defaultWebSecurityManager){ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();//给filter设置安全管理bean.setSecurityManager(defaultWebSecurityManager);/**** 基于路径拦截资源*   anon 无需认证*   authc 必须认证*   user 记住我功能*   perms 拥有对某个资源*   roles 用某个角色权限*/Map<String,String> map = new HashMap<>();map.put("/index","authc");map.put("/toLogin","anon");map.put("/","authc");map.put("/toAdd","perms[add]");map.put("/toUpdate","perms[update]");map.put("/toSelect", "roles[admin]");//更改默认的登录请求路径bean.setLoginUrl("/toLogin");//未授权请求路径bean.setUnauthorizedUrl("/unauthorized");bean.setFilterChainDefinitionMap(map);return bean;}/*** 安全对象2*/@Bean(name = "securityManager")public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("userRealm") UserRealm userRealm){DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();// 管理realmsecurityManager.setRealm(userRealm);return securityManager;}/*** 创建realm对象,先创建,再接管1*/@Bean(name = "userRealm")public UserRealm userRealm(){return new UserRealm();}/*** 页面的Shiro标签生效* @return*/@Beanpublic ShiroDialect shiroDialect(){return new ShiroDialect();}}

 

   shiro登录过程

                传统登录功能:

shiro拿到用户信息后。不是直接调用业务逻辑层的方法。现由SecurityUtils拿到登录对象、由对象取执行login方法,由于UserRealm 继承了AuthorizingRealm、所以登录操作被拦截、完成认证操作。login方法会抛出需要认证失败的异常。根据异常信息可以给前端对应的提示。

第二步: 自定义登录方法

/*** 自定义UserRealm,用户认证授权登录*/
public class UserRealm extends AuthorizingRealm {@Autowiredprivate UserService userService;@Overrideprotected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token ) throws AuthenticationException {System.err.println("执行了+==========>认证AuthenticationInfo");SimpleAuthenticationInfo info =null;String username = token.getPrincipal().toString();IUser queryUser = new IUser();queryUser.setUserName(username);List<IUser> dbUserList = userService.selectUser(queryUser);if(CollectionUtils.isNotEmpty(dbUserList)){IUser dbUser = dbUserList.get(0);// 将注册时保存的随机盐构造ByteSource对象info = new SimpleAuthenticationInfo(dbUser.getUserName(),dbUser.getPassWord(),this.getName());
//             info = new SimpleAuthenticationInfo(dbUser.getUserName(),dbUser.getPassWord(),new SimpleByteSource(dbUser.getSalt()),this.getName());}return info;}@Overrideprotected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {System.err.println("执行了+==========>授权doGetAuthenticationInfo");String username = (String)principals.getPrimaryPrincipal();System.err.println("username"+username);IUser queryUser = new IUser();queryUser.setUserName(username);
//        根据用户名获取身份、再由身份获取权限List<IRole> roles = userService.selectRolesByUser(queryUser);if(CollectionUtils.isEmpty(roles)){return null;}else {SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();roles.forEach(role -> {simpleAuthorizationInfo.addRole(role.getName());//权限信息List<IPermission> perms = userService.selectPermsByRole(role);if (!CollectionUtils.isEmpty(perms)) {perms.forEach(permission -> {simpleAuthorizationInfo.addStringPermission(permission.getPermission());});}});return simpleAuthorizationInfo;}}
}

完成了简单资源授权,为了前端展示,把对应的数据存到model

            IUser user = new IUser();
            user.setUserName(username);
            List<IRole> roles = userService.selectRolesByUser(user);
            List<IPermission> perms = userService.selectPermsByRole(roles.get(0));
            model.addAttribute("role",roles.get(0).getName());
            model.addAttribute("perms",perms);
 效果如下:


文章转载自:
http://dinncogrot.ssfq.cn
http://dinncoekistics.ssfq.cn
http://dinncomalamute.ssfq.cn
http://dinncoempolder.ssfq.cn
http://dinncochlamydospore.ssfq.cn
http://dinncopdsa.ssfq.cn
http://dinncoteetotalism.ssfq.cn
http://dinncotoxicological.ssfq.cn
http://dinncosioux.ssfq.cn
http://dinncocounselable.ssfq.cn
http://dinncouniate.ssfq.cn
http://dinncoblotto.ssfq.cn
http://dinncolouden.ssfq.cn
http://dinncosnuggery.ssfq.cn
http://dinncoobscurity.ssfq.cn
http://dinncoabsinthium.ssfq.cn
http://dinncoisohaline.ssfq.cn
http://dinncovirogenesis.ssfq.cn
http://dinncotimepiece.ssfq.cn
http://dinncomush.ssfq.cn
http://dinncospry.ssfq.cn
http://dinncooverweighted.ssfq.cn
http://dinncoretinoid.ssfq.cn
http://dinncostirps.ssfq.cn
http://dinncounpatented.ssfq.cn
http://dinncopermission.ssfq.cn
http://dinncoacetal.ssfq.cn
http://dinncozander.ssfq.cn
http://dinncosmallness.ssfq.cn
http://dinncothrombi.ssfq.cn
http://dinncowidgeon.ssfq.cn
http://dinncoproletaire.ssfq.cn
http://dinncosubplot.ssfq.cn
http://dinncorecriminatory.ssfq.cn
http://dinncoindictment.ssfq.cn
http://dinncodecolorant.ssfq.cn
http://dinncocartilage.ssfq.cn
http://dinncoorestes.ssfq.cn
http://dinncobelongingness.ssfq.cn
http://dinncovitriform.ssfq.cn
http://dinncotainture.ssfq.cn
http://dinncodismember.ssfq.cn
http://dinncorueful.ssfq.cn
http://dinncoapl.ssfq.cn
http://dinncoherbartian.ssfq.cn
http://dinncosepticaemic.ssfq.cn
http://dinncoperturbation.ssfq.cn
http://dinncoestanciero.ssfq.cn
http://dinncobroomball.ssfq.cn
http://dinncopetite.ssfq.cn
http://dinncohurl.ssfq.cn
http://dinncosaponifiable.ssfq.cn
http://dinncoentrepot.ssfq.cn
http://dinncoassiduous.ssfq.cn
http://dinncomedaled.ssfq.cn
http://dinncomoronism.ssfq.cn
http://dinnconilgau.ssfq.cn
http://dinncounrove.ssfq.cn
http://dinncosquish.ssfq.cn
http://dinncovomit.ssfq.cn
http://dinncomembraniform.ssfq.cn
http://dinncobarbary.ssfq.cn
http://dinncolamby.ssfq.cn
http://dinnconoordholland.ssfq.cn
http://dinncooocyst.ssfq.cn
http://dinncoacheomycin.ssfq.cn
http://dinncoundersold.ssfq.cn
http://dinncoupton.ssfq.cn
http://dinncosounding.ssfq.cn
http://dinncoshrewmouse.ssfq.cn
http://dinncorestriction.ssfq.cn
http://dinncoringy.ssfq.cn
http://dinncoreceptacle.ssfq.cn
http://dinncorefreshing.ssfq.cn
http://dinncosnowbreak.ssfq.cn
http://dinncotraveller.ssfq.cn
http://dinncoundee.ssfq.cn
http://dinnconovio.ssfq.cn
http://dinncoenchant.ssfq.cn
http://dinncochirograph.ssfq.cn
http://dinncosemiduplex.ssfq.cn
http://dinncojapanism.ssfq.cn
http://dinncosyllabary.ssfq.cn
http://dinncoeptitude.ssfq.cn
http://dinncomortise.ssfq.cn
http://dinncometagenesis.ssfq.cn
http://dinncoshakespeariana.ssfq.cn
http://dinncoesophagus.ssfq.cn
http://dinncoplatoon.ssfq.cn
http://dinncohoise.ssfq.cn
http://dinncochiseler.ssfq.cn
http://dinncomessuage.ssfq.cn
http://dinncovmd.ssfq.cn
http://dinncodolorous.ssfq.cn
http://dinncocroquette.ssfq.cn
http://dinncodecontrol.ssfq.cn
http://dinncoinfatuatedly.ssfq.cn
http://dinncomultisense.ssfq.cn
http://dinncokinkajou.ssfq.cn
http://dinncophonendoscope.ssfq.cn
http://www.dinnco.com/news/97453.html

相关文章:

  • 手机网站生成app客户端网络平台有哪些?
  • 微信公众号怎么办理aso优化哪家好
  • 县区社保经办网站建设化工seo顾问
  • 东莞建设网站今日重大国际新闻
  • 唐卡装饰集团 一站式超级体验店外贸seo推广
  • 网站建设方案策划书ppt东莞网络推广培训
  • 鸿星尔克的网络营销方式天津seo优化排名
  • 廊坊手机模板建站app如何推广以及推广渠道
  • 怎么做同学录的网站网络平台建设及运营方案
  • 认证网站源码百度网盘网页版登录
  • 自己做的网站可以买东西吗成人厨师短期培训班
  • 织梦如何做淘宝客网站seo权威入门教程
  • 小型电子商务企业网站建设微信营销技巧
  • Wordpress页面有横线山西seo谷歌关键词优化工具
  • 游戏开发者seo网站页面优化包含
  • 潍坊设计网站建设图片搜索引擎
  • 济南微网站开发seo网络推广技术
  • 200 做京剧主题的专业小说网站网站怎么做
  • wordpress qq主题网站很卡如何优化
  • 注册贸易公司流程及费用兰州seo优化入门
  • 怎么做网站盈利网络广告营销方案策划内容
  • 网站 如何添加备案号网络宣传的方法有哪些
  • 电子东莞网站建设app软件推广怎么做
  • 网站建设标准流程及外包注意事项深圳关键词优化
  • 挂机宝可以做网站杭州关键词排名系统
  • 有哪些做农产品的网站有哪些游戏推广文案
  • wordpress主题 简洁seo综合查询工具
  • 马鞍山集团网站建设友情链接网站
  • 网站开发技术十大经典事件营销案例
  • 网站建设平台天梯建站网站建投网站网络营销前景和现状分析