《shiro源码分析【整合spring】》(三)——Shiro验证

三、验证

  • 看这一小节之前,请先看前面那一节,本节后面会附上地址。

1、subject咋来的

我们可以看下官方给的登录流程的图片。我们可以很简单的看到shiro的核心是Security Manager。所有的核心操作都在里面,而Subject可以说是使用shiro的入口。

shiroFilter进行处理的时候,会创建一个subject对象。

image

由于我们看的是web项目,所以这一块subject的实例化的真实类型是org.apache.shiro.web.subject.WebSubject,这是一个接口。接下来我们跟踪一下代码。

由上一节我们知道,shiro的入口是org.apache.shiro.web.servlet.AbstractShiroFilter这个类里面的doFilterInternal这个方法。里面就对subject进行了初始化。

    final Subject subject = createSubject(request, response);
protected WebSubject createSubject(ServletRequest request, ServletResponse response) {
    return new WebSubject.Builder(getSecurityManager(), request, response).buildWebSubject();
}

由于WebSubject是一个接口,shiro这里是采用直接实例化一个叫Builder的内部类进行构造WebSubject对象的。

public WebSubject buildWebSubject() {
    //此处调用父类的方法进行构造subject。
    Subject subject = super.buildSubject();
    if (!(subject instanceof WebSubject)) {
        String msg = "Subject implementation returned from the SecurityManager was not a " +
                WebSubject.class.getName() + " implementation.  Please ensure a Web-enabled SecurityManager " +
                "has been configured and made available to this builder.";
        throw new IllegalStateException(msg);
    }
    return (WebSubject) subject;
}

//父类的方法
public Subject buildSubject() {
    return this.securityManager.createSubject(this.subjectContext);
}

我们可以看到,subject的创建工作还是由我们的securityManager完成的。securityManager又是一个接口啊!不怕,我们来看下面一张图。

mark

有木有一种豁然开朗的感觉?反正我是有的,从这个图就可以看到,我们如果不自定义securityManager的话,并且是一个web项目的话,我们就需要实例化最下面那个默认的管理器,即org.apache.shiro.web.mgt.DefaultWebSecurityManager。我在项目中也是这么干的,如果不是web项目的话,直接用org.apache.shiro.mgt.DefaultSecurityManager就足够了,当然他们的都不是final类,我们可以根据自己项目的需求进行扩展。

DefaultWebSecurityManager中并没有重载createSubject这个方法,其真正的实现是在DefaultSecurityManager中,看到这可能会有点疑问,如果是父类实现了这个方法的话,怎么能得到WebSubject的实例呢?还没看源码之前我是很疑惑的!那么直接上源码吧。

public Subject createSubject(SubjectContext subjectContext) {
    //create a copy so we don't modify the argument's backing map:
    SubjectContext context = copy(subjectContext);

    //ensure that the context has a SecurityManager instance, and if not, add one:
    context = ensureSecurityManager(context);

    //Resolve an associated Session (usually based on a referenced session ID), and place it in the context before
    //sending to the SubjectFactory.  The SubjectFactory should not need to know how to acquire sessions as the
    //process is often environment specific - better to shield the SF from these details:
    context = resolveSession(context);

    //Similarly, the SubjectFactory should not require any concept of RememberMe - translate that here first
    //if possible before handing off to the SubjectFactory:
    context = resolvePrincipals(context);
    
    //重点在这里哦
    Subject subject = doCreateSubject(context);

    //save this subject for future reference if necessary:
    //(this is needed here in case rememberMe principals were resolved and they need to be stored in the
    //session, so we don't constantly rehydrate the rememberMe PrincipalCollection on every operation).
    //Added in 1.2:
    save(subject);

    return subject;
}

protected Subject doCreateSubject(SubjectContext context) {
    return getSubjectFactory().createSubject(context);
}
//此处的工厂类是在DefaultSecurityManager或者DefaultWebSecurityManager的构造方法中进行创建的。
public SubjectFactory getSubjectFactory() {
    return subjectFactory;
}
//这里贴出DefaultSecurityManager的构造方法
public DefaultSecurityManager() {
    super();
    this.subjectFactory = new DefaultSubjectFactory();
    this.subjectDAO = new DefaultSubjectDAO();
}
//这里贴出DefaultWebSecurityManager的构造方法
public DefaultWebSecurityManager() {
    super();
    ((DefaultSubjectDAO) this.subjectDAO).setSessionStorageEvaluator(new DefaultWebSessionStorageEvaluator());
    this.sessionMode = HTTP_SESSION_MODE;
    setSubjectFactory(new DefaultWebSubjectFactory());
    setRememberMeManager(new CookieRememberMeManager());
    setSessionManager(new ServletContainerSessionManager());
}

至此我们可以看到是由org.apache.shiro.web.mgt.DefaultWebSubjectFactory这个类进行创建WebSubject对象,如果不是web应用则由org.apache.shiro.mgt.DefaultSubjectFactory进行创建。下面我们就可以看到关键方法了!见证奇迹的时刻到了!

