Java ApacheShiro ThreadContext#bind(主题)与ThreadContext.bind(SecurityManager)

Java ApacheShiro ThreadContext#bind(主题)与ThreadContext.bind(SecurityManager),java,shiro,Java,Shiro,我正在学习ApacheShiro并面临一个问题。在我在internet中看到的示例中,线程安全性的入口点是以下代码: try { ThreadContext.bind(subject); //main code here } finally { ThreadContext.unbindSubject(); } 然而,根据该法,有以下几种方法: 静态无效绑定(SecurityManager SecurityManager)便利方

我正在学习ApacheShiro并面临一个问题。在我在internet中看到的示例中,线程安全性的入口点是以下代码:

  try {
        ThreadContext.bind(subject);
        //main code here
    } finally {
        ThreadContext.unbindSubject();
    }
然而,根据该法,有以下几种方法:

静态无效绑定(SecurityManager SecurityManager)便利方法 这简化了将应用程序的SecurityManager实例绑定到 线程上下文

静态无效绑定(主题-主题)简化 将主题绑定到ThreadContext

静态SecurityManager取消绑定SecurityManager()便利方法 这简化了删除应用程序的SecurityManager实例 从线程

静态Subject unbindSubject()简化 从线程中删除线程本地主题


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

这取决于您的代码在做什么,大多数时候您只需要引用您的Subject,但是,如果您的自定义代码使用SecurityManager做了一些事情,Shiro也为此提供了一个实用程序

也就是说,您可能希望在大多数情况下使用
subject.excute(…)

TL;DR,除非您有直接使用安全管理器的代码,否则请通过
主题自动使用线程关联。执行