Shiro浅析:Shiro的登录验证过程

Posted 只有那些躺在坑里从不仰望高空的人,才不会再掉进坑里

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Shiro浅析:Shiro的登录验证过程相关的知识,希望对你有一定的参考价值。

Shiro浅析:Shiro的登录验证过程

shiro的登录验证是从Subject.login开始的

Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePassword(username, password [, remenberme]);
subject.login(token);

下面来看看Subject的实现类org.apache.shiro.subject.support.DelegatingSubject类的login方法是怎么实现的

public void login(AuthenticationToken token) throws AuthenticationException {
        clearRunAsIdentitiesInternal();
    	// 代理的SecurityManager
        Subject subject = securityManager.login(this, token);
		/**……*/
        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;
        }
    }

可以发现Subject的login方法其实是SecurityManger接口实现的DefaultSecurityManager的代理。我们接着往下看DefaultSecurityManager的login方法

public Subject login(Subject subject, AuthenticationToken token) throws AuthenticationException {
        AuthenticationInfo info;
        try {
            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 loggedIn = createSubject(token, info, subject);

        onSuccessfulLogin(token, info, loggedIn);

        return loggedIn;
    }

public AuthenticationInfo authenticate(AuthenticationToken token) throws AuthenticationException {
        return this.authenticator.authenticate(token);
    }

调用Authenticator.authenticate方法获取info,最后到了AbstractAuthenticator类

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

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

        AuthenticationInfo info;
        try {
            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) {
        }

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

        notifySuccess(token, info);

        return info;

我们可以看到一个与重写realm中类似方法doAuthenticate,我们接着问下翻,发现doAuthenticate是来自AbstractAuthenticator的ModularRealmAuthenticator子类

protected AuthenticationInfo doAuthenticate(AuthenticationToken authenticationToken) throws AuthenticationException {
        assertRealmsConfigured();
        Collection<Realm> realms = getRealms();
        if (realms.size() == 1) {
            return doSingleRealmAuthentication(realms.iterator().next(), authenticationToken);
        } else {
            return doMultiRealmAuthentication(realms, authenticationToken);
        }
    }

可以发现这里实际上是获取了我们在Shiro的配置类注册的Realm类,同时对一个或是多个Realm配置执行不同的方法。

@Bean
    public SecurityManager securityManager() {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(loginRealm());
        securityManager.setSessionManager(sessionManager());
        return securityManager;
    }

接下来doSingleRealmAuthentication获取AuthenticationInfo类型的实体, 。而我们重写的realm类也是返回的AuthenticationInfo

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;
    }
public final AuthenticationInfo getAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		// 尝试从缓存中读取token
        AuthenticationInfo info = getCachedAuthenticationInfo(token);
        if (info == null) {
            //otherwise not cached, perform the lookup:
            
            // 调用自己realm类的doGetAuthenticationInfo
            info = doGetAuthenticationInfo(token);
            log.debug("Looked up AuthenticationInfo [{}] from doGetAuthenticationInfo", 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;
    }

protected abstract AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException;

getAuthenticationInfo来自AuthenticatingRealm类,我们写的realm类也是继承自这个抽象类

以上是关于Shiro浅析:Shiro的登录验证过程的主要内容,如果未能解决你的问题,请参考以下文章

springboot整合shiro实现登录验证授权

Shiro安全框架入门篇(登录验证实例详解与源码)

shiro登录认证过程讲解

Java hibernate 与 Apache Shiro 登录身份验证在“成功”登录后返回 false

shiro登录认证过程讲解

shiro默认登录