//DefaultWebSubjectFactory中的方法,DefaultSubjectFactory中的方法结构上是没有多大差别的,其实例化的是DelegatingSubject这个对象
public Subject createSubject(SubjectContext context) {
    //判断是不是WebSubjectContext
    if (!(context instanceof WebSubjectContext)) {
        return super.createSubject(context);
    }
    //获取初始化数据,这里几不一一介绍了。
    WebSubjectContext wsc = (WebSubjectContext) context;
    SecurityManager securityManager = wsc.resolveSecurityManager();
    Session session = wsc.resolveSession();
    boolean sessionEnabled = wsc.isSessionCreationEnabled();
    PrincipalCollection principals = wsc.resolvePrincipals();
    boolean authenticated = wsc.resolveAuthenticated();
    String host = wsc.resolveHost();
    ServletRequest request = wsc.resolveServletRequest();
    ServletResponse response = wsc.resolveServletResponse();

    return new WebDelegatingSubject(principals, authenticated, host, session, sessionEnabled,
            request, response, securityManager);
}

2、从登陆开始

shiro的登陆很简单,总体上只需要三步,

  1. 获取Subject:SecurityUtils.getSubject()
  2. 构建Token。例如:org.apache.shiro.authc.UsernamePasswordToken
  3. 登陆。subject.login(token)

SecurityUtils有兴趣的可以自己去研究,大致上就是,在subject创建的时候就会缓存到ThreadLocal中,需要的时候拿出来。

接下来我们从登陆开始,来看看他是怎么干活的。

直接上代码。

public void login(AuthenticationToken token) throws AuthenticationException {
    clearRunAsIdentitiesInternal();
    //这是主要处理方法
    Subject subject = securityManager.login(this, token);

    PrincipalCollection principals;

    String host = null;

    if (subject instanceof DelegatingSubject) {
        DelegatingSubject delegating = (DelegatingSubject) subject;
        //we have to do this in case there are assumed identities - we don't want to lose the 'real' principals:
        principals = delegating.principals;
        host = delegating.host;
    } else {
        principals = subject.getPrincipals();
    }

    if (principals == null || principals.isEmpty()) {
        String msg = "Principals returned from securityManager.login( token ) returned a null or " +
                "empty value.  This value must be non null and populated with one or more elements.";
        throw new IllegalStateException(msg);
    }
    this.principals = principals;
    this.authenticated = true;
    if (token instanceof HostAuthenticationToken) {
        host = ((HostAuthenticationToken) token).getHost();
    }
    if (host != null) {
        this.host = host;
    }
    Session session = subject.getSession(false);
    if (session != null) {
        this.session = decorate(session);
    } else {
        this.session = null;
    }
}

我们可以很清楚地看到真正执行登录的是:securityManager.login(this, token);
继续上源码。

public Subject login(Subject subject, AuthenticationToken token) throws AuthenticationException {
    AuthenticationInfo info;
    try {
        //如果登录成功会返回AuthenticationInfo
        //如果失败则会抛出异常
        info = authenticate(token);
    } catch (AuthenticationException ae) {
        try {
            //失败的处理
            onFailedLogin(token, ae, subject);
        } catch (Exception e) {
            if (log.isInfoEnabled()) {
                log.info("onFailedLogin method threw an " +
                        "exception.  Logging and propagating original AuthenticationException.", e);
            }
        }
        //继续向上抛出
        throw ae; //propagate
    }
    
    //重新构造Subject
    Subject loggedIn = createSubject(token, info, subject);
    
    ///登录成功的后续处理
    onSuccessfulLogin(token, info, loggedIn);

    return loggedIn;
}

public AuthenticationInfo authenticate(AuthenticationToken token) throws AuthenticationException {
    //交给认证器进行认证,此处认证器是在构造方法中进行初始化的,默认构造器的构造的类是:org.apache.shiro.authc.pam.ModularRealmAuthenticator,我们也可以自己继进行实现,并注入到securityManager中
    return this.authenticator.authenticate(token);
}

先看下面的图吧。

mark

这里的Authenticator有多个实现类,但是authenticate这个方法的实现是在org.apache.shiro.authc.AbstractAuthenticatororg.apache.shiro.mgt.AuthenticatingSecurityManager,但是后者他仅仅是调用他注入的Authenticator的实现,到最后还是回到前者执行的代码,这里直接贴出前者的代码。

