org.apache.shiro.util.ThreadContext.getSecurityManager()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(11.9k)|赞(0)|评价(0)|浏览(177)

本文整理了Java中org.apache.shiro.util.ThreadContext.getSecurityManager()方法的一些代码示例,展示了ThreadContext.getSecurityManager()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ThreadContext.getSecurityManager()方法的具体详情如下:
包路径:org.apache.shiro.util.ThreadContext
类名称:ThreadContext
方法名:getSecurityManager

ThreadContext.getSecurityManager介绍

[英]Convenience method that simplifies retrieval of the application's SecurityManager instance from the current thread. If there is no SecurityManager bound to the thread (probably because framework code did not bind it to the thread), this method returns null.

It is merely a convenient wrapper for the following:

return (SecurityManager)get( SECURITY_MANAGER_KEY );

This method only returns the bound value if it exists - it does not remove it from the thread. To remove it, one must call #unbindSecurityManager() instead.
[中]简化从当前线程检索应用程序的SecurityManager实例的便捷方法。如果没有绑定到线程的SecurityManager(可能是因为框架代码没有将其绑定到线程),此方法将返回null。
它只是以下内容的方便包装:
return (SecurityManager)get( SECURITY_MANAGER_KEY );
此方法仅在绑定值存在时返回它——它不会将其从线程中移除。要删除它,必须调用#unbindSecurityManager()。

代码示例

代码示例来源:origin: apache/shiro

/**
 * Creates a new {@code SubjectThreadState} that will bind and unbind the specified {@code Subject} to the
 * thread
 *
 * @param subject the {@code Subject} instance to bind and unbind from the {@link ThreadContext}.
 */
public SubjectThreadState(Subject subject) {
  if (subject == null) {
    throw new IllegalArgumentException("Subject argument cannot be null.");
  }
  this.subject = subject;
  SecurityManager securityManager = null;
  if ( subject instanceof DelegatingSubject) {
    securityManager = ((DelegatingSubject)subject).getSecurityManager();
  }
  if ( securityManager == null) {
    securityManager = ThreadContext.getSecurityManager();
  }
  this.securityManager = securityManager;
}

代码示例来源:origin: apache/shiro

/**
   * Returns the SecurityManager accessible to the calling code.
   * <p/>
   * This implementation favors acquiring a thread-bound {@code SecurityManager} if it can find one.  If one is
   * not available to the executing thread, it will attempt to use the static singleton if available (see the
   * {@link #setSecurityManager setSecurityManager} method for more on the static singleton).
   * <p/>
   * If neither the thread-local or static singleton instances are available, this method throws an
   * {@code UnavailableSecurityManagerException} to indicate an error - a SecurityManager should always be accessible
   * to calling code in an application. If it is not, it is likely due to a Shiro configuration problem.
   *
   * @return the SecurityManager accessible to the calling code.
   * @throws UnavailableSecurityManagerException
   *          if there is no {@code SecurityManager} instance available to the
   *          calling code, which typically indicates an invalid application configuration.
   */
  public static SecurityManager getSecurityManager() throws UnavailableSecurityManagerException {
    SecurityManager securityManager = ThreadContext.getSecurityManager();
    if (securityManager == null) {
      securityManager = SecurityUtils.securityManager;
    }
    if (securityManager == null) {
      String msg = "No SecurityManager accessible to the calling code, either bound to the " +
          ThreadContext.class.getName() + " or as a vm static singleton.  This is an invalid application " +
          "configuration.";
      throw new UnavailableSecurityManagerException(msg);
    }
    return securityManager;
  }
}

代码示例来源:origin: apache/shiro

protected Subject createAndBindTestSubject() {
  SecurityManager sm = ThreadContext.getSecurityManager();
  if (sm == null) {
    createAndBindTestSecurityManager();
  }
  return SecurityUtils.getSubject();
}

代码示例来源:origin: apache/shiro

/**
 * Binds a {@link Subject} and {@link org.apache.shiro.mgt.SecurityManager SecurityManager} to the
 * {@link ThreadContext} so they can be retrieved later by any
 * {@code SecurityUtils.}{@link org.apache.shiro.SecurityUtils#getSubject() getSubject()} calls that might occur
 * during the thread's execution.
 * <p/>
 * Prior to binding, the {@code ThreadContext}'s existing {@link ThreadContext#getResources() resources} are
 * retained so they can be restored later via the {@link #restore restore} call.
 */
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);
  }
}

