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

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

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

ThreadContext.put介绍

[英]Binds value for the given key to the current thread.

A null value has the same effect as if remove was called for the given key, i.e.:

if ( value == null ) { 
remove( key ); 
}

[中]将给定key的值绑定到当前线程。
空值的效果与为给定密钥调用remove的效果相同,即:

if ( value == null ) { 
remove( key ); 
}

代码示例

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

public static void requestSessionCreation(boolean createSessionRequest) {
    ThreadContext.put(AUTO_CREATE_SESSION_KEY, createSessionRequest);
  }
}

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

/**
 * Convenience method that simplifies binding a Subject to the ThreadContext.
 * <p/>
 * <p>The method's existence is to help reduce casting in your own code and to simplify remembering of
 * ThreadContext key names.  The implementation is simple in that, if the Subject is not <tt>null</tt>,
 * it binds it to the thread, i.e.:
 * <p/>
 * <pre>
 * if (subject != null) {
 *     put( SUBJECT_KEY, subject );
 * }</pre>
 *
 * @param subject the Subject object to bind to the thread.  If the argument is null, nothing will be done.
 * @since 0.2
 */
public static void bind(Subject subject) {
  if (subject != null) {
    put(SUBJECT_KEY, subject);
  }
}

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

/**
 * Convenience method that simplifies binding the application's SecurityManager instance to the ThreadContext.
 * <p/>
 * <p>The method's existence is to help reduce casting in code and to simplify remembering of
 * ThreadContext key names.  The implementation is simple in that, if the SecurityManager is not <tt>null</tt>,
 * it binds it to the thread, i.e.:
 * <p/>
 * <pre>
 * if (securityManager != null) {
 *     put( SECURITY_MANAGER_KEY, securityManager);
 * }</pre>
 *
 * @param securityManager the application's SecurityManager instance to bind to the thread.  If the argument is
 *                        null, nothing will be done.
 * @since 0.9
 */
public static void bind(SecurityManager securityManager) {
  if (securityManager != null) {
    put(SECURITY_MANAGER_KEY, securityManager);
  }
}

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

ThreadContext.put(REQUEST_HEADERS, headers);

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

/**
 * Convenience method that simplifies binding a Subject to the ThreadContext.
 * <p/>
 * <p>The method's existence is to help reduce casting in your own code and to simplify remembering of
 * ThreadContext key names.  The implementation is simple in that, if the Subject is not <tt>null</tt>,
 * it binds it to the thread, i.e.:
 * <p/>
 * <pre>
 * if (subject != null) {
 *     put( SUBJECT_KEY, subject );
 * }</pre>
 *
 * @param subject the Subject object to bind to the thread.  If the argument is null, nothing will be done.
 * @since 0.2
 */
public static void bind(Subject subject) {
  if (subject != null) {
    put(SUBJECT_KEY, subject);
  }
}

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

/**
 * Convenience method that simplifies binding the application's SecurityManager instance to the ThreadContext.
 * <p/>
 * <p>The method's existence is to help reduce casting in code and to simplify remembering of
 * ThreadContext key names.  The implementation is simple in that, if the SecurityManager is not <tt>null</tt>,
 * it binds it to the thread, i.e.:
 * <p/>
 * <pre>
 * if (securityManager != null) {
 *     put( SECURITY_MANAGER_KEY, securityManager);
 * }</pre>
 *
 * @param securityManager the application's SecurityManager instance to bind to the thread.  If the argument is
 *                        null, nothing will be done.
 * @since 0.9
 */
public static void bind(SecurityManager securityManager) {
  if (securityManager != null) {
    put(SECURITY_MANAGER_KEY, securityManager);
  }
}

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

public static void requestSessionCreation(boolean createSessionRequest) {
    ThreadContext.put(AUTO_CREATE_SESSION_KEY, createSessionRequest);
  }
}

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

/**
 * Convenience method that simplifies binding the application's SecurityManager instance to the ThreadContext.
 * <p/>
 * <p>The method's existence is to help reduce casting in code and to simplify remembering of
 * ThreadContext key names.  The implementation is simple in that, if the SecurityManager is not <tt>null</tt>,
 * it binds it to the thread, i.e.:
 * <p/>
 * <pre>
 * if (securityManager != null) {
 *     put( SECURITY_MANAGER_KEY, securityManager);
 * }</pre>
 *
 * @param securityManager the application's SecurityManager instance to bind to the thread.  If the argument is
 *                        null, nothing will be done.
 * @since 0.9
 */
public static void bind(SecurityManager securityManager) {
  if (securityManager != null) {
    put(SECURITY_MANAGER_KEY, securityManager);
  }
}

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

