Shiro:授权的相关实现

一、使用Shiro过滤器实现授权

设置好授权拦截跳转的请求地址

    /**
* 创建ShiroFilterFactoryBean
*/
@Bean
public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager securityManager) {
ShiroFilterFactoryBean shiroFilterFactoryBean=new ShiroFilterFactoryBean();
//设置安全管理器
shiroFilterFactoryBean.setSecurityManager(securityManager); //添加Shiro内置过滤器
/**
* Shiro内置过滤器,可以实现权限相关的拦截器
* 常用的过滤器:
* anon:无需认证(登录)可以访问
* authc:必须认证才可以访问
* user:如果使用rememberMe的功能可以直接访问
* perms:该资源必须得到资源权限才可以访问
* role:该资源必须得到角色权限才可以访问
*/
Map<String, String> filterMap= new LinkedHashMap<String,String>(); //设置拦截后跳转的请求路径
shiroFilterFactoryBean.setLoginUrl("/user/badRequest");
//设置未授权提示的页面
shiroFilterFactoryBean.setUnauthorizedUrl("/user/badRequest");
shiroFilterFactoryBean.setFilterChainDefinitionMap(filterMap);
return shiroFilterFactoryBean;
}

再使用 filterMap 去放对应的拦截即可,例如 filterMap.put("/**", "anon")

上面是认证的,如果需要授权的,相应的多给一个任意的授权字符串如 filterMap.put("/user/add", "perms[user:add]")

