【问题标题】:Why Shiro's SubjectCallable need invoke restore method?为什么 Shiro 的 SubjectCallable 需要调用 restore 方法?
【发布时间】:2018-07-09 08:13:12
【问题描述】:

SubjectCallable的调用方法:

public V call() throws Exception {
    try {
        threadState.bind();
        return doCall(this.callable);
    } finally {
        threadState.restore();
    }
}

1.bind方法是必须的,但是restore是为什么?

public void bind() {
    SecurityManager securityManager = this.securityManager;
    if ( securityManager == null ) {
        //try just in case the constructor didn't find one at the time:
        securityManager = ThreadContext.getSecurityManager();
    }
    this.originalResources = ThreadContext.getResources();
    ThreadContext.remove();

    ThreadContext.bind(this.subject);
    if (securityManager != null) {
        ThreadContext.bind(securityManager);
    }
}

public void restore() {
    ThreadContext.remove();
    if (!CollectionUtils.isEmpty(this.originalResources)) {
        ThreadContext.setResources(this.originalResources);
    }
}

2.originalResources是用来做什么的?每次进入 AbstractShiroFilter 都会创建一个新的 subject 并调用它的 execute 方法,originalResources 似乎没用。

【问题讨论】:

    标签: shiro


    【解决方案1】:

    一般线程健康。您需要清理资源以防线程被重新使用(非常常见)。它也有助于垃圾收集。

    你曾经去远足吗?不留痕迹;)

    【讨论】:

    • 请求进来的时候一切都是新的,Subject,SubjectCallable和SubjectThreadState,所以原来的Resources似乎没用,它属于当前线程,但是当前线程刚刚进来,没有资源放在 ThreadContext 中。
    猜你喜欢
    • 2016-04-13
    • 2015-04-30
    • 2015-08-09
    • 2013-04-10
    • 2018-07-19
    • 2010-11-02
    • 1970-01-01
    • 1970-01-01
    • 2013-05-02
    相关资源
    最近更新 更多