javax.security.auth.Subject.getSubject()方法的使用及代码示例

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

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

Subject.getSubject介绍

[英]Returns the Subject that was last associated with the context provided as argument.
[中]返回上次与作为参数提供的上下文关联的主题。

代码示例

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

@Override
  public Subject run() {
    return Subject.getSubject(AccessController.getContext());
  }
});

代码示例来源:origin: alibaba/jstorm

@VisibleForTesting
public ReqContext(AccessControlContext acl_ctxt) {
  _subject = Subject.getSubject(acl_ctxt);
  _reqID = uniqueId.incrementAndGet();
}

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

@VisibleForTesting
public ReqContext(AccessControlContext acl_ctxt) {
  subject = Subject.getSubject(acl_ctxt);
  reqID = uniqueId.incrementAndGet();
}

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

private void handleCallback(OAuthBearerTokenCallback callback) throws IOException {
  if (callback.token() != null)
    throw new IllegalArgumentException("Callback had a token already");
  Subject subject = Subject.getSubject(AccessController.getContext());
  Set<OAuthBearerToken> privateCredentials = subject != null
    ? subject.getPrivateCredentials(OAuthBearerToken.class)
    : Collections.emptySet();
  if (privateCredentials.size() != 1)
    throw new IOException(
        String.format("Unable to find OAuth Bearer token in Subject's private credentials (size=%d)",
            privateCredentials.size()));
  callback.token(privateCredentials.iterator().next());
}

代码示例来源:origin: spring-projects/spring-framework

@Override
  public String run() {
    Subject subject = Subject.getSubject(acc);
    if (subject == null) {
      return null;
    }
    Set<Principal> principals = subject.getPrincipals();
    if (principals == null) {
      return null;
    }
    for (Principal p : principals) {
      return p.getName();
    }
    return null;
  }
});

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

@Override
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
  if (!configured())
    throw new IllegalStateException("Callback handler not configured");
  for (Callback callback : callbacks) {
    if (callback instanceof OAuthBearerTokenCallback)
      handleCallback((OAuthBearerTokenCallback) callback);
    else if (callback instanceof SaslExtensionsCallback)
      handleCallback((SaslExtensionsCallback) callback, Subject.getSubject(AccessController.getContext()));
    else
      throw new UnsupportedCallbackException(callback);
  }
}

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

@Override
public void open() throws TTransportException {
 try {
  AccessControlContext context = AccessController.getContext();
  Subject subject = Subject.getSubject(context);
  Subject.doAs(subject, new PrivilegedExceptionAction<Void>() {
   public Void run() {
    try {
     wrapped.open();
    } catch (TTransportException tte) {
     // Wrap the transport exception in an RTE, since Subject.doAs() then goes
     // and unwraps this for us out of the doAs block. We then unwrap one
     // more time in our catch clause to get back the TTE. (ugh)
     throw new RuntimeException(tte);
    }
    return null;
   }
  });
 } catch (PrivilegedActionException ioe) {
  throw new RuntimeException("Received an ioe we never threw!", ioe);
 } catch (RuntimeException rte) {
  if (rte.getCause() instanceof TTransportException) {
   throw (TTransportException) rte.getCause();
  } else {
   throw rte;
  }
 }
}

代码示例来源:origin: spring-projects/spring-framework

public void init() {
  AccessControlContext acc = AccessController.getContext();
  Subject subject = Subject.getSubject(acc);
  if (subject == null) {
    return;
  }
  setNameFromPrincipal(subject.getPrincipals());
}

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

/**
   * Look in the current subject for a WorkerToken.  This should really only happen when we are in a worker, because the tokens will not
   * be placed in anything else.
   *
   * @param type the type of connection we need a token for.
   * @return the found token or null.
   */
  public static WorkerToken findWorkerTokenInSubject(ThriftConnectionType type) {
    WorkerTokenServiceType serviceType = type.getWtType();
    WorkerToken ret = null;
    if (serviceType != null) {
      Subject subject = Subject.getSubject(AccessController.getContext());
      if (subject != null) {
        ret = ClientAuthUtils.findWorkerToken(subject, serviceType);
      }
    }
    return ret;
  }
}