代码示例来源:origin: apache/shiro

@Test
public void testExecuteRunnable() {
  String username = "jsmith";
  SecurityManager securityManager = createNiceMock(SecurityManager.class);
  PrincipalCollection identity = new SimplePrincipalCollection(username, "testRealm");
  final Subject sourceSubject = new DelegatingSubject(identity, true, null, null, securityManager);
  assertNull(ThreadContext.getSubject());
  assertNull(ThreadContext.getSecurityManager());
  Runnable runnable = new Runnable() {
    public void run() {
      Subject callingSubject = SecurityUtils.getSubject();
      assertNotNull(callingSubject);
      assertNotNull(SecurityUtils.getSecurityManager());
      assertEquals(callingSubject, sourceSubject);
    }
  };
  sourceSubject.execute(runnable);
  assertNull(ThreadContext.getSubject());
  assertNull(ThreadContext.getSecurityManager());
}

代码示例来源:origin: apache/shiro

@Test
public void testExecuteCallable() {
  String username = "jsmith";
  SecurityManager securityManager = createNiceMock(SecurityManager.class);
  PrincipalCollection identity = new SimplePrincipalCollection(username, "testRealm");
  final Subject sourceSubject = new DelegatingSubject(identity, true, null, null, securityManager);
  assertNull(ThreadContext.getSubject());
  assertNull(ThreadContext.getSecurityManager());
  Callable<String> callable = new Callable<String>() {
    public String call() throws Exception {
      Subject callingSubject = SecurityUtils.getSubject();
      assertNotNull(callingSubject);
      assertNotNull(SecurityUtils.getSecurityManager());
      assertEquals(callingSubject, sourceSubject);
      return "Hello " + callingSubject.getPrincipal();
    }
  };
  String response = sourceSubject.execute(callable);
  assertNotNull(response);
  assertEquals("Hello " + username, response);
  assertNull(ThreadContext.getSubject());
  assertNull(ThreadContext.getSecurityManager());
}

代码示例来源:origin: org.apache.shiro/shiro-core

/**
 * Creates a new {@code SubjectThreadState} that will bind and unbind the specified {@code Subject} to the
 * thread
 *
 * @param subject the {@code Subject} instance to bind and unbind from the {@link ThreadContext}.
 */
public SubjectThreadState(Subject subject) {
  if (subject == null) {
    throw new IllegalArgumentException("Subject argument cannot be null.");
  }
  this.subject = subject;
  SecurityManager securityManager = null;
  if ( subject instanceof DelegatingSubject) {
    securityManager = ((DelegatingSubject)subject).getSecurityManager();
  }
  if ( securityManager == null) {
    securityManager = ThreadContext.getSecurityManager();
  }
  this.securityManager = securityManager;
}

代码示例来源:origin: org.apache.shiro/shiro-core

/**
   * Returns the SecurityManager accessible to the calling code.
   * <p/>
   * This implementation favors acquiring a thread-bound {@code SecurityManager} if it can find one.  If one is
   * not available to the executing thread, it will attempt to use the static singleton if available (see the
   * {@link #setSecurityManager setSecurityManager} method for more on the static singleton).
   * <p/>
   * If neither the thread-local or static singleton instances are available, this method throws an
   * {@code UnavailableSecurityManagerException} to indicate an error - a SecurityManager should always be accessible
   * to calling code in an application. If it is not, it is likely due to a Shiro configuration problem.
   *
   * @return the SecurityManager accessible to the calling code.
   * @throws UnavailableSecurityManagerException
   *          if there is no {@code SecurityManager} instance available to the
   *          calling code, which typically indicates an invalid application configuration.
   */
  public static SecurityManager getSecurityManager() throws UnavailableSecurityManagerException {
    SecurityManager securityManager = ThreadContext.getSecurityManager();
    if (securityManager == null) {
      securityManager = SecurityUtils.securityManager;
    }
    if (securityManager == null) {
      String msg = "No SecurityManager accessible to the calling code, either bound to the " +
          ThreadContext.class.getName() + " or as a vm static singleton.  This is an invalid application " +
          "configuration.";
      throw new UnavailableSecurityManagerException(msg);
    }
    return securityManager;
  }
}

