本文整理了Java中org.apache.shiro.util.ThreadContext.getSubject()
方法的一些代码示例,展示了ThreadContext.getSubject()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ThreadContext.getSubject()
方法的具体详情如下:
包路径:org.apache.shiro.util.ThreadContext
类名称:ThreadContext
方法名:getSubject
[英]Convenience method that simplifies retrieval of a thread-bound Subject. If there is no Subject bound to the thread, this method returns null. It is merely a convenient wrapper for the following:
return (Subject)get( SUBJECT_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 #unbindSubject() instead.
[中]方便的方法,简化了检索线程绑定的主题。如果没有主题绑定到线程,该方法将返回null。它只是以下内容的方便包装:return (Subject)get( SUBJECT_KEY );
此方法仅在绑定值存在时返回它——它不会将其从线程中移除。要删除它,必须调用#unbindSubject()。
代码示例来源:origin: Graylog2/graylog2-server
public void loginSubject() throws AuthenticationException {
subject.login(token);
// the subject instance will change to include the session
final Subject newSubject = ThreadContext.getSubject();
if (newSubject != null) {
subject = newSubject;
}
}
代码示例来源:origin: apache/shiro
public T get() {
Subject subject = ThreadContext.getSubject();
if (subject == null) {
throw new OutOfScopeException("There is no Shiro Session currently in scope.");
}
Session session = subject.getSession();
T scoped = castSessionAttribute(session);
if (scoped == null) {
scoped = unscoped.get();
}
return scoped;
}
代码示例来源:origin: apache/shiro
/**
* Returns the currently accessible {@code Subject} available to the calling code depending on
* runtime environment.
* <p/>
* This method is provided as a way of obtaining a {@code Subject} without having to resort to
* implementation-specific methods. It also allows the Shiro team to change the underlying implementation of
* this method in the future depending on requirements/updates without affecting your code that uses it.
*
* @return the currently accessible {@code Subject} accessible to the calling code.
* @throws IllegalStateException if no {@link Subject Subject} instance or
* {@link SecurityManager SecurityManager} instance is available with which to obtain
* a {@code Subject}, which which is considered an invalid application configuration
* - a Subject should <em>always</em> be available to the caller.
*/
public static Subject getSubject() {
Subject subject = ThreadContext.getSubject();
if (subject == null) {
subject = (new Subject.Builder()).buildSubject();
ThreadContext.bind(subject);
}
return subject;
}
代码示例来源:origin: apache/geode
@Test
public void getSubject_login_logout() throws Exception {
this.securityService.login(new Properties());
Subject subject = this.securityService.getSubject();
assertThat(subject).isNotNull();
assertThat(ThreadContext.getSubject()).isNotNull();
this.securityService.logout();
assertThat(ThreadContext.getSubject()).isNull();
}
代码示例来源: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
/**
* Returns the currently accessible {@code Subject} available to the calling code depending on
* runtime environment.
* <p/>
* This method is provided as a way of obtaining a {@code Subject} without having to resort to
* implementation-specific methods. It also allows the Shiro team to change the underlying implementation of
* this method in the future depending on requirements/updates without affecting your code that uses it.
*
* @return the currently accessible {@code Subject} accessible to the calling code.
* @throws IllegalStateException if no {@link Subject Subject} instance or
* {@link SecurityManager SecurityManager} instance is available with which to obtain
* a {@code Subject}, which which is considered an invalid application configuration
* - a Subject should <em>always</em> be available to the caller.
*/
public static Subject getSubject() {
Subject subject = ThreadContext.getSubject();
if (subject == null) {
subject = (new Subject.Builder()).buildSubject();
ThreadContext.bind(subject);
}
return subject;
}
代码示例来源:origin: org.graylog2/graylog2-server
public void loginSubject() throws AuthenticationException {
subject.login(token);
// the subject instance will change to include the session
final Subject newSubject = ThreadContext.getSubject();
if (newSubject != null) {
subject = newSubject;
}
}
代码示例来源:origin: ueboot/ueboot
/**
* Returns the current auditor of the application.
*
* @return the current auditor
*/
@Override
public String getCurrentAuditor() {
//使用当前登录用户名称作为创建人和最后修改人字段的值
if (ThreadContext.getSubject() != null && SecurityUtils.getSubject() != null) {
return (String) SecurityUtils.getSubject().getPrincipal();
}
return null;
}
}
代码示例来源:origin: org.sonatype.nexus/nexus-app
protected String getCurrentUserId()
{
Subject subject = ThreadContext.getSubject(); // Use ThreadContext directly, SecurityUtils will associate a
// new Subject with the thread.
if ( subject != null && subject.getPrincipal() != null )
{
return subject.getPrincipal().toString();
}
else
{
return null;
}
}
代码示例来源:origin: codice/ddf
/**
* Appends any additional attributes as defined in the comma-delimited system property {@link
* #EXTRA_ATTRIBUTES_PROP}.
*
* @param subject the subject of the logging request
* @param messageBuilder buffer to which to append attribute text, if any
*/
private static void appendConditionalAttributes(Subject subject, StringBuilder messageBuilder) {
String attributes = System.getProperty(EXTRA_ATTRIBUTES_PROP);
if (attributes == null) {
return;
}
if (subject == null) {
subject = ThreadContext.getSubject();
}
List<String> attributeList = Arrays.asList(attributes.split(","));
for (String attribute : attributeList) {
List<String> attributeValueList = SubjectUtils.getAttribute(subject, attribute);
if (CollectionUtils.isNotEmpty(attributeValueList)) {
messageBuilder.append(" ").append(attribute).append(" : ");
if (attributeValueList.size() > 1) {
messageBuilder.append(attributeValueList);
} else {
messageBuilder.append(attributeValueList.get(0));
}
}
}
}
代码示例来源:origin: bazaarvoice/emodb
@Override
public Object getValue() {
return new Subject(_securityManager, ThreadContext.getSubject().getPrincipals());
}
};
代码示例来源:origin: stormpath/stormpath-shiro
/**
* Logs out the current Subject (if any) when a {@link LogoutRequestEvent} is published.
* @param event The logout event.
*/
@Subscribe
public void onLogout(LogoutRequestEvent event) {
Subject subject = ThreadContext.getSubject();
if (subject != null) {
subject.logout();
}
}
}
代码示例来源:origin: org.apache.shiro/shiro-guice
public T get() {
Subject subject = ThreadContext.getSubject();
if (subject == null) {
throw new OutOfScopeException("There is no Shiro Session currently in scope.");
}
Session session = subject.getSession();
T scoped = castSessionAttribute(session);
if (scoped == null) {
scoped = unscoped.get();
}
return scoped;
}
代码示例来源:origin: codice/ddf
private static String getUser(Subject subject) {
try {
if (subject == null) {
subject = ThreadContext.getSubject();
}
if (subject == null) {
javax.security.auth.Subject javaSubject =
javax.security.auth.Subject.getSubject(AccessController.getContext());
if (javaSubject != null) {
Set<UserPrincipal> userPrincipal = javaSubject.getPrincipals(UserPrincipal.class);
if (userPrincipal != null && !userPrincipal.isEmpty()) {
return userPrincipal.toArray(new UserPrincipal[1])[0].getName();
}
}
} else {
return SubjectUtils.getName(subject, NO_USER);
}
} catch (Exception e) {
// ignore and return NO_USER
}
return NO_USER;
}
代码示例来源:origin: bazaarvoice/emodb
@Override
public ContainerResponse filter(ContainerRequest request, ContainerResponse response) {
Subject subject = ThreadContext.getSubject();
if (subject != null) {
if (subject.isAuthenticated()) {
subject.logout();
}
ThreadContext.unbindSubject();
}
return response;
}
代码示例来源:origin: org.graylog2/graylog2-shared
public void loginSubject() throws AuthenticationException {
// what a hack :(
ThreadContext.put("REQUEST_HEADERS", headers);
subject.login(token);
// the subject instance will change to include the session
final Subject newSubject = ThreadContext.getSubject();
if (newSubject != null) {
subject = newSubject;
}
ThreadContext.remove("REQUEST_HEADERS");
}
代码示例来源:origin: org.apache.servicemix.bundles/org.apache.servicemix.bundles.shiro
/**
* Returns the currently accessible {@code Subject} available to the calling code depending on
* runtime environment.
* <p/>
* This method is provided as a way of obtaining a {@code Subject} without having to resort to
* implementation-specific methods. It also allows the Shiro team to change the underlying implementation of
* this method in the future depending on requirements/updates without affecting your code that uses it.
*
* @return the currently accessible {@code Subject} accessible to the calling code.
* @throws IllegalStateException if no {@link Subject Subject} instance or
* {@link SecurityManager SecurityManager} instance is available with which to obtain
* a {@code Subject}, which which is considered an invalid application configuration
* - a Subject should <em>always</em> be available to the caller.
*/
public static Subject getSubject() {
Subject subject = ThreadContext.getSubject();
if (subject == null) {
subject = (new Subject.Builder()).buildSubject();
ThreadContext.bind(subject);
}
return subject;
}
代码示例来源:origin: bazaarvoice/emodb
/**
* Authorizes the client for the annotated permissions. If any authorizations fail an {@link AuthorizationException}
* will be thrown, otherwise the original request is returned.
*/
@Override
public ContainerRequest filter(ContainerRequest request) {
Subject subject = ThreadContext.getSubject();
String[] permissions = resolvePermissions(request);
if (permissions.length == 1 || _logical == Logical.AND) {
// Shortcut call to check all permissions at once
subject.checkPermissions(permissions);
} else {
// Check each permission until any passes
boolean anyPermitted = false;
int p = 0;
while (!anyPermitted) {
try {
subject.checkPermission(permissions[p]);
anyPermitted = true;
} catch (AuthorizationException e) {
// If this is the last permission then pass the exception along
if (++p == permissions.length) {
throw e;
}
}
}
}
return request;
}
代码示例来源:origin: org.sonatype.nexus.plugins/nexus-restlet1x-plugin
Subject subject = ThreadContext.getSubject();
内容来源于网络,如有侵权,请联系作者删除!