/**
 * Convenience method that simplifies binding a Subject to the ThreadContext.
 * <p/>
 * <p>The method's existence is to help reduce casting in your own code and to simplify remembering of
 * ThreadContext key names.  The implementation is simple in that, if the Subject is not <tt>null</tt>,
 * it binds it to the thread, i.e.:
 * <p/>
 * <pre>
 * if (subject != null) {
 *     put( SUBJECT_KEY, subject );
 * }</pre>
 *
 * @param subject the Subject object to bind to the thread.  If the argument is null, nothing will be done.
 * @since 0.2
 */
public static void bind(Subject subject) {
  if (subject != null) {
    put(SUBJECT_KEY, subject);
  }
}

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

@Override
public void onSuccess(AuthenticationToken token, AuthenticationInfo info) {
  if (!(token instanceof ProcessAuthenticationToken) && !(token instanceof SystemAccountAuthenticationToken)) {
    LogonEvent event = new LogonEvent(token, info);
    ThreadContext.put(IN_AUTHENTICATION_EVENT_FLAG, new InAuthenticationEvent());
    try {
      logonEvent.fire(event);
    } finally {
      // In any case (also in case of access denied) we need to remove this flag
      ThreadContext.remove(IN_AUTHENTICATION_EVENT_FLAG);
    }
  }
}

代码示例来源: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: be.c4j.ee.security.octopus/octopus-core

@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
  ThreadContext.put(IN_AUTHORIZATION_FLAG, new InAuthorization());
  AuthorizationInfo authorizationInfo = null;
  try {
    Iterator<OctopusDefinedAuthorizationInfo> iterator = octopusDefinedAuthorizationInfoList.iterator();
    Object primaryPrincipal = principals.getPrimaryPrincipal();
    while (authorizationInfo == null && iterator.hasNext()) {
      authorizationInfo = iterator.next().getAuthorizationInfo(primaryPrincipal);
    }
    if (authorizationInfo == null) {
      authorizationInfo = securityDataProvider.getAuthorizationInfo(principals);
    }
  } finally {
    ThreadContext.remove(IN_AUTHORIZATION_FLAG);
  }
  return authorizationInfo;
}

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

ThreadContext.put(REQUEST_HEADERS, headers);

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

@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
  AuthenticationInfo authenticationInfo = null;
  // TODO What about IncorrectDataToken, should be return null immediatly??
  // How is IncorrectDataToken used?
  Iterator<OctopusDefinedAuthenticationInfo> iterator = octopusDefinedAuthenticationInfoList.iterator();
  while (authenticationInfo == null && iterator.hasNext()) {
    authenticationInfo = iterator.next().getAuthenticationInfo(token);
  }
  if (authenticationInfo == null && !(token instanceof IncorrectDataToken)) {
    ThreadContext.put(IN_AUTHENTICATION_FLAG, new InAuthentication());
    try {
      authenticationInfo = securityDataProvider.getAuthenticationInfo(token);
      verifyHashEncoding(authenticationInfo);
    } finally {
      // Even in the case of an exception (access not allowed) we need to reset this flag
      ThreadContext.remove(IN_AUTHENTICATION_FLAG);
    }
  }
  if (authenticationInfo != null && authenticationInfo.getPrincipals() != null) {
    Object principal = authenticationInfo.getPrincipals().getPrimaryPrincipal();
    if (principal instanceof UserPrincipal) {
      UserPrincipal user = (UserPrincipal) principal;
      user.addUserInfo(OctopusConstants.TOKEN, token);
    }
  }
  return authenticationInfo;
}

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

@Override
protected AuthenticationInfo doSingleRealmAuthentication(Realm realm, AuthenticationToken token) {
  AuthenticationInfo authenticationInfo = super.doSingleRealmAuthentication(realm, token);
  // At this point the user is authenticated, otherwise there was already an exception thrown.
  if (authorizationInfoRequired && realm instanceof OctopusRealm) {
    ThreadContext.put(IN_AUTHORIZATION_FLAG, new InAuthorization());
    try {
      OctopusRealm octopusRealm = (OctopusRealm) realm;
      AuthorizationInfo authorizationInfo = octopusRealm.doGetAuthorizationInfo(authenticationInfo.getPrincipals());
      UserPrincipal userPrincipal = authenticationInfo.getPrincipals().oneByType(UserPrincipal.class);
      if (userPrincipal != null) {
        userPrincipal.addUserInfo(AUTHORIZATION_INFO, authorizationInfo);
        // authorizationInfoRequired -> When PrincipalAuthorizationInfoAvailibility implementing bean found
        // By default only when jwt-scs-client is added to the project
        // Can also be used (a possibility need to investigate another options) for Octopus SSO client to make sure that with every logon
        // of the user, the latest permissions are retrieved from the octopus SSO server.
        // TODO Document this
        octopusRealm.setAuthorizationCachedData(userPrincipal, authorizationInfo);
      }
    } finally {
      ThreadContext.remove(IN_AUTHORIZATION_FLAG);
    }
  }
  return authenticationInfo;
}

相关文章