代码示例来源:origin: org.apache.shiro/shiro-core

/**
 * Binds a {@link Subject} and {@link org.apache.shiro.mgt.SecurityManager SecurityManager} to the
 * {@link ThreadContext} so they can be retrieved later by any
 * {@code SecurityUtils.}{@link org.apache.shiro.SecurityUtils#getSubject() getSubject()} calls that might occur
 * during the thread's execution.
 * <p/>
 * Prior to binding, the {@code ThreadContext}'s existing {@link ThreadContext#getResources() resources} are
 * retained so they can be restored later via the {@link #restore restore} call.
 */
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);
  }
}

代码示例来源:origin: org.apache.servicemix.bundles/org.apache.servicemix.bundles.shiro

/**
 * Creates a new {@code SubjectThreadState} that will bind and unbind the specified {@code Subject} to the
 * thread
 *
 * @param subject the {@code Subject} instance to bind and unbind from the {@link ThreadContext}.
 */
public SubjectThreadState(Subject subject) {
  if (subject == null) {
    throw new IllegalArgumentException("Subject argument cannot be null.");
  }
  this.subject = subject;
  SecurityManager securityManager = null;
  if ( subject instanceof DelegatingSubject) {
    securityManager = ((DelegatingSubject)subject).getSecurityManager();
  }
  if ( securityManager == null) {
    securityManager = ThreadContext.getSecurityManager();
  }
  this.securityManager = securityManager;
}

代码示例来源:origin: org.apache.servicemix.bundles/org.apache.servicemix.bundles.shiro

/**
   * Returns the SecurityManager accessible to the calling code.
   * <p/>
   * This implementation favors acquiring a thread-bound {@code SecurityManager} if it can find one.  If one is
   * not available to the executing thread, it will attempt to use the static singleton if available (see the
   * {@link #setSecurityManager setSecurityManager} method for more on the static singleton).
   * <p/>
   * If neither the thread-local or static singleton instances are available, this method throws an
   * {@code UnavailableSecurityManagerException} to indicate an error - a SecurityManager should always be accessible
   * to calling code in an application. If it is not, it is likely due to a Shiro configuration problem.
   *
   * @return the SecurityManager accessible to the calling code.
   * @throws UnavailableSecurityManagerException
   *          if there is no {@code SecurityManager} instance available to the
   *          calling code, which typically indicates an invalid application configuration.
   */
  public static SecurityManager getSecurityManager() throws UnavailableSecurityManagerException {
    SecurityManager securityManager = ThreadContext.getSecurityManager();
    if (securityManager == null) {
      securityManager = SecurityUtils.securityManager;
    }
    if (securityManager == null) {
      String msg = "No SecurityManager accessible to the calling code, either bound to the " +
          ThreadContext.class.getName() + " or as a vm static singleton.  This is an invalid application " +
          "configuration.";
      throw new UnavailableSecurityManagerException(msg);
    }
    return securityManager;
  }
}

代码示例来源:origin: org.apache.servicemix.bundles/org.apache.servicemix.bundles.shiro

/**
 * Binds a {@link Subject} and {@link org.apache.shiro.mgt.SecurityManager SecurityManager} to the
 * {@link ThreadContext} so they can be retrieved later by any
 * {@code SecurityUtils.}{@link org.apache.shiro.SecurityUtils#getSubject() getSubject()} calls that might occur
 * during the thread's execution.
 * <p/>
 * Prior to binding, the {@code ThreadContext}'s existing {@link ThreadContext#getResources() resources} are
 * retained so they can be restored later via the {@link #restore restore} call.
 */
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);
  }
}

代码示例来源:origin: org.wicketstuff/wicket-shiro

if (action == constraint.action())
  final SecurityManager sm = ThreadContext.getSecurityManager();
  final Subject subject = SecurityUtils.getSubject();
  switch (constraint.constraint())

相关文章