Shiro学习(二、身份验证)

参考:https://www.iteye.com/blog/lgbolgger-2163890

https://www.iteye.com/blog/jinnianshilongnian-2019547

身份验证,即在应用中谁能证明他就是他本人。一般提供如他们的身份ID一些标识信息来表明他就是他本人,如提供身份证,用户名/密码来证明。

在shiro中,用户需要提供principals (身份)和credentials(证明)给shiro,从而应用能验证用户身份:

principals:身份,即主体的标识属性,可以是任何东西,如用户名、邮箱等,唯一即可。一个主体可以有多个principals,但只有一个Primary principals,一般是用户名/密码/手机号。

credentials:证明/凭证,即只有主体知道的安全值,如密码/数字证书等。

最常见的principals和credentials组合就是用户名/密码了。接下来先进行一个基本的身份认证。

另外两个相关的概念是之前提到的SubjectRealm,分别是主体及验证主体的数据源。

 

2.2  环境准备

本文使用Maven构建,因此需要一点Maven知识。首先准备环境依赖: 

Java代码  收藏代码

  1. <dependencies>  
  2.     <dependency>  
  3.         <groupId>junit</groupId>  
  4.         <artifactId>junit</artifactId>  
  5.         <version>4.9</version>  
  6.     </dependency>  
  7.     <dependency>  
  8.         <groupId>commons-logging</groupId>  
  9.         <artifactId>commons-logging</artifactId>  
  10.         <version>1.1.3</version>  
  11.     </dependency>  
  12.     <dependency>  
  13.         <groupId>org.apache.shiro</groupId>  
  14.         <artifactId>shiro-core</artifactId>  
  15.         <version>1.2.2</version>  
  16.     </dependency>  
  17. </dependencies>   

添加junit、common-logging及shiro-core依赖即可。

2.3  登录/退出

1、首先准备一些用户身份/凭据(shiro.ini)

Java代码 

 收藏代码

  1. [users]  
  2. zhang=123  
  3. wang=123  

此处使用ini配置文件,通过[users]指定了两个主体:zhang/123、wang/123。

2、测试用例(com.github.zhangkaitao.shiro.chapter2.LoginLogoutTest) 

Java代码  收藏代码

  1. @Test  
  2. public void testHelloworld() {  
  3.     //1、获取SecurityManager工厂,此处使用Ini配置文件初始化SecurityManager  
  4.     Factory<org.apache.shiro.mgt.SecurityManager> factory =  
  5.             new IniSecurityManagerFactory("classpath:shiro.ini");  
  6.     //2、得到SecurityManager实例 并绑定给SecurityUtils  
  7.     org.apache.shiro.mgt.SecurityManager securityManager = factory.getInstance();  
  8.     SecurityUtils.setSecurityManager(securityManager);  
  9.     //3、得到Subject及创建用户名/密码身份验证Token(即用户身份/凭证)  
  10.     Subject subject = SecurityUtils.getSubject();  
  11.     UsernamePasswordToken token = new UsernamePasswordToken("zhang""123");  
  12.   
  13.     try {  
  14.         //4、登录,即身份验证  
  15.         subject.login(token);  
  16.     } catch (AuthenticationException e) {  
  17.         //5、身份验证失败  
  18.     }  
  19.   
  20.     Assert.assertEquals(true, subject.isAuthenticated()); //断言用户已经登录  
  21.   
  22.     //6、退出  
  23.     subject.logout();  
  24. }  
  25.    

1、首先通过new IniSecurityManagerFactory并指定一个ini配置文件来创建一个SecurityManager工厂;

2、接着获取SecurityManager并绑定到SecurityUtils,这是一个全局设置,设置一次即可;

3、通过SecurityUtils得到Subject,其会自动绑定到当前线程;如果在web环境在请求结束时需要解除绑定;然后获取身份验证的Token,如用户名/密码;

4、调用subject.login方法进行登录,其会自动委托给SecurityManager.login方法进行登录;

5、如果身份验证失败请捕获AuthenticationException或其子类,常见的如: DisabledAccountException(禁用的帐号)、LockedAccountException(锁定的帐号)、UnknownAccountException(错误的帐号)、ExcessiveAttemptsException(登录失败次数过多)、IncorrectCredentialsException (错误的凭证)、ExpiredCredentialsException(过期的凭证)等,具体请查看其继承关系;对于页面的错误消息展示,最好使用如“用户名/密码错误”而不是“用户名错误”/“密码错误”,防止一些恶意用户非法扫描帐号库;

6、最后可以调用subject.logout退出,其会自动委托给SecurityManager.logout方法退出。

 

总结出身份验证的步骤:

1、收集用户身份/凭证,即如用户名/密码;

2、调用Subject.login进行登录,如果失败将得到相应的AuthenticationException异常,根据异常提示用户错误信息;否则登录成功;

3、最后调用Subject.logout进行退出操作。

a、使用工厂模式来得到SecurityManager,由于可以通过不同工厂创建出不同的SecurityManager,如通过配置文件的形式来创建的IniSecurityManagerFactory工厂。类图如下: 

Factory接口:通过泛型定义了一个T getInstance()方法 
AbstractFactory抽象类:对于getInstance返回的对象加入单例或者非单例的功能,而把真正创建实例对象的createInstance功能留给子类去实现 

Java代码 

 收藏代码

  1. public T getInstance() {  
  2.         T instance;  
  3.         if (isSingleton()) {  
  4.             if (this.singletonInstance == null) {  
  5.                 this.singletonInstance = createInstance();  
  6.             }  
  7.             instance = this.singletonInstance;  
  8.         } else {  
  9.             instance = createInstance();  
  10.         }  
  11.         if (instance == null) {  
  12.             String msg = "Factory 'createInstance' implementation returned a null object.";  
  13.             throw new IllegalStateException(msg);  
  14.         }  
  15.         return instance;  
  16.     }  
  17.   
  18.     protected abstract T createInstance();  

IniFactorySupport:加入了Ini ini属性,同过该对象来创建出一个实例,IniFactorySupport对于ini的获取给出了两种方式,方式一:在构造IniFactorySupport时传入Ini 对象,另一种就是加载类路径下默认的Ini,如下: 

Java代码 

 收藏代码

  1. public static Ini loadDefaultClassPathIni() {  
  2.         Ini ini = null;  
  3.         if (ResourceUtils.resourceExists(DEFAULT_INI_RESOURCE_PATH)) {  
  4.             log.debug("Found shiro.ini at the root of the classpath.");  
  5.             ini = new Ini();  
  6.             ini.loadFromPath(DEFAULT_INI_RESOURCE_PATH);  
  7.             if (CollectionUtils.isEmpty(ini)) {  
  8.                 log.warn("shiro.ini found at the root of the classpath, but it did not contain any data.");  
  9.             }  
  10.         }  
  11.         return ini;  
  12.     }  

其中DEFAULT_INI_RESOURCE_PATH为classpath:shiro.ini。然而IniFactorySupport并不负责通过ini配置文件来创建出什么样的对象,它仅仅负责获取ini配置文件,所以它要留出了两个方法让子类实现: 

  1. protected abstract T createInstance(Ini ini); 
  2. protected abstract T createDefaultInstance();  

第一个方法就是通过ini配置文件创建出什么对象,第二个方法就是当获取不到ini配置文件时,要创建默认的对象。 
IniSecurityManagerFactory:通过Ini配置文件可以创建出SecurityManager对象,也可以通过ini配置文件创建FilterChainResolver对象,而IniSecurityManagerFactory则是通过ini配置文件来创建SecurityManager的,所以对于泛型的实例化是在该类完成的,如下: 

Java代码 

 收藏代码

  1. public class IniSecurityManagerFactory extends IniFactorySupport<SecurityManager>  
  2. public class IniFilterChainResolverFactory extends IniFactorySupport<FilterChainResolver>  

IniSecurityManagerFactory 还不具有web功能,WebIniSecurityManagerFactory则加入了web功能。 
可以看到,有很多的类继承关系,每一个类都完成了一个基本功能,把职责划分的更加明确,而不是一锅粥把很多功能放到一个类中,导致很难去复用某些功能。 

b、将创建的SecurityManager放到SecurityUtils类的静态变量中,供所有对象来访问。

c、创建一个Subject实例,接口Subject的文档介绍如下: A {@code Subject} represents state and security operations for a <em>single</em> application user.These operations include authentication (login/logout), authorization (access control), and session access  

即外界通过Subject接口来和SecurityManager进行交互,该接口含有登录、退出、权限判断、获取Session,其中的Session可不是平常我们所使用的HttpSession等,而是shiro自定义的,是一个数据上下文,与一个Subject相关联的。

先回到创建Subject的地方:

  1. public static Subject getSubject() {  
  2.         Subject subject = ThreadContext.getSubject();  
  3.         if (subject == null) {  
  4.             subject = (new Subject.Builder()).buildSubject();  
  5.             ThreadContext.bind(subject);  
  6.         }  
  7.         return subject;  
  8.     }  

一看就是使用的是ThreadLocal设计模式,获取当前线程相关联的Subject对象,如果没有则创建一个,然后绑定到当前线程。然后我们来看下具体实现:

ThreadContext是org.apache.shiro.util包下的一个工具类,它是用来操作和当前线程绑定的SecurityManager和Subject,它必然包含了一个ThreadLocal对象如下: 

Java代码 

 收藏代码

  1. public abstract class ThreadContext {  
  2.   
  3.     public static final String SECURITY_MANAGER_KEY = ThreadContext.class.getName() + "_SECURITY_MANAGER_KEY";  
  4.     public static final String SUBJECT_KEY = ThreadContext.class.getName() + "_SUBJECT_KEY";  
  5.   
  6.     private static final ThreadLocal<Map<Object, Object>> resources = new InheritableThreadLocalMap<Map<Object, Object>>();  
  7.   
  8.  //略  
  9.   
  10. }  

ThreadLocal中存放的数据是一个Map集合,集合中所存的key有两个SECURITY_MANAGER_KEY和SUBJECT_KEY,就是通过这两个key来存取SecurityManager和Subject两个对象。

当前线程还没有绑定一个Subject时,就需要通过Sucject.Builder来创建一个然后绑定到当前线程。Builder是Subject的一个内部类,它拥有两个重要的属性,SubjectContext和SecurityManger,创建Builder时使用SecurityUtils工具来获取它的全局静态变量SecurityManager,SubjectContext则是使用newSubjectContextInstance创建一个DefaultSubjectContext对象:

  1. public Builder() {  
  2.             this(SecurityUtils.getSecurityManager());  
  3.         }  
  4.   
  5.         public Builder(SecurityManager securityManager) {  
  6.             if (securityManager == null) {  
  7.                 throw new NullPointerException("SecurityManager method argument cannot be null.");  
  8.             }  
  9.             this.securityManager = securityManager;  
  10.             this.subjectContext = newSubjectContextInstance();  
  11.             if (this.subjectContext == null) {  
  12.                 throw new IllegalStateException("Subject instance returned from 'newSubjectContextInstance' " +  
  13.                         "cannot be null.");  
  14.             }  
  15.             this.subjectContext.setSecurityManager(securityManager);  
  16.         }  
  17.   
  18. protected SubjectContext newSubjectContextInstance() {  
  19.             return new DefaultSubjectContext();  
  20.         }  

Builder准备工作完成后,调用buildSubject来创建一个Subject: 

Java代码 

 收藏代码

  1. public Subject buildSubject() {  
  2.             return this.securityManager.createSubject(this.subjectContext);  
  3.         }  

最终还是通过securityManager根据subjectContext来创建一个Subject。最终是通过一个SubjectFactory来创建的,SubjectFactory是一个接口,接口方法为SubjectcreateSubject(SubjectContext context),默认的SubjectFactory实现是DefaultSubjectFactory,DefaultSubjectFactory创建的Subject是DelegatingSubject。至此创建Subject就简单说完了。

d、登录部分

登录方法为void login(AuthenticationToken token),AuthenticationToken接口如下:

  1. public interface AuthenticationToken extends Serializable {  
  2.   
  3.     Object getPrincipal();  
  4.   
  5.     Object getCredentials();  
  6.   
  7. }  

Principal就相当于用户名,Credentials就相当于密码,AuthenticationToken 的实现UsernamePasswordToken有四个重要属性,即username、char[] password、boolean rememberMe、host。认证过程是由Authenticator来完成的,先来看下Authenticator的整体: 

  1. public interface Authenticator {  
  2.     public AuthenticationInfo authenticate(AuthenticationToken authenticationToken)  
  3.             throws AuthenticationException;  
  4. }  

很简单,就是根据AuthenticationToken 返回一个AuthenticationInfo ,如果认证失败会抛出AuthenticationException异常。 
AbstractAuthenticator实现了Authenticator 接口,它仅仅加入了对认证成功与失败的监听功能,即有一个Collection<AuthenticationListener>集合: 

private Collection<AuthenticationListener>  listeners;

对于认证过程:

  1. public final AuthenticationInfo authenticate(AuthenticationToken token) throws AuthenticationException {  
  2.   
  3.         if (token == null) {  
  4.             throw new IllegalArgumentException("Method argumet (authentication token) cannot be null.");  
  5.         }  
  6.   
  7.         log.trace("Authentication attempt received for token [{}]", token);  
  8.   
  9.         AuthenticationInfo info;  
  10.         try {  
  11.             info = doAuthenticate(token);  
  12.             if (info == null) {  
  13.                 String msg = "No account information found for authentication token [" + token + "] by this " +  
  14.                         "Authenticator instance.  Please check that it is configured correctly.";  
  15.                 throw new AuthenticationException(msg);  
  16.             }  
  17.         } catch (Throwable t) {  
  18.             AuthenticationException ae = null;  
  19.             if (t instanceof AuthenticationException) {  
  20.                 ae = (AuthenticationException) t;  
  21.             }  
  22.             if (ae == null) {  
  23.                 //Exception thrown was not an expected AuthenticationException.  Therefore it is probably a little more  
  24.                 //severe or unexpected.  So, wrap in an AuthenticationException, log to warn, and propagate:  
  25.                 String msg = "Authentication failed for token submission [" + token + "].  Possible unexpected " +  
  26.                         "error? (Typical or expected login exceptions should extend from AuthenticationException).";  
  27.                 ae = new AuthenticationException(msg, t);  
  28.             }  
  29.             try {  
  30.                 notifyFailure(token, ae);  
  31.             } catch (Throwable t2) {  
  32.                 if (log.isWarnEnabled()) {  
  33.                     String msg = "Unable to send notification for failed authentication attempt - listener error?.  " +  
  34.                             "Please check your AuthenticationListener implementation(s).  Logging sending exception " +  
  35.                             "and propagating original AuthenticationException instead...";  
  36.                     log.warn(msg, t2);  
  37.                 }  
  38.             }  
  39.   
  40.   
  41.             throw ae;  
  42.         }  
  43.   
  44.         log.debug("Authentication successful for token [{}].  Returned account [{}]", token, info);  
  45.   
  46.         notifySuccess(token, info);  
  47.   
  48.         return info;  
  49.     }  
  50.   
  51. protected abstract AuthenticationInfo doAuthenticate(AuthenticationToken token)  
  52.             throws AuthenticationException; 

从上面可以看到实际的认证过程doAuthenticate是交给子类来实现的,AbstractAuthenticator只对认证结果进行处理,认证成功时调用notifySuccess(token, info)通知所有的listener,认证失败时调用notifyFailure(token, ae)通知所有的listener。 

具体的认证过程就需要看AbstractAuthenticator子类对于doAuthenticate方法的实现,ModularRealmAuthenticator继承了AbstractAuthenticator,它有两个重要的属性如下 

  1. private Collection<Realm> realms;  
  2. private AuthenticationStrategy authenticationStrategy;  

首先就是Realm的概念:就是配置各种角色、权限和用户的地方,即提供了数据源供shiro来使用,它能够根据一个AuthenticationToken中的用户名和密码来判定是否合法等,文档如下: 

Java代码 

A <tt>Realm</tt> is a security component that can access application-specific security entities such as users, roles, and permissions to determine authentication and authorization operations 

接口如下: 

Java代码 

 收藏代码

  1. public interface Realm {  
  2.   
  3.     String getName();  
  4.   
  5.     boolean supports(AuthenticationToken token);  
  6.   
  7.     AuthenticationInfo getAuthenticationInfo(AuthenticationToken token) throws AuthenticationException;  
  8.   
  9. }  

Realm 首先有一个重要的name属性,全局唯一的标示。supports、getAuthenticationInfo方法就是框架中非常常见的一种写法,ModularRealmAuthenticator拥有Collection<Realm> realms集合,在判定用户合法性时,会首先调用每个Realm的supports方法,如果支持才会去掉用相应的getAuthenticationInfo方法。 
关于Realm的详细接口设计之后再给出详细说明,此时先继续回到ModularRealmAuthenticator认证的地方 

  1. protected AuthenticationInfo doAuthenticate(AuthenticationToken authenticationToken) throws AuthenticationException {  
  2.         assertRealmsConfigured();  
  3.         Collection<Realm> realms = getRealms();  
  4.         if (realms.size() == 1) {  
  5.             return doSingleRealmAuthentication(realms.iterator().next(), authenticationToken);  
  6.         } else {  
  7.             return doMultiRealmAuthentication(realms, authenticationToken);  
  8.         }  
  9.     }  

代码很简单,当只有一个Realm时先调用Realm的supports方法看是否支持,若不支持则抛出认证失败的异常,若支持则调用Realm的getAuthenticationInfo(token)方法如下: 

Java代码 

 收藏代码

  1. protected AuthenticationInfo doSingleRealmAuthentication(Realm realm, AuthenticationToken token) {  
  2.         if (!realm.supports(token)) {  
  3.             String msg = "Realm [" + realm + "] does not support authentication token [" +  
  4.                     token + "].  Please ensure that the appropriate Realm implementation is " +  
  5.                     "configured correctly or that the realm accepts AuthenticationTokens of this type.";  
  6.             throw new UnsupportedTokenException(msg);  
  7.         }  
  8.         AuthenticationInfo info = realm.getAuthenticationInfo(token);  
  9.         if (info == null) {  
  10.             String msg = "Realm [" + realm + "] was unable to find account data for the " +  
  11.                     "submitted AuthenticationToken [" + token + "].";  
  12.             throw new UnknownAccountException(msg);  
  13.         }  
  14.         return info;  
  15.     }  

若有多个Realm 时怎样才算是认证成功的呢?这就需要ModularRealmAuthenticator的认证策略AuthenticationStrategy 来指定,对于AuthenticationStrategy目前有三种实现 
AllSuccessfulStrategy:即所有的Realm 都验证通过才算是通过 
AtLeastOneSuccessfulStrategy:只要有一个Realm 验证通过就算通过 
FirstSuccessfulStrategy:这个刚开始不太好理解,和AtLeastOneSuccessfulStrategy稍微有些区别。AtLeastOneSuccessfulStrategy返回了所有Realm认证成功的信息,FirstSuccessfulStrategy只返回了第一个Realm认证成功的信息。 
试想一下,如果让你来设计,你会怎么设计? 
然后来具体看下AuthenticationStrategy 的接口设计: 

  1. public interface AuthenticationStrategy {  
  2.   
  3.     AuthenticationInfo beforeAllAttempts(Collection<? extends Realm> realms, AuthenticationToken token) throws AuthenticationException;  
  4.   
  5.     AuthenticationInfo beforeAttempt(Realm realm, AuthenticationToken token, AuthenticationInfo aggregate) throws AuthenticationException;  
  6.   
  7.     AuthenticationInfo afterAttempt(Realm realm, AuthenticationToken token, AuthenticationInfo singleRealmInfo, AuthenticationInfo aggregateInfo, Throwable t)  
  8.             throws AuthenticationException;  
  9.   
  10.     AuthenticationInfo afterAllAttempts(AuthenticationToken token, AuthenticationInfo aggregate) throws AuthenticationException;  
  11. }  

验证过程是这样的,每一个Realm验证token后都会返回一个当前Realm的验证信息AuthenticationInfo singleRealmInfo,然后呢会有一个贯穿所有Realm验证过程的验证信息AuthenticationInfo aggregateInfo,每一个Realm验证过后会进行singleRealmInfo和aggregateInfo的合并,这是大体的流程

 对于AllSuccessfulStrategy来说:它要确保每一个Realm都要验证成功,所以必然 
(1)要在beforeAttempt中判断当前realm是否支持token,如不支持抛出异常结束验证过程 
(2)要在afterAttempt(Realm realm, AuthenticationToken token, AuthenticationInfo singleRealmInfo, AuthenticationInfo aggregateInfo, Throwable t)中判断是否验证通过了,即异常t为空,并且singleRealmInfo不为空,则表示验证通过了,然后将singleRealmInfo和aggregateInfo合并,所以最终返回的aggregateInfo是几个Realm认证信息合并后的结果 
AllSuccessfulStrategy就会在这两处进行把关,一旦不符合抛出异常,认证失败,如下: 

  1. public AuthenticationInfo beforeAttempt(Realm realm, AuthenticationToken token, AuthenticationInfo info) throws AuthenticationException {  
  2.         if (!realm.supports(token)) {  
  3.             String msg = "Realm [" + realm + "] of type [" + realm.getClass().getName() + "] does not support " +  
  4.                     " the submitted AuthenticationToken [" + token + "].  The [" + getClass().getName() +  
  5.                     "] implementation requires all configured realm(s) to support and be able to process the submitted " +  
  6.                     "AuthenticationToken.";  
  7.             throw new UnsupportedTokenException(msg);  
  8.         }  
  9.   
  10.         return info;  
  11.     }  
  12.   
  13. public AuthenticationInfo afterAttempt(Realm realm, AuthenticationToken token, AuthenticationInfo info, AuthenticationInfo aggregate, Throwable t)  
  14.             throws AuthenticationException {  
  15.         if (t != null) {  
  16.             if (t instanceof AuthenticationException) {  
  17.                 //propagate:  
  18.                 throw ((AuthenticationException) t);  
  19.             } else {  
  20.                 String msg = "Unable to acquire account data from realm [" + realm + "].  The [" +  
  21.                         getClass().getName() + " implementation requires all configured realm(s) to operate successfully " +  
  22.                         "for a successful authentication.";  
  23.                 throw new AuthenticationException(msg, t);  
  24.             }  
  25.         }  
  26.         if (info == null) {  
  27.             String msg = "Realm [" + realm + "] could not find any associated account data for the submitted " +  
  28.                     "AuthenticationToken [" + token + "].  The [" + getClass().getName() + "] implementation requires " +  
  29.                     "all configured realm(s) to acquire valid account data for a submitted token during the " +  
  30.                     "log-in process.";  
  31.             throw new UnknownAccountException(msg);  
  32.         }  
  33.   
  34.         log.debug("Account successfully authenticated using realm [{}]", realm);  
  35.   
  36.         // If non-null account is returned, then the realm was able to authenticate the  
  37.         // user - so merge the account with any accumulated before:  
  38.         merge(info, aggregate);  
  39.   
  40.         return aggregate;  
  41.     }  

对于AtLeastOneSuccessfulStrategy来说:它只需确保在所有Realm验证完成之后,判断下aggregateInfo是否含有用户信息即可,若有则表示有些Realm是验证通过了,此时aggregateInfo也是合并后的信息,如下 

Java代码 

 收藏代码

  1. public AuthenticationInfo afterAllAttempts(AuthenticationToken token, AuthenticationInfo aggregate) throws AuthenticationException {  
  2.         //we know if one or more were able to succesfully authenticate if the aggregated account object does not  
  3.         //contain null or empty data:  
  4.         if (aggregate == null || CollectionUtils.isEmpty(aggregate.getPrincipals())) {  
  5.             throw new AuthenticationException("Authentication token of type [" + token.getClass() + "] " +  
  6.                     "could not be authenticated by any configured realms.  Please ensure that at least one realm can " +  
  7.                     "authenticate these tokens.");  
  8.         }  
  9.   
  10.         return aggregate;  
  11.     }  

对于FirstSuccessfulStrategy来说:它只需要第一个Realm验证成功的信息,不需要去进行合并,所以它必须在合并上做手脚,即不会进行合并,一旦有一个Realm验证成功,信息保存到
aggregateInfo中,之后即使再次验证成功也不会进行合并,如下 

Java代码 

 收藏代码

  1. protected AuthenticationInfo merge(AuthenticationInfo info, AuthenticationInfo aggregate) {  
  2.         if (aggregate != null && !CollectionUtils.isEmpty(aggregate.getPrincipals())) {  
  3.             return aggregate;  
  4.         }  
  5.         return info != null ? info : aggregate;  
  6.     }  

验证策略分析完成之后,我们来看下ModularRealmAuthenticator的真个验证的代码过程: 

Java代码 

 收藏代码

  1. protected AuthenticationInfo doMultiRealmAuthentication(Collection<Realm> realms, AuthenticationToken token) {  
  2.   
  3.         AuthenticationStrategy strategy = getAuthenticationStrategy();  
  4.   
  5.         AuthenticationInfo aggregate = strategy.beforeAllAttempts(realms, token);  
  6.   
  7.         if (log.isTraceEnabled()) {  
  8.             log.trace("Iterating through {} realms for PAM authentication", realms.size());  
  9.         }  
  10.   
  11.         for (Realm realm : realms) {  
  12.   
  13.             aggregate = strategy.beforeAttempt(realm, token, aggregate);  
  14.   
  15.             if (realm.supports(token)) {  
  16.   
  17.                 log.trace("Attempting to authenticate token [{}] using realm [{}]", token, realm);  
  18.   
  19.                 AuthenticationInfo info = null;  
  20.                 Throwable t = null;  
  21.                 try {  
  22.                     info = realm.getAuthenticationInfo(token);  
  23.                 } catch (Throwable throwable) {  
  24.                     t = throwable;  
  25.                     if (log.isDebugEnabled()) {  
  26.                         String msg = "Realm [" + realm + "] threw an exception during a multi-realm authentication attempt:";  
  27.                         log.debug(msg, t);  
  28.                     }  
  29.                 }  
  30.   
  31.                 aggregate = strategy.afterAttempt(realm, token, info, aggregate, t);  
  32.   
  33.             } else {  
  34.                 log.debug("Realm [{}] does not support token {}.  Skipping realm.", realm, token);  
  35.             }  
  36.         }  
  37.   
  38.         aggregate = strategy.afterAllAttempts(token, aggregate);  
  39.   
  40.         return aggregate;  
  41.     }  


有了之前的分析,这个过程便变的相当容易了。 
再回到我们的入门案例中,有了AuthenticationInfo 验证信息,之后进行了那些操作呢? 
回到DefaultSecurityManager的如下login方法中: 

Java代码 

 收藏代码

  1. public Subject login(Subject subject, AuthenticationToken token) throws AuthenticationException {  
  2.         AuthenticationInfo info;  
  3.         try {  
  4.             info = authenticate(token);  
  5.         } catch (AuthenticationException ae) {  
  6.             try {  
  7.                 onFailedLogin(token, ae, subject);  
  8.             } catch (Exception e) {  
  9.                 if (log.isInfoEnabled()) {  
  10.                     log.info("onFailedLogin method threw an " +  
  11.                             "exception.  Logging and propagating original AuthenticationException.", e);  
  12.                 }  
  13.             }  
  14.             throw ae; //propagate  
  15.         }  
  16.   
  17.         Subject loggedIn = createSubject(token, info, subject);  
  18.   
  19.         onSuccessfulLogin(token, info, loggedIn);  
  20.   
  21.         return loggedIn;  
  22.     }  


Subject loggedIn = createSubject(token, info, subject)会根据已有的token、认证结果信息info、和subject从新创建一个已登录的Subject,含有Session信息,创建过程如下: 

Java代码 

 收藏代码

  1. protected Subject createSubject(AuthenticationToken token, AuthenticationInfo info, Subject existing) {  
  2.         SubjectContext context = createSubjectContext();  
  3.         context.setAuthenticated(true);  
  4.         context.setAuthenticationToken(token);  
  5.         context.setAuthenticationInfo(info);  
  6.         if (existing != null) {  
  7.             context.setSubject(existing);  
  8.         }  
  9.         return createSubject(context);  
  10.     }  


就是填充SubjectContext,然后根据SubjectContext来创建Subject,此Subject的信息是经过SubjectDAO保存的,再回到登陆方法: 

Java代码 

 收藏代码

  1. public void login(AuthenticationToken token) throws AuthenticationException {  
  2.         clearRunAsIdentitiesInternal();  
  3.         Subject subject = securityManager.login(this, token);  
  4.   
  5.         PrincipalCollection principals;  
  6.   
  7.         String host = null;  
  8.   
  9.         if (subject instanceof DelegatingSubject) {  
  10.             DelegatingSubject delegating = (DelegatingSubject) subject;  
  11.             //we have to do this in case there are assumed identities - we don't want to lose the 'real' principals:  
  12.             principals = delegating.principals;  
  13.             host = delegating.host;  
  14.         } else {  
  15.             principals = subject.getPrincipals();  
  16.         }  
  17.   
  18.         if (principals == null || principals.isEmpty()) {  
  19.             String msg = "Principals returned from securityManager.login( token ) returned a null or " +  
  20.                     "empty value.  This value must be non null and populated with one or more elements.";  
  21.             throw new IllegalStateException(msg);  
  22.         }  
  23.         this.principals = principals;  
  24.         this.authenticated = true;  
  25.         if (token instanceof HostAuthenticationToken) {  
  26.             host = ((HostAuthenticationToken) token).getHost();  
  27.         }  
  28.         if (host != null) {  
  29.             this.host = host;  
  30.         }  
  31.         Session session = subject.getSession(false);  
  32.         if (session != null) {  
  33.             this.session = decorate(session);  
  34.         } else {  
  35.             this.session = null;  
  36.         }  
  37.     }  


最后的这些操作就是将刚才创建出来的Subject信息复制到我们所使用的Subject上,即 

Java代码 

 收藏代码

  1. subject.login(token)  


中的subject中。至此已经太长了,先告一段落,如SubjectDAO和Session的细节后面再详细说明。 

 

如上测试的几个问题:

1、用户名/密码硬编码在ini配置文件,以后需要改成如数据库存储,且密码需要加密存储;

2、用户身份Token可能不仅仅是用户名/密码,也可能还有其他的,如登录时允许用户名/邮箱/手机号同时登录。 

 

2.4  身份认证流程

流程如下:

1、首先调用Subject.login(token)进行登录,其会自动委托给Security Manager,调用之前必须通过SecurityUtils. setSecurityManager()设置;

2、SecurityManager负责真正的身份验证逻辑;它会委托给Authenticator进行身份验证;

3、Authenticator才是真正的身份验证者,Shiro API中核心的身份认证入口点,此处可以自定义插入自己的实现;

4、Authenticator可能会委托给相应的AuthenticationStrategy进行多Realm身份验证,默认ModularRealmAuthenticator会调用AuthenticationStrategy进行多Realm身份验证;

5、Authenticator会把相应的token传入Realm,从Realm获取身份验证信息,如果没有返回/抛出异常表示身份验证失败了。此处可以配置多个Realm,将按照相应的顺序及策略进行访问。

 

2.5  Realm

Realm:域,Shiro从从Realm获取安全数据(如用户、角色、权限),就是说SecurityManager要验证用户身份,那么它需要从Realm获取相应的用户进行比较以确定用户身份是否合法;也需要从Realm得到用户相应的角色/权限进行验证用户是否能进行操作;可以把Realm看成DataSource,即安全数据源。如我们之前的ini配置方式将使用org.apache.shiro.realm.text.IniRealm。

 

org.apache.shiro.realm.Realm接口如下: 

Java代码  收藏代码

  1. String getName(); //返回一个唯一的Realm名字  
  2. boolean supports(AuthenticationToken token); //判断此Realm是否支持此Token  
  3. AuthenticationInfo getAuthenticationInfo(AuthenticationToken token)  
  4.  throws AuthenticationException;  //根据Token获取认证信息  

 

Realm配置

1、自定义Realm实现(com.github.zhangkaitao.shiro.chapter2.realm.MyRealm1):  

Java代码  收藏代码

  1. public class MyRealm1 implements Realm {  
  2.     @Override  
  3.     public String getName() {  
  4.         return "myrealm1";  
  5.     }  
  6.     @Override  
  7.     public boolean supports(AuthenticationToken token) {  
  8.         //仅支持UsernamePasswordToken类型的Token  
  9.         return token instanceof UsernamePasswordToken;   
  10.     }  
  11.     @Override  
  12.     public AuthenticationInfo getAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {  
  13.         String username = (String)token.getPrincipal();  //得到用户名  
  14.         String password = new String((char[])token.getCredentials()); //得到密码  
  15.         if(!"zhang".equals(username)) {  
  16.             throw new UnknownAccountException(); //如果用户名错误  
  17.         }  
  18.         if(!"123".equals(password)) {  
  19.             throw new IncorrectCredentialsException(); //如果密码错误  
  20.         }  
  21.         //如果身份认证验证成功,返回一个AuthenticationInfo实现;  
  22.         return new SimpleAuthenticationInfo(username, password, getName());  
  23.     }  
  24. }   

 

