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

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

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

ThreadContext.unbindSubject介绍

[英]Convenience method that simplifies removal of a thread-local Subject from the thread.

The implementation just helps reduce casting and remembering of the ThreadContext key name, i.e it is merely a convenient wrapper for the following:

return (Subject)remove( SUBJECT_KEY );

If you wish to just retrieve the object from the thread without removing it (so it can be retrieved later during thread execution), you should use the #getSubject() method for that purpose.
[中]简化从线程中删除线程本地主题的便捷方法。
该实现只帮助减少对ThreadContext键名称的强制转换和记忆,也就是说,它只是以下内容的方便包装:
return (Subject)remove( SUBJECT_KEY );
如果只想从线程中检索对象,而不删除它(以便稍后在线程执行期间检索),那么应该使用#getSubject()方法。

代码示例

代码示例来源:origin: Graylog2/graylog2-server

@Override
  public void configure(ResourceInfo resourceInfo, FeatureContext context) {
    final Class<?> resourceClass = resourceInfo.getResourceClass();
    final Method resourceMethod = resourceInfo.getResourceMethod();

    context.register(ShiroSecurityContextFilter.class);

    if (resourceMethod.isAnnotationPresent(RequiresAuthentication.class) || resourceClass.isAnnotationPresent(RequiresAuthentication.class)) {
      if (resourceMethod.isAnnotationPresent(RequiresGuest.class)) {
        LOG.debug("Resource method {}#{} is marked as unauthenticated, skipping setting filter.");
      } else {
        LOG.debug("Resource method {}#{} requires an authenticated user.", resourceClass.getCanonicalName(), resourceMethod.getName());
        context.register(new ShiroAuthenticationFilter());
      }
    }

    if (resourceMethod.isAnnotationPresent(RequiresPermissions.class) || resourceClass.isAnnotationPresent(RequiresPermissions.class)) {
      RequiresPermissions requiresPermissions = resourceClass.getAnnotation(RequiresPermissions.class);
      if (requiresPermissions == null) {
        requiresPermissions = resourceMethod.getAnnotation(RequiresPermissions.class);
      }

      LOG.debug("Resource method {}#{} requires an authorization checks.", resourceClass.getCanonicalName(), resourceMethod.getName());
      context.register(new ShiroAuthorizationFilter(requiresPermissions));
    }

    // TODO this is the wrong approach, we should have an Environment and proper request wrapping
    context.register((ContainerResponseFilter) (requestContext, responseContext) -> ThreadContext.unbindSubject());
  }
}

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

@Test
public void testScope() throws Exception {
  Subject subject = createMock(Subject.class);
  try {
    ThreadContext.bind(subject);
    final Key<SomeClass> key = Key.get(SomeClass.class);
    Provider<SomeClass> mockProvider = createMock(Provider.class);
    Session session = createMock(Session.class);
    SomeClass retuned = new SomeClass();
    expect(subject.getSession()).andReturn(session);
    expect(session.getAttribute(key)).andReturn(null);
    expect(mockProvider.get()).andReturn(retuned);
    expect(subject.getSession()).andReturn(session);
    expect(session.getAttribute(key)).andReturn(retuned);
    replay(subject, mockProvider, session);
    ShiroSessionScope underTest = new ShiroSessionScope();
    // first time the session doesn't contain it, we expect the provider to be invoked
    assertSame(retuned, underTest.scope(key, mockProvider).get());
    // second time the session does contain it, we expect the provider to not be invoked
    assertSame(retuned, underTest.scope(key, mockProvider).get());
    verify(subject, mockProvider, session);
  } finally {
    ThreadContext.unbindSubject();
  }
}

代码示例来源:origin: org.graylog2/graylog2-shared

@Override
  public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) {
    ThreadContext.unbindSubject();
  }
});

代码示例来源:origin: org.sonatype.nexus/nexus-app

public void logout()
{
  ThreadContext.unbindSubject();
}

代码示例来源:origin: be.c4j.ee.security.octopus/octopus-core

public void releaseSubject() {
  ThreadContext.unbindSubject();
}

代码示例来源:origin: org.seedstack.addons.ws/web-services-core

@Override
  public void close(MessageContext context) {
    ThreadContext.unbindSubject();
    ThreadContext.unbindSecurityManager();
  }
}

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

