一、简介
如果你正想学习权限方面的知识,或者正打算把Shiro作为权限组件集成到自己的web application中,或许正在为Shiro如何实现CAPTCHA(验证码)功能而伤透脑筋。那么,本文正是为你准备的。本文简单介绍权限方面的基础知识并以实际例子,带你进入Shiro的世界。
二、权限基础
a)
判断你(被认证者)是谁的过程。通常被认证者提供用户名和密码。
常见的认证包含如下几种
匿名认证:允许访问资源,不做任何类型的安全检查。
表单认证:访问资源之前,需要提交包含用户名和密码的表单。这是web application最常用的认证方式。这个过程一般会接合Session,只在第一次(新会话)访问资源时提交认证表单。
基本HTTP认证:基于RFC 2617的一种认证方式。
用户认证:Filter that allows access to resources if the accessor is a known user, which is defined as having a known principal. This means that any user who is authenticated or remembered via a 'remember me' feature will be allowed access from this filter.
b)
判断被认证者(你)是否能做什么操作的过程。
端口授权:必须通过指定的某个端口才能访问资源。
Permission授权:Filter that allows access if the current user has the permissions specified by the mapped value, or denies access if the user does not have all of the permissions specified.
Role授权:Filter that allows access if the current user has the roles specified by the mapped value, or denies access if the user does not have all of the roles specified.
perms
port
roles
ssl
c)
使用技术手段(如:MD5、SHA等)把待加密的数据变为密文(如:信息摘要等)过程。
d)
基于角色的访问控制(Role-Based Access Control)。
e)
data access object for an application’s security components (users,roles, permissions)
f)
最小粒度的授权,不与用户关联。
例如:导出报表、查看id号为“PO20090008”的采购单、创建FAQ。
g)
Permission的集合。
三、Shiro特点
简单
功能强大
能独立运行,不依赖其它框架或容器
包含了认证、授权、Session管理、加密
易于扩展
四、web application 集成Shiro
接下来,按照如下步骤开始我们的Shiro之旅:
a)
用户账号Account,可以简单的理解为用户。
一个账号可以拥有多个角色(Role)。
一个角色包含了多个权限(Permission)。
b)
如果你正在全用Eclipse:
File--New--Other--Web--Dynamic Web Project
在 /WEB-INFO/lib/目录下添加如下Jar包
相关Jar包,你可以在http://incubator.apache.org/shiro/download.html
c)
</filter-mapping>
d)
[main]
#SHA256加密
sha256Matcher = org.apache.shiro.authc.credential.Sha256CredentialsMatcher
#realm
myRealm = com.wearereading.example.shiro.MyShiroRealm
myRealm.credentialsMatcher = $sha256Matcher
#缓存
myRealm.authorizationCachingEnab
cache=org.apache.shiro.cache.ehcache.EhCacheManager
myRealm.cacheManager=$cache
[filters]
shiro.loginUrl = /login.jsp
#authc=org.apache.shiro.web.filter.authc.FormAuthenticationFilter
authc.successUrl =/background.jsp
perms.unauthorizedUrl =/401.jsp
[urls]
/login.jsp=authc
/logout.jsp=anon
/about.jsp=anon
/background.jsp=authc
/faq/test.jsp=authc
/faq/list.jsp=authc,perms["faq:list"]
/faq/view.jsp=authc,perms["faq:view"]
位置:
配置参数可以写在web.xml文件中,也可以单独文件形式存放在本地类根路径、文件系统以及网络环境中。
Shiro INI Inline Config 和External Config
public class MyShiroRealm extends AuthorizingRealm{
}
f)
FormAuthenticationFilter
%>
<form action="login.jsp" method="post">
</form>
g)
<%
%>
写在后面的几句话:
此文的发表于新浪博客积沙成塔,转载请标注来源 http://blog.sina.com.cn/minssh。
文章大概写于一年前,由于原文章是Word排版,里面的代码在这里看起来有点乱(Sina不支持代码排版),请原谅!