org.apache.http.auth.AuthState类的使用及代码示例

x33g5p2x  于2022-01-15 转载在 其他  
字(13.9k)|赞(0)|评价(0)|浏览(270)

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

AuthState介绍

[英]This class provides detailed information about the state of the authentication process.
[中]此类提供有关身份验证过程状态的详细信息。

代码示例

代码示例来源:origin: yasserg/crawler4j

@Override
  public void process(HttpRequest request, HttpContext context) {
    AuthState authState = (AuthState) context.getAttribute(HttpClientContext.TARGET_AUTH_STATE);
    if (authState.getAuthScheme() == null) {
      CredentialsProvider credsProvider =
          (CredentialsProvider) context.getAttribute(HttpClientContext.CREDS_PROVIDER);
      HttpHost targetHost = (HttpHost) context.getAttribute(HttpCoreContext.HTTP_TARGET_HOST);
      Credentials credentials = credsProvider.getCredentials(
          new AuthScope(targetHost.getHostName(), targetHost.getPort()));
      if (credentials != null) {
        authState.update(new BasicScheme(), credentials);
      }
    }
  }
}

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

AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
if (authState.getAuthScheme() == null) {
  AuthScheme authScheme = (AuthScheme) context.getAttribute("preemptive-auth");
  CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER);
  HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
  if (authScheme != null) {
    Credentials creds = credsProvider.getCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()));
    if (creds == null) {
      throw new HttpException("No credentials for preemptive authentication");
    authState.setAuthScheme(authScheme);
    authState.setCredentials(creds);

代码示例来源:origin: code4craft/webmagic

private HttpClientContext convertHttpClientContext(Request request, Site site, Proxy proxy) {
  HttpClientContext httpContext = new HttpClientContext();
  if (proxy != null && proxy.getUsername() != null) {
    AuthState authState = new AuthState();
    authState.update(new BasicScheme(ChallengeState.PROXY), new UsernamePasswordCredentials(proxy.getUsername(), proxy.getPassword()));
    httpContext.setAttribute(HttpClientContext.PROXY_AUTH_STATE, authState);
  }
  if (request.getCookies() != null && !request.getCookies().isEmpty()) {
    CookieStore cookieStore = new BasicCookieStore();
    for (Map.Entry<String, String> cookieEntry : request.getCookies().entrySet()) {
      BasicClientCookie cookie1 = new BasicClientCookie(cookieEntry.getKey(), cookieEntry.getValue());
      cookie1.setDomain(UrlUtils.removePort(UrlUtils.getDomain(request.getUrl())));
      cookieStore.addCookie(cookie1);
    }
    httpContext.setCookieStore(cookieStore);
  }
  return httpContext;
}

代码示例来源:origin: robovm/robovm

private static Principal getAuthPrincipal(final AuthState authState) {
  AuthScheme scheme = authState.getAuthScheme();
  if (scheme != null && scheme.isComplete() && scheme.isConnectionBased()) {
    Credentials creds = authState.getCredentials();
    if (creds != null) {
      return creds.getUserPrincipal(); 
    }
  }
  return null;
}

代码示例来源:origin: robolectric/robolectric

private void processChallenges(
  final Map<String, Header> challenges,
  final AuthState authState,
  final AuthenticationHandler authHandler,
  final HttpResponse response,
  final HttpContext context)
   throws MalformedChallengeException, AuthenticationException {
 AuthScheme authScheme = authState.getAuthScheme();
 if (authScheme == null) {
  // Authentication not attempted before
  authScheme = authHandler.selectScheme(challenges, response, context);
  authState.setAuthScheme(authScheme);
 }
 String id = authScheme.getSchemeName();
 Header challenge = challenges.get(id.toLowerCase(Locale.ENGLISH));
 if (challenge == null) {
  throw new AuthenticationException(id +
   " authorization challenge expected, but not found");
 }
 authScheme.processChallenge(challenge);
 this.log.debug("Authorization challenge processed");
}

代码示例来源:origin: com.hynnet/httpclient

HttpHost target = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
if (target == null) {
  target = route.getTargetHost();
if (target.getPort() < 0) {
  final Scheme scheme = connManager.getSchemeRegistry().getScheme(target);
  target = new HttpHost(target.getHostName(), scheme.getDefaultPort(), target.getSchemeName());
    target, response, this.targetAuthStrategy, targetAuthState, context);
HttpHost proxy = route.getProxyHost();
  proxy = route.getTargetHost();
redirect.setHeaders(orig.getAllHeaders());
if (!route.getTargetHost().equals(newTarget)) {
  this.log.debug("Resetting target auth state");
  targetAuthState.reset();
  final AuthScheme authScheme = proxyAuthState.getAuthScheme();
  if (authScheme != null && authScheme.isConnectionBased()) {
    this.log.debug("Resetting proxy auth state");
    proxyAuthState.reset();

代码示例来源:origin: robolectric/robolectric

final CredentialsProvider credsProvider) {
if (!authState.isValid()) {
 return;
AuthScheme authScheme = authState.getAuthScheme();
AuthScope authScope = new AuthScope(
  hostname,
  port,
  authScheme.getRealm(),
  authScheme.getSchemeName());
Credentials creds = authState.getCredentials();
if (creds == null) {
 creds = credsProvider.getCredentials(authScope);
 if (this.log.isDebugEnabled()) {
  if (creds != null) {
 if (authScheme.isComplete()) {
  this.log.debug("Authentication failed");
  creds = null;
authState.setAuthScope(authScope);
authState.setCredentials(creds);

代码示例来源:origin: org.apache.jmeter/ApacheJMeter_http

CredentialsProvider credentialsProvider = localContext.getCredentialsProvider();
  if (key.hasProxy && !StringUtils.isEmpty(key.proxyUser)) {
    authScope = new AuthScope(key.proxyHost, key.proxyPort);
    credentials = credentialsProvider.getCredentials(authScope);
  credentialsProvider.clear();
  if (credentials != null) {
    credentialsProvider.setCredentials(authScope, credentials);
} else {
  try {
    requestURI = new URI(request.getRequestLine().getUri());
  } catch (final URISyntaxException ignore) { // NOSONAR
  HttpHost targetHost = (HttpHost) context.getAttribute(HttpCoreContext.HTTP_TARGET_HOST);
  URL url;
  if(requestURI.isAbsolute()) {
    url = requestURI.toURL();
  } else {
    url = new URL(targetHost.getSchemeName(), targetHost.getHostName(), targetHost.getPort(), 
        requestURI.getPath());
    AuthState authState = (AuthState) context.getAttribute(HttpClientContext.TARGET_AUTH_STATE);
    if (authState.getAuthScheme() == null) {
      AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort(),
          authorization.getRealm(), targetHost.getSchemeName());
      Credentials creds = credentialsProvider.getCredentials(authScope);

代码示例来源:origin: robovm/robovm

if (request.containsHeader(AUTH.PROXY_AUTH_RESP)) {
  return;
AuthState authState = (AuthState) context.getAttribute(
    ClientContext.PROXY_AUTH_STATE);
if (authState == null) {
AuthScheme authScheme = authState.getAuthScheme();
if (authScheme == null) {
  return;
Credentials creds = authState.getCredentials();
if (creds == null) {
  this.log.debug("User credentials not available");
  return;
if (authState.getAuthScope() != null || !authScheme.isConnectionBased()) {
  try {
    request.addHeader(authScheme.authenticate(creds, request));
  } catch (AuthenticationException ex) {
    if (this.log.isErrorEnabled()) {

代码示例来源:origin: robolectric/robolectric

HttpHost newTarget = new HttpHost(
  uri.getHost(),
  uri.getPort(),
targetAuthState.setAuthScope(null);
proxyAuthState.setAuthScope(null);
if (!route.getTargetHost().equals(newTarget)) {
 targetAuthState.invalidate();
 AuthScheme authScheme = proxyAuthState.getAuthScheme();
 if (authScheme != null && authScheme.isConnectionBased()) {
  proxyAuthState.invalidate();
redirect.setHeaders(orig.getAllHeaders());
context.getAttribute(ClientContext.CREDS_PROVIDER);
 if (this.targetAuthState.getCredentials() != null) {
 this.targetAuthState.setAuthScope(null);
 HttpHost proxy = route.getProxyHost();
 if (this.proxyAuthState.getCredentials() != null) {
 this.proxyAuthState.setAuthScope(null);

代码示例来源:origin: net.oauth.core/oauth-httpclient4

/**
   * If no auth scheme has been selected for the given context, consider each
   * of the preferred auth schemes and select the first one for which an
   * AuthScheme and matching Credentials are available.
   */
  public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
    AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
    if (authState != null && authState.getAuthScheme() != null) {
      return;
    }
    HttpHost target = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
    CredentialsProvider creds = (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER);
    AuthSchemeRegistry schemes = (AuthSchemeRegistry) context.getAttribute(ClientContext.AUTHSCHEME_REGISTRY);
    for (Object schemeName : (Iterable) context.getAttribute(ClientContext.AUTH_SCHEME_PREF)) {
      AuthScheme scheme = schemes.getAuthScheme(schemeName.toString(), request.getParams());
      if (scheme != null) {
        AuthScope targetScope = new AuthScope(target.getHostName(), target.getPort(), scheme.getRealm(), scheme
            .getSchemeName());
        Credentials cred = creds.getCredentials(targetScope);
        if (cred != null) {
          authState.setAuthScheme(scheme);
          authState.setCredentials(cred);
          return;
        }
      }
    }
  }
}

代码示例来源:origin: robovm/robovm

HttpHost proxy = route.getProxyHost();
RequestWrapper request = roureq.getRequest();
  HttpHost newTarget = new HttpHost(
      uri.getHost(), 
      uri.getPort(),
  redirect.setHeaders(orig.getAllHeaders());
  context.getAttribute(ClientContext.CREDS_PROVIDER);
      context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
    if (target == null) {
      target = route.getTargetHost();
    if (this.targetAuthState.getCredentials() != null) {
    this.targetAuthState.setAuthScope(null);
    if (this.proxyAuthState.getCredentials() != null) {
    this.proxyAuthState.setAuthScope(null);

代码示例来源:origin: robovm/robovm

throws HttpException, IOException {
HttpHost proxy = route.getProxyHost();
HttpHost target = route.getTargetHost();
HttpResponse response = null;
    connect.addHeader(HTTP.USER_AGENT, agent);
  connect.addHeader(HTTP.TARGET_HOST, target.toHostString());
  AuthScheme authScheme = this.proxyAuthState.getAuthScheme();
  AuthScope authScope = this.proxyAuthState.getAuthScope();
  Credentials creds = this.proxyAuthState.getCredentials();
  if (creds != null) {
    if (authScope != null || !authScheme.isConnectionBased()) {
      try {
        connect.addHeader(authScheme.authenticate(creds, connect));
      } catch (AuthenticationException ex) {
        if (this.log.isErrorEnabled()) {
    context.getAttribute(ClientContext.CREDS_PROVIDER);
      if (this.proxyAuthState.getCredentials() != null) {
        done = false;
      this.proxyAuthState.setAuthScope(null);

代码示例来源:origin: se.vgregion.pubsubhubbub/pubsubhubbub-hub-composite-twitter

public void process(HttpRequest request, HttpContext context) {
    AuthState authState = (AuthState) context.getAttribute(
        ClientContext.TARGET_AUTH_STATE);
    
    // If not auth scheme has been initialized yet
    if (authState.getAuthScheme() == null) {
      Credentials creds = new UsernamePasswordCredentials(username, password);
      authState.setAuthScheme(new BasicScheme());
      authState.setCredentials(creds);
    }
  }
}

代码示例来源:origin: ibinti/bugvm

final AuthState authState,
  final HttpContext context) throws HttpException, IOException {
AuthScheme authScheme = authState.getAuthScheme();
Credentials creds = authState.getCredentials();
switch (authState.getState()) { // TODO add UNCHALLENGED and HANDSHAKE cases
case FAILURE:
  return;
case SUCCESS:
  ensureAuthScheme(authScheme);
  if (authScheme.isConnectionBased()) {
    return;
  final Queue<AuthOption> authOptions = authState.getAuthOptions();
  if (authOptions != null) {
    while (!authOptions.isEmpty()) {
      authScheme = authOption.getAuthScheme();
      creds = authOption.getCredentials();
      authState.update(authScheme, creds);
      if (this.log.isDebugEnabled()) {
        this.log.debug("Generating response to an authentication challenge using "
            + authScheme.getSchemeName() + " scheme");
        request.addHeader(header);
        break;
      } catch (final AuthenticationException ex) {

代码示例来源:origin: robolectric/robolectric

throws HttpException, IOException {
HttpHost proxy = route.getProxyHost();
HttpHost target = route.getTargetHost();
HttpResponse response = null;
 connect.setParams(this.params);
 context.setAttribute(ExecutionContext.HTTP_TARGET_HOST,
   target);
 context.setAttribute(ExecutionContext.HTTP_PROXY_HOST,
   proxy);
 context.setAttribute(ExecutionContext.HTTP_CONNECTION,
   managedConn);
 context.setAttribute(ClientContext.TARGET_AUTH_STATE,
   if (this.proxyAuthState.getCredentials() != null) {
    done = false;
   this.proxyAuthState.setAuthScope(null);

代码示例来源:origin: soundcloud/java-api-wrapper

@Override public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
    if (request == null) throw new IllegalArgumentException("HTTP request may not be null");
    if (context == null) throw new IllegalArgumentException("HTTP context may not be null");

    if (!request.getRequestLine().getMethod().equalsIgnoreCase("CONNECT")) {
      AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
      if (authState != null) {
        AuthScheme authScheme = authState.getAuthScheme();
        if (authScheme != null && !authScheme.isConnectionBased()) {
          try {
            request.setHeader(authScheme.authenticate(null, request));
          } catch (AuthenticationException ignored) {
            // ignored
          }
        }
      }
    }
  }
}

代码示例来源:origin: at.bestsolution.efxclipse.eclipse/org.apache.httpcomponents.httpclient

if (!redirect.headerIterator().hasNext()) {
  final HttpRequest original = request.getOriginal();
  redirect.setHeaders(original.getAllHeaders());
if (!currentRoute.getTargetHost().equals(newTarget)) {
  final AuthState targetAuthState = context.getTargetAuthState();
  if (targetAuthState != null) {
    this.log.debug("Resetting target auth state");
    targetAuthState.reset();
    final AuthScheme authScheme = proxyAuthState.getAuthScheme();
    if (authScheme != null && authScheme.isConnectionBased()) {
      this.log.debug("Resetting proxy auth state");
      proxyAuthState.reset();

代码示例来源:origin: jamesagnew/hapi-fhir

@Override
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
  AuthState authState = (AuthState) context.getAttribute(HttpClientContext.TARGET_AUTH_STATE);
  if (authState.getAuthScheme() == null) {
    Credentials creds = new UsernamePasswordCredentials(myUsername, myPassword);
    authState.update(new BasicScheme(), creds);
  }
}

代码示例来源:origin: org.apache.solr/solr-solrj

@Override
 public void process(final HttpRequest request, final HttpContext context) throws HttpException,
   IOException {

  AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
  // If no auth scheme available yet, try to initialize it preemptively
  if (authState.getAuthScheme() == null) {
   CredentialsProvider credsProvider = (CredentialsProvider) context
     .getAttribute(ClientContext.CREDS_PROVIDER);
   Credentials creds = credsProvider.getCredentials(AuthScope.ANY);
   authState.update(authScheme, creds);
  }
 }
}

相关文章