@Override
public void passivateService()
    throws Exception
{
  ThreadContext.unbindSubject();
  ThreadContext.unbindSecurityManager();
}

代码示例来源:origin: apache/attic-polygene-java

@Override
public void passivateService()
    throws Exception
{
  ThreadContext.unbindSubject();
  ThreadContext.unbindSecurityManager();
}

代码示例来源:origin: org.seedstack.seed/seed-shell-support

@Override
public void destroy() {
  ses.shutdownNow();
  ThreadContext.unbindSubject();
  ThreadContext.unbindSecurityManager();
}

代码示例来源:origin: org.seedstack.seed/seed-shell

@Override
public void destroy() {
  ses.shutdownNow();
  ThreadContext.unbindSubject();
  ThreadContext.unbindSecurityManager();
}

代码示例来源:origin: org.seedstack.seed/seed-security-core

@Override
public void afterTest(TestContext testContext) {
  if (subject != null) {
    LOGGER.info("Logging user out", testContext.testMethod());
    subject.logout();
    ThreadContext.unbindSecurityManager();
    ThreadContext.unbindSubject();
  }
}

代码示例来源:origin: theonedev/onedev

@Override
public void run() {
  try {
    ThreadContext.bind(subject);
    check(load(requestId));
  } catch (Exception e) {
    logger.error("Error checking pull request status", e);
  } finally {
    ThreadContext.unbindSubject();
  }
}

代码示例来源:origin: org.seedstack.seed/seed-shell

@Override
public void destroy() {
  ses.shutdownNow();
  consoleReader.shutdown();
  ThreadContext.unbindSubject();
  ThreadContext.unbindSecurityManager();
}

代码示例来源:origin: apache/attic-polygene-java

public void interactionEnds()
{
  ThreadContext.unbindSubject();
  ThreadContext.unbindSecurityManager();
}
// END SNIPPET: thread-context

代码示例来源:origin: org.seedstack.seed/seed-shell-support

@Override
public void destroy() {
  ses.shutdownNow();
  consoleReader.shutdown();
  ThreadContext.unbindSubject();
  ThreadContext.unbindSecurityManager();
}

代码示例来源:origin: theonedev/onedev

@Sessional
@Override
public void postStart() {
  listenerRegistry.post(new SystemStarted());
  ThreadContext.unbindSubject();
  logger.info("Server is ready at " + configManager.getSystemSetting().getServerUrl() + ".");
  initStage = null;
}

代码示例来源:origin: theonedev/onedev

@Override
public void run() {
  ThreadContext.bind(subject);
  try {
    PullRequest request = load(requestId);
    request.getTargetProject().cacheObjectId(request.getTargetRef(), newTargetHeadId);
    listenerRegistry.post(new RefUpdated(request.getTargetProject(), targetRef, 
          targetHeadId, newTargetHeadId));
  } finally {
    ThreadContext.unbindSubject();
  }
}

代码示例来源:origin: theonedev/onedev

@Override
public void run() {
  ThreadContext.bind(subject);
  try {
    Project project = OneDev.getInstance(ProjectManager.class).load(projectId);
    project.cacheObjectId(branch, refUpdated.getNewCommitId());
    RefUpdated refUpdated = new RefUpdated(project, refName, oldCommitId, newCommitId);
    OneDev.getInstance(ListenerRegistry.class).post(refUpdated);
  } finally {
    ThreadContext.unbindSubject();
  }
}

代码示例来源:origin: theonedev/onedev

@Override
public void run() {
  ThreadContext.bind(subject);
  try {
    Project project = OneDev.getInstance(ProjectManager.class).load(getId());
    OneDev.getInstance(ListenerRegistry.class).post(
        new RefUpdated(project, refName, commitId, ObjectId.zeroId()));
  } finally {
    ThreadContext.unbindSubject();
  }
}

代码示例来源:origin: theonedev/onedev

@Override
public void run() {
  ThreadContext.bind(subject);
  try {
    Project project = OneDev.getInstance(ProjectManager.class).load(getId());
    OneDev.getInstance(ListenerRegistry.class).post(
        new RefUpdated(project, refName, commitId, ObjectId.zeroId()));
  } finally {
    ThreadContext.unbindSubject();
  }
}

相关文章