2、ini配置文件指定自定义Realm实现(shiro-realm.ini)  

Java代码  收藏代码

  1. #声明一个realm  
  2. myRealm1=com.github.zhangkaitao.shiro.chapter2.realm.MyRealm1  
  3. #指定securityManager的realms实现  
  4. securityManager.realms=$myRealm1   

通过$name来引入之前的realm定义

 

3、测试用例请参考com.github.zhangkaitao.shiro.chapter2.LoginLogoutTest的testCustomRealm测试方法,只需要把之前的shiro.ini配置文件改成shiro-realm.ini即可。

 

Realm配置

1、ini配置文件(shiro-multi-realm.ini)  

Java代码  收藏代码

  1. #声明一个realm  
  2. myRealm1=com.github.zhangkaitao.shiro.chapter2.realm.MyRealm1  
  3. myRealm2=com.github.zhangkaitao.shiro.chapter2.realm.MyRealm2  
  4. #指定securityManager的realms实现  
  5. securityManager.realms=$myRealm1,$myRealm2   

securityManager会按照realms指定的顺序进行身份认证。此处我们使用显示指定顺序的方式指定了Realm的顺序,如果删除“securityManager.realms=$myRealm1,$myRealm2”,那么securityManager会按照realm声明的顺序进行使用(即无需设置realms属性,其会自动发现),当我们显示指定realm后,其他没有指定realm将被忽略,如“securityManager.realms=$myRealm1”,那么myRealm2不会被自动设置进去。

 

2、测试用例请参考com.github.zhangkaitao.shiro.chapter2.LoginLogoutTest的testCustomMultiRealm测试方法。

 

Shiro默认提供的Realm

以后一般继承AuthorizingRealm(授权)即可;其继承了AuthenticatingRealm(即身份验证),而且也间接继承了CachingRealm(带有缓存实现)。其中主要默认实现如下:

org.apache.shiro.realm.text.IniRealm[users]部分指定用户名/密码及其角色;[roles]部分指定角色即权限信息;

org.apache.shiro.realm.text.PropertiesRealm user.username=password,role1,role2指定用户名/密码及其角色;role.role1=permission1,permission2指定角色及权限信息;

org.apache.shiro.realm.jdbc.JdbcRealm通过sql查询相应的信息,如“select password from users where username = ?”获取用户密码,“select password, password_salt from users where username = ?”获取用户密码及盐;“select role_name from user_roles where username = ?”获取用户角色;“select permission from roles_permissions where role_name = ?”获取角色对应的权限信息;也可以调用相应的api进行自定义sql;

 

JDBC Realm使用

1、数据库及依赖

Java代码  收藏代码

  1. <dependency>  
  2.     <groupId>mysql</groupId>  
  3.     <artifactId>mysql-connector-java</artifactId>  
  4.     <version>5.1.25</version>  
  5. </dependency>  
  6. <dependency>  
  7.     <groupId>com.alibaba</groupId>  
  8.     <artifactId>druid</artifactId>  
  9.     <version>0.2.23</version>  
  10. </dependency>   

本文将使用mysql数据库及druid连接池; 

 

2、到数据库shiro下建三张表:users(用户名/密码)、user_roles(用户/角色)、roles_permissions(角色/权限),具体请参照shiro-example-chapter2/sql/shiro.sql;并添加一个用户记录,用户名/密码为zhang/123;

 

3、ini配置(shiro-jdbc-realm.ini) 

Java代码  收藏代码

  1. jdbcRealm=org.apache.shiro.realm.jdbc.JdbcRealm  
  2. dataSource=com.alibaba.druid.pool.DruidDataSource  
  3. dataSource.driverClassName=com.mysql.jdbc.Driver  
  4. dataSource.url=jdbc:mysql://localhost:3306/shiro  
  5. dataSource.username=root  
  6. #dataSource.password=  
  7. jdbcRealm.dataSource=$dataSource  
  8. securityManager.realms=$jdbcRealm   

1、变量名=全限定类名会自动创建一个类实例

2、变量名.属性=值 自动调用相应的setter方法进行赋值

3、$变量名 引用之前的一个对象实例 

4、测试代码请参照com.github.zhangkaitao.shiro.chapter2.LoginLogoutTest的testJDBCRealm方法,和之前的没什么区别。

2.6  Authenticator及AuthenticationStrategy

Authenticator的职责是验证用户帐号,是Shiro API中身份验证核心的入口点: 

Java代码  收藏代码

  1. public AuthenticationInfo authenticate(AuthenticationToken authenticationToken)  
  2.             throws AuthenticationException;   

如果验证成功,将返回AuthenticationInfo验证信息;此信息中包含了身份及凭证;如果验证失败将抛出相应的AuthenticationException实现。

 

SecurityManager接口继承了Authenticator,另外还有一个ModularRealmAuthenticator实现,其委托给多个Realm进行验证,验证规则通过AuthenticationStrategy接口指定,默认提供的实现:

FirstSuccessfulStrategy:只要有一个Realm验证成功即可,只返回第一个Realm身份验证成功的认证信息,其他的忽略;

AtLeastOneSuccessfulStrategy:只要有一个Realm验证成功即可,和FirstSuccessfulStrategy不同,返回所有Realm身份验证成功的认证信息;

AllSuccessfulStrategy:所有Realm验证成功才算成功,且返回所有Realm身份验证成功的认证信息,如果有一个失败就失败了。

 

