码迷,mamicode.com
首页 > Web开发 > 详细

shiro Apache 框架的学习之authentication 和authorization

时间:2019-08-20 01:10:53      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:abs   enc   static   notice   html   log   download   graph   介绍   

shiro  作为Apache的开源项目。该框架下的主要模块包含如下:

    1,Subject

    2,SecurityManager

    3,Authenticator

    4,Authorizer

    5,Realm

    6,SessionManager

    7,SessionDao

    8,CacheManager

    9,Cryptography

源码下载链接:http://shiro.apache.org/download.html  详见官网。

    如下以版本1.4学习为主。


    shiro 作为java security  framework, 其特点参考官网和百科。

    如下以例子用户登录做authentication开篇介绍。

    编写如下伪代码:

//伪代码...
class Controller{
... ModelAndView login(Customer c){ Subject s
= SecurityUtils.getSubject() UsenamePasswordToken token = new U...(c); try{s.login(token);} catche(..){..} .. } }

例子是个简单的login的Controller,使用SpringMVC的思想。

代码中会引入有关shiro框架的各种类。

很明显我们看到了Subject 这个模块了。但是其他的模块好像都没有看到。这个时候我们不得不思考Subject是一个什么类,它是如何来完成Authentication和Authorization的。

很明显我们是一头雾水的,在没看APi的情况下。


 

源码学习: 通过查看shiro 的API文档 我们至少可以发现Subject的hierarchy 结构图。它有一个委托的子类DelegatingSubject 。以及Api中介绍的如何获得这个instance的方法:SecurityUtils.getSubject() (To acquire the currently-executing Subject 获取当前执行的用户Subject,很明显避免并发产生的问题。

interface  Authencator  method: authencate(...)

SecurityUtils Api下有三个静态的方法;我们可以得到SecurityManager 模块了。
在执行Subject的login(token)方法时,通过如上链接下载的源码学习,可以看到
login(token)方法由DelegatingSubject的login(token)运行指向了
DefaultSecurityManager 的login(subject,token),在此方法中进行authenticate(token)
从此开始为了获得AuthenticationInfo
此时:
AuthenticatingSecurityManager实现了该方法,执行该方法时,Manager this.authenticator.authenticate()使用一个
了 实现了
模块Authenticator authenticate(token)的抽象类AbstractAuthenticator在执行authenticate(token)方法时候,将执行一个doAuthenticate(token)
notice:注意虽然AbstractAuthenticator 有方法:authenticate(),但是方法修饰为 final ,不会造成歧义。
doAuthenticate(token)该方法是个抽象方法,这个时候,让子类ModularRealmAuthenticator来实现了该方法。 该方法中我们会得到一个Realms模块。
最终方法通过doAuthenticate(token)--->doSingleRealmsAuthentication(realms,token)--->Realms 模块的doGetAuthenticationInfo(token)


最终需要我们配置一个类CustomerRealm 来继承AuthorizatingRealms 实现抽象方法doGetAuthenticationInfo(token) 和dogetAuthorizationInfo(token)抽象方法。

总结:在做authenticating和authorizating 时候,我们是将该验证交给了以个核心的SecurityManager来管理。而SecurityManager将该登录验证和权限管理交给对应的...SecurityManager,再交给验证器来做对应的验证和权限验证
而为了做这个验证我们将此验证交给开发者来做这个Realms的管理。


而SecurityManager 接口实现了接口:Authenticator, Authorizer, SessionManager

技术图片

 

 


其中的设计模式叫什么来着的,有点忘记了。


 

shiro Apache 框架的学习之authentication 和authorization

标签:abs   enc   static   notice   html   log   download   graph   介绍   

原文地址:https://www.cnblogs.com/futureT/p/11380600.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!