代码示例来源:origin: spring-projects/spring-security

@GetMapping("/username")
  public String username() {
    Subject subject = Subject.getSubject(AccessController.getContext());
    return subject.getPrincipals().iterator().next().getName();
  }
}

代码示例来源:origin: org.apache.hadoop/hadoop-common

/**
 * Return the current user, including any doAs in the current stack.
 * @return the current user
 * @throws IOException if login fails
 */
@InterfaceAudience.Public
@InterfaceStability.Evolving
public static UserGroupInformation getCurrentUser() throws IOException {
 AccessControlContext context = AccessController.getContext();
 Subject subject = Subject.getSubject(context);
 if (subject == null || subject.getPrincipals(User.class).isEmpty()) {
  return getLoginUser();
 } else {
  return new UserGroupInformation(subject);
 }
}

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

@Override
public void handle(Callback[] callbacks) throws UnsupportedCallbackException {
  Subject subject = Subject.getSubject(AccessController.getContext());
  for (Callback callback : callbacks) {
    if (callback instanceof NameCallback) {

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

/**
 * It first looks the shiro subject in AccessControlContext since JMX will use multiple threads to
 * process operations from the same client, then it looks into Shiro's thead context.
 *
 * @return the shiro subject, null if security is not enabled
 */
@Override
public Subject getSubject() {
 Subject currentUser;
 // First try get the principal out of AccessControlContext instead of Shiro's Thread context
 // since threads can be shared between JMX clients.
 javax.security.auth.Subject jmxSubject =
   javax.security.auth.Subject.getSubject(AccessController.getContext());
 if (jmxSubject != null) {
  Set<ShiroPrincipal> principals = jmxSubject.getPrincipals(ShiroPrincipal.class);
  if (!principals.isEmpty()) {
   ShiroPrincipal principal = principals.iterator().next();
   currentUser = principal.getSubject();
   ThreadContext.bind(currentUser);
   return currentUser;
  }
 }
 // in other cases like rest call, client operations, we get it from the current thread
 currentUser = SecurityUtils.getSubject();
 if (currentUser == null || currentUser.getPrincipal() == null) {
  throw new AuthenticationRequiredException("Failed to find the authenticated user.");
 }
 return currentUser;
}

代码示例来源:origin: org.postgresql/postgresql

boolean performAuthentication = jaasLogin;
GSSCredential gssCredential = null;
Subject sub = Subject.getSubject(AccessController.getContext());
if (sub != null) {
 Set<GSSCredential> gssCreds = sub.getPrivateCredentials(GSSCredential.class);

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

loggedInSubject = Subject.getSubject(context);
if (loggedInSubject == null) {
 throw new SQLException("The Subject is not set");

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

JMXAuditLogEntry entry = null;
if (audit != OFF) {
  Subject subject = Subject.getSubject(AccessController.getContext());
  String caller = "anonymous";
  if (subject != null) {

代码示例来源:origin: stackoverflow.com

try {
  Subject current = Subject.getSubject(AccessController.getContext());
  System.out.println("----------------------------------------");
  Set<Principal> principals = current.getPrincipals();

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

private Iterator<? extends Object> getJAASPrincipals() {
  Subject subject = Subject.getSubject(AccessController.getContext());
  return subject != null && subject.getPrincipals() != null
    ? subject.getPrincipals().iterator() : Collections.emptyIterator();
}

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

public static String getCurrentUserName() {
  AccessControlContext acc = AccessController.getContext();
  final Subject subject = Subject.getSubject(acc);
  if (subject != null && subject.getPrincipals(UserPrincipal.class).iterator().hasNext()) {
    return subject.getPrincipals(UserPrincipal.class).iterator().next().getName();
  } else {
    return null;
  }
}

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

public static String getCurrentUserName() {
  AccessControlContext acc = AccessController.getContext();
  final Subject subject = Subject.getSubject(acc);
  if (subject != null && subject.getPrincipals(UserPrincipal.class).iterator().hasNext()) {
    return subject.getPrincipals(UserPrincipal.class).iterator().next().getName();
  } else {
    return null;
  }
}

相关文章