shiro讲解 之 Authorization (二)
来源:互联网 发布:centos iso镜像 下载 编辑:程序博客网 时间:2024/06/13 17:29
shiro讲解 之 Authorization (二)
本章我们将深度的学习 Shiro 的 Authorization。在之前的章节我们有学习过 Shiro 的基本 Authorization 即在 Realm 做Subject 的授权,在SpringContext中配置 Shiro Authorizartion 的 DefaultFilter。
DefaultFilter
什么是 DefaultFilter
首先我们了解一下 Shiro 的 DefaultFilter。 Shiro DefaultFilter 是在身份验证、授权等的统一入口。比如之前我们在学习 Authorization 的实例中 我们在SpringContext 配置文件中做了一下声明:
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <property name="securityManager" ref="securityManager" /> <property name="loginUrl" value="/login/toLogin" /> <property name="successUrl" value="/example/index" /> <property name="unauthorizedUrl" value="/example/unauthorized" /> <property name="filterChainDefinitions"> <value> /login/toLogin = anon /login/loginVal = anon /login/logout = logout /example/admin =roles[admin] /example/user = roles[user] /** = authc </value> </property> </bean>
在上面的实例代码中的 filterChainDefinitions 属性即是 DefaultFilter 的配置和入口。
DefaultFilter 有哪些声明:
public enum DefaultFilter { anon(AnonymousFilter.class), authc(FormAuthenticationFilter.class), authcBasic(BasicHttpAuthenticationFilter.class), logout(LogoutFilter.class), noSessionCreation(NoSessionCreationFilter.class), perms(PermissionsAuthorizationFilter.class), port(PortFilter.class), rest(HttpMethodPermissionFilter.class), roles(RolesAuthorizationFilter.class), ssl(SslFilter.class), user(UserFilter.class); }
具体说明
身份认证
授权
其他
三种授权方式
Shiro 提供了三种授权方式分别为 编程式、注解式和JSP/GSP 标签。
编程式
通过写if/else 授权代码块完成。@RequestMapping(value="/user")public String AuthorizationTwo(){ Subject user =SecurityUtils.getSubject(); System.out.println("角色" + user.hasRole("user")); if(user.hasRole("user")){ return "user"; }else{ return "unauthorized"; }}
注解式
通过在执行的Java方法上放置相应的注解完成,没有权限将抛出相应的异常。@RequiresRoles("admin") @RequestMapping(value="/admin") public String AuthorizationOne () { Subject admin =SecurityUtils.getSubject(); System.out.println("角色" + admin.hasRole("admin")); return "admin"; } @RequiresRoles("user") @RequestMapping(value="/user") public String AuthorizationTwo(){ Subject admin =SecurityUtils.getSubject(); System.out.println("角色" + admin.hasRole("user")); return "user"; }
在需要做角色权限控制的方法上(可以使Controller 层也可以是 Service层,如有需求Dao 层亦可以。一般我们在Service层角色访问权限)加上 一下 Shiro注解
@RequiresAuthentication
- 表示当前Subject已经通过login 进行了身份验证;即 Subject. isAuthenticated() 返回 true
@RequiresUser
- 表示当前 Subject 已经身份验证或者通过记住我登录的。
- @RequiresGuest
- 表示当前Subject没有身份验证或通过记住 我登录过,即是游客身份。
@RequiresRoles
单个角色逻辑授权。 @RequiresRoles(“admin”)
多个角色逻辑授权。@RequiresRoles(value={“admin”, “user”}, logical=Logical.AND):表示当前 Subject 需要角色 admin 和user
@RequiresPermissions
- (value={“user:a”, “user:b”},logical= Logical.OR):表示当前 Subject 需要权限 user:a 或
user:b。
- (value={“user:a”, “user:b”},logical= Logical.OR):表示当前 Subject 需要权限 user:a 或
JSP/GSP 标签
在JSP/GSP 页面通过相应的标签完成。<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%><%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags"%><% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path;%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Insert title here</title></head><body> <h1>Hi ~~shiro</h1> <h4>This Subject has role of User</h4> <shiro:hasRole name="admin"> Only admin can access to those words </shiro:hasRole> <br> <br> <shiro:hasRole name="user"> Only User can access to those words </shiro:hasRole> <br> <br> <shiro:hasRole name="Manager"> Only Manager can access to those words </shiro:hasRole> <br> <br> <a href="<%=basePath%>/login/logout">Logout</a></body></html>
使用标签式授权方式第一步需要在JSP页面添加 Shiro 标签(关于Shiro 标签我们在后续章节中学习。)
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>
通过标签进行角色权限处理的核心代码
<!-- 只有admin 角色才能显示一下内容 --><shiro:hasRole name="admin"> Only admin can access to those words</shiro:hasRole><br><br><!-- 只有user 角色才能显示一下内容 --><shiro:hasRole name="user"> Only User can access to those words</shiro:hasRole><br><br>
小结
Shiro 的授权方式有三种,一般我常用注解的方式来做授权,一来代码简介高效,二来灵活(可在我们想要的方法上加上注解即可)。
要理解Shiro DeaultFilter 在 App中所处的位置和作用。她应在 Web 层和 MVC 之间。用于拦截我们自定义的URL,然后根据特定的属性声明来做认证和授权以及其他的处理。
在使用注解方式授权时一定要注意一个问题:一般我们在使用Spring作为Main FrameWork 时在我们的service层会实现事务代理,即在service的一些方法中使用事务,此时我们一定要注意在service 使用的事务代理的情况下我们使用shiro 的注解授权时的注解不能加在service层而要在controller层,以为代理不能成为代理的代理也即注解授权代理不能成为事务代理的代理。
- shiro讲解 之 Authorization (二)
- shiro讲解 之 Authorization (一)
- shiro讲解 之 Authorization (三)
- shiro讲解之 多Realm 之 Authorization
- shiro讲解 之 Authentication-Authorization小结
- shiro之Authorization的概述
- shiro讲解之 Shiro Filter
- Shiro系列之Shiro+Mysql实现用户授权(Authorization)
- 【Shiro】Apache Shiro架构之权限认证(Authorization)
- 【Shiro】Apache Shiro架构之权限认证(Authorization)
- shiro讲解之 入门
- shiro讲解之 Authentication
- shiro讲解之 Cryptography
- shiro讲解之 Realm
- shiro讲解之 SpringMVC 集成Shiro
- Open Authorization协议 讲解
- shiro讲解之 多Realm 之 Authentication
- Apache Shiro Authorization学习笔记
- delphi FMX图像简单的腐蚀和膨胀
- 精通设计模式之工厂方法模式
- Hibernate框架第二天(一级缓存、Query和Criteria查询接口)
- python日常笔记3--关于分布式任务的分配
- MySql按周,按月,按日分组统计数据
- shiro讲解 之 Authorization (二)
- ng-class判断奇偶数,使其背景色不一样
- Ubuntu搭建hadoop集群(分布式)
- Android开发EditText弹出软键盘的相关使用
- 浅析JVM之内存管理
- rxjs5.X系列 —— filter系列 api 笔记
- mvvm
- 相似度计算
- ue4 修改3dui内容