将 Shiro 作为应用的权限基础 三:基于注解实现的授权认证过程
授权即访问控制,它将判断用户在应用程序中对资源是否拥有相应的访问权限。
如,判断一个用户有查看页面的权限,编辑数据的权限,拥有某一按钮的权限等等。
一、用户权限模型
为实现一个较为灵活的用户权限数据模型,通常把用户信息单独用一个实体表示,用户权限信息用两个实体表示。
- 用户信息用 LoginAccount 表示,最简单的用户信息可能只包含用户名 loginName 及密码 password 两个属性。实际应用中可能会包含用户是否被禁用,用户信息是否过期等信息。
- 用户权限信息用 Role 与 Permission 表示,Role 与 Permission 之间构成多对多关系。Permission 可以理解为对一个资源的操作,Role 可以简单理解为 Permission 的集合。
- 用户信息与 Role 之间构成多对多关系。表示同一个用户可以拥有多个 Role,一个 Role 可以被多个用户所拥有。
权限声明及粒度
Shiro权限声明通常是使用以冒号分隔的表达式。就像前文所讲,一个权限表达式可以清晰的指定资源类型,允许的操作。同时,Shiro权限表达式支持简单的通配符,可以更加灵活的进行权限设置。
下面以实例来说明权限表达式。
可查询用户数据
User:view
可查询或编辑用户数据
User:view,edit
可对用户数据进行所有操作
User:*或 user
可编辑id为123的用户数据
User:edit:123
授权处理过程
认证通过后接受 Shiro 授权检查,授权验证时,需要判断当前角色是否拥有该权限。
只有授权通过,才可以访问受保护 URL 对应的资源,否则跳转到“未经授权页面”。
如果我们自定义Realm实现,比如我后面的例子中,自定义了ShiroDbRealm类,当访问ShiroDbRealm.doGetAuthorizationInfo()进行授权。
@Controller
@RequestMapping(value = "/user")
public class UserController { @Resource(name="userService")
private IUserService userService; /**
* 测试权限
* 只有拥有 user:create权限,才能进行注册
* @param user
* @return
*/
@RequestMapping(value = "/register")
@ResponseBody
@RequiresPermissions("user:create")
public boolean register(User user){
return userService.register(user);
}
二、授权实现
Shiro支持三种方式实现授权过程:
- 编码实现
- 注解实现
- JSP Taglig实现
1、基于编码的授权实现
1、基于权限对象的实现
创建org.apache.shiro.authz.Permission的实例,将该实例对象作为参数传递给Subject.isPermitted()进行验证。
Permission printPermission = new PrinterPermission("laserjet4400n", "print");
Subject currentUser = SecurityUtils.getSubject();
if (currentUser.isPermitted(printPermission)) {
//show the Print button
} else {
//don't show the button? Grey it out?
}
2、基于字符串的实现
相比笨重的基于对象的实现方式,基于字符串的实现便显得更加简洁。
Subject currentUser = SecurityUtils.getSubject();
if (currentUser.isPermitted("printer:print:laserjet4400n")) {
//show the Print button
} else {
//don't show the button? Grey it out?
}
使用冒号分隔的权限表达式是org.apache.shiro.authz.permission.WildcardPermission默认支持的实现方式。
这里分别代表了资源类型:操作:资源ID
2、基于注解的授权实现
Shiro注解支持AspectJ、Spring、Google-Guice等,可根据应用进行不同的配置。
相关的注解:
@RequiresAuthentication
可以用户类/属性/方法,用于表明当前用户需是经过认证的用户。
@RequiresAuthentication
public void updateAccount(Account userAccount) {
//this method will only be invoked by a
//Subject that is guaranteed authenticated
...
}
@RequiresPermissions
当前用户需拥有制定权限
@RequiresPermissions("account:create")
public void createAccount(Account account) {
//this method will only be invoked by a Subject
//that is permitted to create an account
...
}
3、基于JSP TAG的授权实现
Shiro提供了一套JSP标签库来实现页面级的授权控制。
在使用Shiro标签库前,首先需要在JSP引入shiro标签:
hasRole标签
验证当前用户是否属于该角色
<shiro:hasRole name="administrator">
<a href="admin.jsp">Administer the system</a>
</shiro:hasRole>
hasPermission标签
验证当前用户是否拥有制定权限
<shiro:hasPermission name="user:create">
<a href="createUser.jsp">Create a new User</a>
</shiro:hasPermission>
三、Shiro授权的内部处理机制
1、在应用程序中调用授权验证方法(Subject的isPermitted*或hasRole*等)
2、Sbuject会委托应用程序设置的securityManager实例调用相应的isPermitted*或hasRole*方法。
3、接下来SecurityManager会委托内置的Authorizer的实例(默认是ModularRealmAuthorizer类的实例,类似认证实例)调用相应的授权方法。
4、每一个Realm将检查是否实现了相同的Authorizer 接口。然后,将调用Reaml自己的相应的授权验证方法。
四、授权代码
UserController:处理用户登录后的请求(注册)
package org.shiro.demo.controller; import javax.annotation.Resource; import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.shiro.demo.entity.User;
import org.shiro.demo.service.IUserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; @Controller
@RequestMapping(value = "/user")
public class UserController { @Resource(name="userService")
private IUserService userService; /**
* 测试权限
* 只有拥有 user:create 权限,才能进行注册
* @param user
* @return
*/
@RequestMapping(value = "/register")
@ResponseBody
@RequiresPermissions("user:create")
public boolean register(User user){
return userService.register(user);
} /**
* 测试角色
* 只有拥有 administrator 角色,才能跳转到register页面
* @return
*/
@RequestMapping(value = "/toRegister")
@RequiresRoles("administrator")
public String toRegister(){
return "/system/user/register";
}
}
ShiroDbRealm:自定义的指定Shiro验证用户授权的类
packageorg.shiro.demo.service.realm; importjava.util.ArrayList;
importjava.util.List; importjavax.annotation.Resource; importorg.apache.commons.lang.StringUtils;
importorg.apache.shiro.authc.AuthenticationException;
importorg.apache.shiro.authc.AuthenticationInfo;
importorg.apache.shiro.authc.AuthenticationToken;
importorg.apache.shiro.authc.SimpleAuthenticationInfo;
importorg.apache.shiro.authc.UsernamePasswordToken;
importorg.apache.shiro.authz.AuthorizationException;
importorg.apache.shiro.authz.AuthorizationInfo;
importorg.apache.shiro.authz.SimpleAuthorizationInfo;
importorg.apache.shiro.realm.AuthorizingRealm;
importorg.apache.shiro.subject.PrincipalCollection;
importorg.shiro.demo.entity.Permission;
importorg.shiro.demo.entity.Role;
importorg.shiro.demo.entity.User;
importorg.shiro.demo.service.IUserService; /**
* 自定义的指定Shiro验证用户登录的类
* @author TCH
*
*/
publicclass ShiroDbRealm extends AuthorizingRealm{ //@Resource(name="userService")
privateIUserService userService; publicvoid setUserService(IUserService userService) {
this.userService= userService;
} /**
* 为当前登录的Subject授予角色和权限
* @see 经测试:本例中该方法的调用时机为需授权资源被访问时
* @see经测试:并且每次访问需授权资源时都会执行该方法中的逻辑,这表明本例未启用AuthorizationCache
* @seeweb层可以有shiro的缓存,dao层可以配有hibernate的缓存(后面介绍)
*/
protectedAuthorizationInfo doGetAuthorizationInfo(
PrincipalCollectionprincipals) { //获取当前登录的用户名,等价于(String)principals.fromRealm(this.getName()).iterator().next()
Stringaccount = (String) super.getAvailablePrincipal(principals); List<String>roles = new ArrayList<String>();
List<String>permissions = new ArrayList<String>(); //从数据库中获取当前登录用户的详细信息
Useruser = userService.getByAccount(account); if(user!= null){
//实体类User中包含有用户角色的实体类信息
if(user.getRoles() != null && user.getRoles().size() > 0) {
//获取当前登录用户的角色
for(Role role : user.getRoles()) {
roles.add(role.getName());
//实体类Role中包含有角色权限的实体类信息
if(role.getPmss() != null && role.getPmss().size() > 0) {
//获取权限
for(Permission pmss : role.getPmss()) {
if(!StringUtils.isEmpty(pmss.getPermission())){
permissions.add(pmss.getPermission());
}
}
}
}
}
}else{
thrownew AuthorizationException();
} //为当前用户设置角色和权限
SimpleAuthorizationInfoinfo = new SimpleAuthorizationInfo();
info.addRoles(roles);
info.addStringPermissions(permissions); returninfo; } }
将 Shiro 作为应用的权限基础 三:基于注解实现的授权认证过程的更多相关文章
- 将 Shiro 作为应用的权限基础 二:shiro 认证
认证就是验证用户身份的过程.在认证过程中,用户需要提交实体信息(Principals)和凭据信息(Credentials)以检验用户是否合法.最常见的“实体/凭证”组合便是“用户名/密码”组合. 一. ...
- 将 Shiro 作为应用的权限基础 一:shiro的整体架构
将 Shiro 作为应用的权限基础 一:shiro的整体架构 近来在做一个重量级的项目,其中权限.日志.报表.工作量由我负责,工作量还是蛮大的,不过想那么多干嘛,做就是了. 这段时间,接触的东西挺多, ...
- 将 Shiro 作为应用的权限基础 二:基于SpringMVC实现的认证过程
认证就是验证用户身份的过程.在认证过程中,用户需要提交实体信息(Principals)和凭据信息(Credentials)以检验用户是否合法.最常见的“实体/凭证”组合便是“用户名/密码”组合. 一. ...
- 将 Shiro 作为应用的权限基础
Shiro 是 Java 世界中新近出现的权限框架,较之 JAAS 和 Spring Security,Shiro 在保持强大功能的同时,还在简单性和灵活性方面拥有巨大优势.本文介绍了 Shiro 的 ...
- 将 Shiro 作为应用的权限基础 五:SpringMVC+Apache Shiro+JPA(hibernate)整合配置
配置web.xml,applicationContext.xml, spring-mvc.xml,applicationContext-shiro.xml,而且都有详细的说明. Web.xml是web ...
- 将 Shiro 作为应用的权限基础 四:shiro的配置说明
Apache Shiro的配置主要分为四部分: SecurityManager的配置 URL过滤器的配置 静态用户配置 静态角色配置 其中,由于用户.角色一般由后台进行操作的动态数据,比如通过@Req ...
- 【权限管理系统】Spring security(三)---认证过程(原理解析,demo)
在前面两节Spring security (一)架构框架-Component.Service.Filter分析和Spring Security(二)--WebSecurityConfigurer配 ...
- 【原】无脑操作:IDEA + maven + Shiro + SpringBoot + JPA + Thymeleaf实现基础授权权限
上一篇<[原]无脑操作:IDEA + maven + Shiro + SpringBoot + JPA + Thymeleaf实现基础认证权限>介绍了实现Shiro的基础认证.本篇谈谈实现 ...
- django-rest-framework 基础三 认证、权限和频率
django-rest-framework 基础三 认证.权限和频率 目录 django-rest-framework 基础三 认证.权限和频率 1. 认证 1.1 登录接口 1.2 认证 2. 权限 ...
随机推荐
- 详解Linux Initrd
在Linux操作系统中,有一项特殊的功能--初始化内存盘INITRD(INITial Ram Disk)技术,而且内核支持压缩的文件系统映像.有了这两项功能,我们可以让Linux系统从小的初始化内存盘 ...
- Parallel中分区器Partitioner的简单使用
Partitioner.Create(1,10,4).GetDynamicPartitions() 为长度为10的序列创建分区,每个分区至多4个元素,分区方法及结果:Partitioner.Creat ...
- hibernate学习(三) hibernate中的对象状态
hibernate对象的状态分为三种: 游离状态,持久化状态,瞬时状态 下面一行代码区分: Configuration cfg=new Configuration().configure(); ...
- 初识Go语言
一.Go语言的主要特性: ① 开放源代码的通用计算机编程语言.开放源代码的软件(以下简称开源软件)更容易被修正和改进. ② 虽为静态类型.编译型的语言,但go语言的语法却趋于脚本化,非常简 ...
- WPF基础篇之系统中141种颜色
WPF最大的特点就是酷炫的外观,在学习过程中经常看见各种渐变窗体.作为几乎没做过美工的程序员,我对各种颜色的argb值不熟,颜色的英文单词也只认识部分.为了不至于每次都用Colors点出颜色再随机挑选 ...
- linux系统文件系统重要知识介绍
[root@Asterplus:~]$ls -lhitotal 48K3684713 -rw------- 1 root root 5.9K Jul 1 00:23 anaconda-ks.cfg36 ...
- 【Chrome控制台】获取元素上绑定的事件信息以及监控事件
需求场景 在前端开发中,偶尔需要验证下某个元素上到底绑定了哪些事件,以及监控某个元素上的事件触发情况. 解决方案 普通操作 之前面对这种情况,一般采取的措施就是在各个事件里写console.info, ...
- python PEP8相关介绍
在学习了python相关技术之后,开始重视其开发规范,以满足代码的可读性以及可维护性.主要的是学习了PEP8-style for python code的相关内容. 代码布局 缩进:每一级4个缩进.连 ...
- handsontable 属性汇总
常规属性: 1.固定行列位置 fixedRowsTop:行数 //固定顶部多少行不能垂直滚动 fixedColumnsLeft:列数 //固定左侧多少列不能水平滚动 2.拖拽行头或列头改变行或列的大小 ...
- jqgrid 单元格引入时间datepicker控件
简述原理:引入jquery-ui插件,设置好表格所需的字段变量以及字段属性1.设置colName与colModel colNames: ['过期时间''] colModel:[{ name ...