public final AuthenticationInfo authenticate(AuthenticationToken token) throws AuthenticationException {

    if (token == null) {
        throw new IllegalArgumentException("Method argument (authentication token) cannot be null.");
    }

    log.trace("Authentication attempt received for token [{}]", token);

    AuthenticationInfo info;
    try {
        // 交由doAuthenticate进行处理
        info = doAuthenticate(token);
        if (info == null) {
            String msg = "No account information found for authentication token [" + token + "] by this " +
                    "Authenticator instance.  Please check that it is configured correctly.";
            throw new AuthenticationException(msg);
        }
    } catch (Throwable t) {
        AuthenticationException ae = null;
        if (t instanceof AuthenticationException) {
            ae = (AuthenticationException) t;
        }
        if (ae == null) {
            //Exception thrown was not an expected AuthenticationException.  Therefore it is probably a little more
            //severe or unexpected.  So, wrap in an AuthenticationException, log to warn, and propagate:
            String msg = "Authentication failed for token submission [" + token + "].  Possible unexpected " +
                    "error? (Typical or expected login exceptions should extend from AuthenticationException).";
            ae = new AuthenticationException(msg, t);
            if (log.isWarnEnabled())
                log.warn(msg, t);
        }
        try {
            notifyFailure(token, ae);
        } catch (Throwable t2) {
            if (log.isWarnEnabled()) {
                String msg = "Unable to send notification for failed authentication attempt - listener error?.  " +
                        "Please check your AuthenticationListener implementation(s).  Logging sending exception " +
                        "and propagating original AuthenticationException instead...";
                log.warn(msg, t2);
            }
        }


        throw ae;
    }

    log.debug("Authentication successful for token [{}].  Returned account [{}]", token, info);

    notifySuccess(token, info);

    return info;
}
//这是一个抽象方法,此处贴的是`org.apache.shiro.authc.pam.ModularRealmAuthenticator`这一子类的实现
protected AuthenticationInfo doAuthenticate(AuthenticationToken authenticationToken) throws AuthenticationException {
    assertRealmsConfigured();
    //此处的realm就是我们注入的realm了,这是一个集合,此处判断集合是不是一个
    Collection<Realm> realms = getRealms();
    if (realms.size() == 1) {
        return doSingleRealmAuthentication(realms.iterator().next(), authenticationToken);
    } else {
        return doMultiRealmAuthentication(realms, authenticationToken);
    }
}

protected AuthenticationInfo doSingleRealmAuthentication(Realm realm, AuthenticationToken token) {
    if (!realm.supports(token)) {
        String msg = "Realm [" + realm + "] does not support authentication token [" +
                token + "].  Please ensure that the appropriate Realm implementation is " +
                "configured correctly or that the realm accepts AuthenticationTokens of this type.";
        throw new UnsupportedTokenException(msg);
    }
    AuthenticationInfo info = realm.getAuthenticationInfo(token);
    if (info == null) {
        String msg = "Realm [" + realm + "] was unable to find account data for the " +
                "submitted AuthenticationToken [" + token + "].";
        throw new UnknownAccountException(msg);
    }
    return info;
}

此处我只贴出单个realm的处理源码,多个realms的有兴趣的留给大家去研究吧,不过还是说下思路吧,这边既然是多个realm了,那么就是遍历每一个realm,并分别执行realm的getAuthenticationInfo并获取AuthenticationInfo,有点不同的是,在获取AuthenticationInfo之后要将多个AuthenticationInfo合并成一个,shiro这一块有一个merge方法,大家有兴趣可以找找,算是留下点东西给你探索吧(其实是我不想写了)。

我们继续看getAuthenticationInfo

public final AuthenticationInfo getAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
    //从缓存中获取
    AuthenticationInfo info = getCachedAuthenticationInfo(token);
    if (info == null) {
        //otherwise not cached, perform the lookup:
        //核心
        info = doGetAuthenticationInfo(token);
        log.debug("Looked up AuthenticationInfo [{}] from doGetAuthenticationInfo", info);
        //缓存info
        if (token != null && info != null) {
            cacheAuthenticationInfoIfPossible(token, info);
        }
    } else {
        log.debug("Using cached authentication info [{}] to perform credentials matching.", info);
    }

    if (info != null) {
        assertCredentialsMatch(token, info);
    } else {
        log.debug("No AuthenticationInfo found for submitted AuthenticationToken [{}].  Returning null.", token);
    }

    return info;
}

接下来看看我实现的一个realm里面的doGetAuthenticationInfo方法的实现,在实际项目使用中也可以这样。

@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
    String principal = token.getPrincipal().toString();
    User u = userService.loadByPrincipal(principal);
    if (u == null) {
        throw new UnknownAccountException();
    }
    
    SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(u, u.getPassword(),
            ByteSource.Util.bytes(u.getEmail()), this.getName());
    return authenticationInfo;
}

《shiro源码分析【整合spring】》(二)——Shiro过滤器

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,482评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,377评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 152,762评论 0 342
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,273评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,289评论 5 373
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,046评论 1 285
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,351评论 3 400
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,988评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,476评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,948评论 2 324
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,064评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,712评论 4 323
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,261评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,264评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,486评论 1 262
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,511评论 2 354
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,802评论 2 345

推荐阅读更多精彩内容