【问题标题】:Apache Shiro ThreadContext#bind(Subject) vs ThreadContext.bind(SecurityManager)Apache Shiro ThreadContext#bind(Subject) vs ThreadContext.bind(SecurityManager)
【发布时间】:2020-08-20 17:28:43
【问题描述】:

我刚刚学习 Apache Shiro,遇到了一个问题。在我在互联网上看到的示例中,线程安全的入口点是以下代码:

  try {
        ThreadContext.bind(subject);
        //main code here
    } finally {
        ThreadContext.unbindSubject();
    }

但是,根据documentation有以下方法:

static void bind(SecurityManager securityManager) 便捷方法 这简化了将应用程序的 SecurityManager 实例绑定到 ThreadContext。

static void bind(Subject subject) 简化的便捷方法 将 Subject 绑定到 ThreadContext。

静态SecurityManager unbindSecurityManager() 便捷方法 这简化了应用程序的 SecurityManager 实例的删除 来自线程。

static Subject unbindSubject() 简化的便捷方法 从线程中删除线程本地主题。

谁能解释一下,什么时候(在什么情况下)我们需要绑定 Subject 以及什么时候需要 SecurityManager?

【问题讨论】:

    标签: java shiro


    【解决方案1】:

    这取决于您的代码在做什么,大多数时候您只需要对主题的引用,但是,如果您的自定义代码使用 SecurityManager 执行某些操作,Shiro 也为此提供了一个实用程序。

    也就是说,在大多数情况下,您可能希望改用 subject.excute(...)https://shiro.apache.org/subject.html#thread-association

    TL;DR,除非您有直接使用安全管理器的代码,否则请通过subject.execute 自动使用线程关联

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-02
      • 2018-08-02
      • 2017-09-20
      • 2016-05-07
      • 2017-01-04
      • 2013-03-13
      • 2017-12-29
      • 2014-08-31
      相关资源
      最近更新 更多