zoukankan      html  css  js  c++  java
  • Authenticator及AuthenticationStrategy

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

    如果验证成功,将返回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 不同的是返回时的身份变了;

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

    #指定securityManager的authenticator实现
    authenticator=org.apache.shiro.authc.pam.ModularRealmAuthenticator
    securityManager.authenticator=$authenticator
    #指定securityManager.authenticator的authenticationStrategy
    allSuccessfulStrategy=org.apache.shiro.authc.pam.AllSuccessfulStrategy
    securityManager.authenticator.authenticationStrategy=$allSuccessfulStrategy
    myRealm1=com.github.zhangkaitao.shiro.chapter2.realm.MyRealm1
    myRealm2=com.github.zhangkaitao.shiro.chapter2.realm.MyRealm2
    myRealm3=com.github.zhangkaitao.shiro.chapter2.realm.MyRealm3
    securityManager.realms=$myRealm1,$myRealm3

    自定义AuthenticationStrategy实现

    //在所有Realm验证之前调用
    AuthenticationInfo beforeAllAttempts(
    Collection<? extends Realm> realms, AuthenticationToken token)
    throws AuthenticationException;
    //在每个Realm之前调用
    AuthenticationInfo beforeAttempt(
    Realm realm, AuthenticationToken token, AuthenticationInfo aggregate)
    throws AuthenticationException;
    //在每个Realm之后调用
    AuthenticationInfo afterAttempt(
    Realm realm, AuthenticationToken token,
    AuthenticationInfo singleRealmInfo, AuthenticationInfo aggregateInfo, Throwable t)
    throws AuthenticationException;
    //在所有Realm之后调用
    AuthenticationInfo afterAllAttempts(
    AuthenticationToken token, AuthenticationInfo aggregate)
    throws AuthenticationException;

    自定义实现时一般继承org.apache.shiro.authc.pam.AbstractAuthenticationStrategy即可

  • 相关阅读:
    《Codeforces Round #696 (Div. 2)》
    从Java源码角度聊设计模式之工厂模式
    HashMap死循环【基于JDK1.7】情景重现与分析(带示意图)
    Java应用级别的线程中断—interrupt&isInterrupted&interrupted的区别
    Java队列学习笔记(2)---DelayQueue
    Java队列学习笔记(1)---AbstractQueue&PriorityQueue
    junit测试用例从src/test/resources下的txt文件读取多行数据并随机选择一行
    (Ant编程) Ienumerable类型、枚举器 和自定义的 linq
    如何 给select 下拉框 设定默认值
    分页sql
  • 原文地址:https://www.cnblogs.com/s648667069/p/6635233.html
Copyright © 2011-2022 走看看