授权逻辑编写的位置

    /**
* 执行授权逻辑
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg0) {
System.out.println("开始执行Shiro的授权方法..."); //给资源进行授权
SimpleAuthorizationInfo info= new SimpleAuthorizationInfo(); //添加资源的授权字符串
info.addStringPermission("user:add"); return null;
}

授权字符串和之前的字符串对应

补充

    /**
* 执行认证逻辑
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken arg0) throws AuthenticationException {
System.out.println("开始执行Shiro的认证方法...");
//编写Shiro判断逻辑,判断用户名和密码
//判断用户名是否存在
UsernamePasswordToken token=(UsernamePasswordToken)arg0;
User user = userMapper.findByUsername(token.getUsername());
if(user==null)
return null;
//判断密码是否正确,参数一为认证的实体
return new SimpleAuthenticationInfo(user,user.getPassword(),"");
}

在执行认证时的SimpleAuthenticationInfo第一个传参可以用来获取当前用户

/**
* 执行授权逻辑
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg0) {
System.out.println("开始执行Shiro的授权方法..."); //给资源进行授权
SimpleAuthorizationInfo info= new SimpleAuthorizationInfo(); //添加资源的授权字符串
info.addStringPermission("user:register"); //获取当前的登录用户
User user=(User)SecurityUtils.getSubject().getPrincipal(); return null;
}

Shiro:授权的相关实现的更多相关文章

  1. Apache Shiro 使用手册(三)Shiro 授权

    授权即访问控制,它将判断用户在应用程序中对资源是否拥有相应的访问权限. 如,判断一个用户有查看页面的权限,编辑数据的权限,拥有某一按钮的权限,以及是否拥有打印的权限等等. 一.授权的三要素 授权有着三 ...

  2. Apache shiro集群实现 (四)shiro授权(Authentication)--访问控制

    Apache shiro集群实现 (一) shiro入门介绍 Apache shiro集群实现 (二) shiro 的INI配置 Apache shiro集群实现 (三)shiro身份认证(Shiro ...

  3. Shiro授权管理

    一.授权 授权,也叫访问控制,即在应用中控制谁能访问哪些资源(如访问页面/编辑数据/页面操作等).在授权中需了解的几个关键对象:主体(Subject).资源(Resource).权限(Permissi ...

  4. 转:JAVAWEB开发之权限管理(二)——shiro入门详解以及使用方法、shiro认证与shiro授权

    原文地址:JAVAWEB开发之权限管理(二)——shiro入门详解以及使用方法.shiro认证与shiro授权 以下是部分内容,具体见原文. shiro介绍 什么是shiro shiro是Apache ...

  5. Apache Shiro 使用手册(三)Shiro 授权(转发:http://kdboy.iteye.com/blog/1155450)

    授权即访问控制,它将判断用户在应用程序中对资源是否拥有相应的访问权限. 如,判断一个用户有查看页面的权限,编辑数据的权限,拥有某一按钮的权限,以及是否拥有打印的权限等等. 一.授权的三要素 授权有着三 ...

  6. Shiro授权流程

    1,授权中涉及的一些概念      [1]授权:访问控制,即在应用中认证用户能否访问的系统资源(如一个页面,一个按钮等).      [2]资源:在Web应用中反应为用户可以访问的URL.       ...

  7. (转) Apache Shiro 使用手册(三)Shiro 授权

    解惑之处: 使用冒号分隔的权限表达式是org.apache.shiro.authz.permission.WildcardPermission 默认支持的实现方式. 这里分别代表了 资源类型:操作:资 ...

  8. Shiro笔记(五)Shiro授权

    Shiro授权 也叫访问控制,即在应用中控制谁能访问那些资源(如访问页面.编辑数据.页面操作等).在授权中需要了解几个关键对象:主体(subject).资源(resource).权限(Permissi ...

  9. shiro授权-记调试过程

    根据张开涛老师的shiro教程学习过程中 感觉shiro授权这块有点绕 调试了十几遍 大概有个思路  记录一下 1.单元测试入口 2.subject().isPermitted("+user ...

随机推荐

  1. 2019-03-28 git github SSH配置,上传下载操作

    1.通过git获取scrapy源码,并安装到系统里面 https://git-scm.com/download/win 下载无脑安装啊(C:\Program Files\Git),进入git bash ...

  2. 提高生产力:Web开发基础平台WebCommon的设计和实现

    Web开发中,存在着各种各样的重复性的工作.为了提高开发效率,不在当码农,我在思考和实践如何搭建一个Web开发的基础平台. Web开发基础平台的目标和功能 1.提供一套基础的开发环境,整合了常用的框架 ...

  3. BIRT报表Cannot open the connection for the driver:org.eclipse.birt.report.data.oda.jdbc.dbprofile

    现象:报表不能打开: 找了半个小时,随手改了一下tomcat/conf文件夹下的context.xml文件 <Context xmlBlockExternal="false" ...

  4. 设计模式之二十:责任链模式(Chain of Responsibility)

    感觉这个设计模式和组合模式一样是一种非常巧妙的设计模式,在须要使用它的地方假设不使用这样的设计模式代码会变的非常复杂,可是这样的设计模式的基本原理又是非常easy的. 责任链模式: 通过使多个对象都有 ...

  5. [HTML 5] aria-hidden

    You want to use aria-hidden to prevent screen reader to access some content should be hidden from us ...

  6. Android-自己定义标题栏

    Android-自己定义标题栏 2014年4月25日 分享知识点 最近也比較多事情,想发发博客就是心有余而力不足,本篇博文主要教大家怎样实现自己定义标题栏,非常easy.那么聪明的你一下就看懂. 有兴 ...

  7. nyoj19(排列组合next_permutation(s.begin(),s.end()))

    题目意思: 从n个数中选择m个数,按字典序输出其排列. pid=19">http://acm.nyist.net/JudgeOnline/problem.php?pid=19 例: 输 ...

  8. Class example in C/C++

    class Player {  private:   int health; //these are the attributes   int strength;   int agility;  pu ...

  9. 火狐浏览器中加入httprequest的方法

    今天弄了非常久就才装好. 以下的样例是以 window为样例的,mac的也是这样, 下载好火狐之后点击右上角的菜单 想到httprequest是个插件,就点击附加组件 搜索出来之后找到httprequ ...

  10. 学习ASP.NET MVC系列 - 还有比这更简炼的吗?把复杂的事情变简单了,贡献啊!

    转自