org.apache.shiro.subject.Subject.execute()方法的使用及代码示例

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

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

Subject.execute介绍

[英]Associates the specified Runnable with this Subject instance and then executes it on the currently running thread. If you want to execute the Runnable on a different thread, it is better to use the #associateWith(Runnable) method instead.

Note: This method is primarily provided to execute existing/legacy Runnable implementations. It is better for new code to use #execute(Callable) since that supports the ability to return values and catch exceptions.
[中]将指定的Runnable与此主题实例关联,然后在当前运行的线程上执行它。如果希望在不同的线程上执行Runnable,最好使用#associateWith(Runnable)方法。
注意:此方法主要用于执行现有的/遗留的可运行实现。新代码最好使用#execute(Callable),因为它支持返回值和捕获异常。

代码示例

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

subject.execute(new Callable() {
  public Object call() throws Exception {
    updateSessionLastAccessTime(request, response);

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

return subject.execute(new Callable() {
  public Object call() throws Exception {
    return SecureRemoteInvocationExecutor.super.invoke(invocation, targetObject);

代码示例来源: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.knox/gateway-provider-security-shiro

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 
  throws IOException, ServletException {
 
 Subject subject = SecurityUtils.getSubject();
 
 // trigger call to shiro authorization realm
 // we use shiro authorization realm to look up groups
 subject.hasRole("authenticatedUser");
 CallableChain callableChain = new CallableChain(request, response, chain);
 SecurityUtils.getSubject().execute(callableChain);
}

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

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
  throws IOException, ServletException {
 Subject subject = SecurityUtils.getSubject();
 // trigger call to shiro authorization realm
 // we use shiro authorization realm to look up groups
 subject.hasRole("authenticatedUser");
 CallableChain callableChain = new CallableChain(request, response, chain);
 SecurityUtils.getSubject().execute(callableChain);
}

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

return subject.execute(new Callable() {
  public Object call() throws Exception {
    return SecureRemoteInvocationExecutor.super.invoke(invocation, targetObject);

相关文章