ModularRealmAuthenticator默认使用AtLeastOneSuccessfulStrategy策略。

 

假设我们有三个realm:

myRealm1: 用户名/密码为zhang/123时成功,且返回身份/凭据为zhang/123;

myRealm2: 用户名/密码为wang/123时成功,且返回身份/凭据为wang/123;

myRealm3: 用户名/密码为zhang/123时成功,且返回身份/凭据为zhang@163.com/123,和myRealm1不同的是返回时的身份变了;

 

1、ini配置文件(shiro-authenticator-all-success.ini) 

Java代码  收藏代码

  1. #指定securityManager的authenticator实现  
  2. authenticator=org.apache.shiro.authc.pam.ModularRealmAuthenticator  
  3. securityManager.authenticator=$authenticator  
  4.   
  5. #指定securityManager.authenticator的authenticationStrategy  
  6. allSuccessfulStrategy=org.apache.shiro.authc.pam.AllSuccessfulStrategy  
  7. securityManager.authenticator.authenticationStrategy=$allSuccessfulStrategy  

Java代码  收藏代码

  1. myRealm1=com.github.zhangkaitao.shiro.chapter2.realm.MyRealm1  
  2. myRealm2=com.github.zhangkaitao.shiro.chapter2.realm.MyRealm2  
  3. myRealm3=com.github.zhangkaitao.shiro.chapter2.realm.MyRealm3  
  4. securityManager.realms=$myRealm1,$myRealm3  

 

2、测试代码(com.github.zhangkaitao.shiro.chapter2.AuthenticatorTest)

2.1、首先通用化登录逻辑 

Java代码  收藏代码

  1. private void login(String configFile) {  
  2.     //1、获取SecurityManager工厂,此处使用Ini配置文件初始化SecurityManager  
  3.     Factory<org.apache.shiro.mgt.SecurityManager> factory =  
  4.             new IniSecurityManagerFactory(configFile);  
  5.   
  6.     //2、得到SecurityManager实例 并绑定给SecurityUtils  
  7.     org.apache.shiro.mgt.SecurityManager securityManager = factory.getInstance();  
  8.     SecurityUtils.setSecurityManager(securityManager);  
  9.   
  10.     //3、得到Subject及创建用户名/密码身份验证Token(即用户身份/凭证)  
  11.     Subject subject = SecurityUtils.getSubject();  
  12.     UsernamePasswordToken token = new UsernamePasswordToken("zhang""123");  
  13.   
  14.     subject.login(token);  
  15. }  

 

2.2、测试AllSuccessfulStrategy成功:    

Java代码  收藏代码

  1. @Test  
  2. public void testAllSuccessfulStrategyWithSuccess() {  
  3.     login("classpath:shiro-authenticator-all-success.ini");  
  4.     Subject subject = SecurityUtils.getSubject();  
  5.   
  6.     //得到一个身份集合,其包含了Realm验证成功的身份信息  
  7.     PrincipalCollection principalCollection = subject.getPrincipals();  
  8.     Assert.assertEquals(2, principalCollection.asList().size());  
  9. }   

即PrincipalCollection包含了zhang和zhang@163.com身份信息。

 

2.3、测试AllSuccessfulStrategy失败:

Java代码  收藏代码

  1.     @Test(expected = UnknownAccountException.class)  
  2.     public void testAllSuccessfulStrategyWithFail() {  
  3.         login("classpath:shiro-authenticator-all-fail.ini");  
  4.         Subject subject = SecurityUtils.getSubject();  
  5. }   

shiro-authenticator-all-fail.ini与shiro-authenticator-all-success.ini不同的配置是使用了securityManager.realms=$myRealm1,$myRealm2;即myRealm验证失败。

 

对于AtLeastOneSuccessfulStrategy和FirstSuccessfulStrategy的区别,请参照testAtLeastOneSuccessfulStrategyWithSuccess和testFirstOneSuccessfulStrategyWithSuccess测试方法。唯一不同点一个是返回所有验证成功的Realm的认证信息;另一个是只返回第一个验证成功的Realm的认证信息。

 

自定义AuthenticationStrategy实现,首先看其API:

Java代码  收藏代码

  1. //在所有Realm验证之前调用  
  2. AuthenticationInfo beforeAllAttempts(  
  3. Collection<? extends Realm> realms, AuthenticationToken token)   
  4. throws AuthenticationException;  
  5. //在每个Realm之前调用  
  6. AuthenticationInfo beforeAttempt(  
  7. Realm realm, AuthenticationToken token, AuthenticationInfo aggregate)   
  8. throws AuthenticationException;  
  9. //在每个Realm之后调用  
  10. AuthenticationInfo afterAttempt(  
  11. Realm realm, AuthenticationToken token,   
  12. AuthenticationInfo singleRealmInfo, AuthenticationInfo aggregateInfo, Throwable t)  
  13. throws AuthenticationException;  
  14. //在所有Realm之后调用  
  15. AuthenticationInfo afterAllAttempts(  
  16. AuthenticationToken token, AuthenticationInfo aggregate)   
  17. throws AuthenticationException;   

因为每个AuthenticationStrategy实例都是无状态的,所有每次都通过接口将相应的认证信息传入下一次流程;通过如上接口可以进行如合并/返回第一个验证成功的认证信息。

 

自定义实现时一般继承org.apache.shiro.authc.pam.AbstractAuthenticationStrategy即可,具体可以参考代码com.github.zhangkaitao.shiro.chapter2.authenticator.strategy包下OnlyOneAuthenticatorStrategy 和AtLeastTwoAuthenticatorStrategy。

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值