Shiro SessionManager会话管理器

Posted BINGJJFLY

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Shiro SessionManager会话管理器相关的知识,希望对你有一定的参考价值。

SessionManager所有的行为

public interface SessionManager {

    // 开启一个新的会话
    Session start(SessionContext context);

    // 获得会话
    Session getSession(SessionKey key) throws SessionException;
}

登录

Subject currentUser = SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken("username", "password");
currentUser.login(token);            

DelegatingSubject的login

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;
    }
}

 

以上是关于Shiro SessionManager会话管理器的主要内容,如果未能解决你的问题,请参考以下文章

Shiro Session管理——操作session

shiro框架04会话管理+缓存管理+Ehcache使用

Apache shiro 属性“sessionManager.globalSessionTimeout”不存在

shiro源码分析2

Shiro的校验Session是否过期处理的过程

注册新的undertow SessionManager