您的位置:首页 > 运维架构 > Apache

学习Apache Shiro - 预研

2016-02-22 23:25 281 查看
官方手册导航:

http://shiro.apache.org/documentation.html

官方参考手册:

http://shiro.apache.org/reference.html

Java API Doc:

http://shiro.apache.org/static/1.2.3/apidocs/

============================================

摘自:

/article/4162259.html

认证内部处理机制
以上,是Shiro认证在应用程序中的处理过程,下面将详细解说Shiro认证的内部处理机制。


如上图,我们通过Shiro架构图的认证部分,来说明Shiro认证内部的处理顺序:
1、应用程序构建了一个终端用户认证信息的AuthenticationToken 实例后,调用Subject.login方法。
2、Sbuject的实例通常是DelegatingSubject类(或子类)的实例对象,在认证开始时,会委托应用程序设置的securityManager实例调用securityManager.login(token)方法。
3、SecurityManager接受到token(令牌)信息后会委托内置的Authenticator的实例(通常都是ModularRealmAuthenticator类的实例)调用authenticator.authenticate(token). ModularRealmAuthenticator在认证过程中会对设置的一个或多个Realm实例进行适配,它实际上为Shiro提供了一个可拔插的认证机制。
4、如果在应用程序中配置了多个Realm,ModularRealmAuthenticator会根据配置的AuthenticationStrategy(认证策略)来进行多Realm的认证过程。在Realm被调用后,AuthenticationStrategy将对每一个Realm的结果作出响应。
注:如果应用程序中仅配置了一个Realm,Realm将被直接调用而无需再配置认证策略。
5、判断每一个Realm是否支持提交的token,如果支持,Realm将调用getAuthenticationInfo(token); getAuthenticationInfo 方法就是实际认证处理,我们通过覆盖Realm的doGetAuthenticationInfo方法来编写我们自定义的认证处理。

角色授权 - 代码实现

/article/4162260.html

建议使用 权限角色 进行设计编码。

权限声明,例:

User:view,edit,123: - 查看权限,编辑id为123的用户资源的权限
这里分别代表了 资源类型:操作:资源ID

具体实现有以下三种方法:

1. java编码

2. 注解 annotation

3. JSP tag标签库

<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>

web filter配置说明

/article/4162262.html

Filter Chain定义说明

1、一个URL可以配置多个Filter,使用逗号分隔

2、当设置多个过滤器时,全部验证通过,才视为通过

3、部分过滤器可指定参数,如perms,roles

Shiro内置的FilterChain

Filter NameClass
anonorg.apache.shiro.web.filter.authc.AnonymousFilter
authcorg.apache.shiro.web.filter.authc.FormAuthenticationFilter
authcBasicorg.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter
permsorg.apache.shiro.web.filter.authz.PermissionsAuthorizationFilter
portorg.apache.shiro.web.filter.authz.PortFilter
restorg.apache.shiro.web.filter.authz.HttpMethodPermissionFilter
rolesorg.apache.shiro.web.filter.authz.RolesAuthorizationFilter
sslorg.apache.shiro.web.filter.authz.SslFilter
userorg.apache.shiro.web.filter.authc.UserFilter
== ==

IBM website上面又一篇对其架构描述得较为清楚得文章!!待细读。

http://www.ibm.com/developerworks/cn/opensource/os-cn-shiro/

授权部分很简单,即对安全实体进行 CRUD。其中 Permission 的权限字符串根据实际情况形成,在本例中:

如果对所有 message 具有修改权限,权限字符串可以为:message:delete,update:*;
如果针对某一个 message 具有修改权限,权限字符串可以为:message:update,delete:messageid。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: