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

x33g5p2x  于2022-01-16 转载在 其他  
字(15.0k)|赞(0)|评价(0)|浏览(234)

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

AuthScheme介绍

[英]This interface represents an abstract challenge-response oriented authentication scheme.

An authentication scheme should be able to support the following functions:

  • Parse and process the challenge sent by the targer server in response to request for a protected resource
  • Provide its textual designation
  • Provide its parameters, if available
  • Provide the realm this authentication scheme is applicable to, if available
  • Generate authorization string for the given set of credentials, request method and URI as specificed in the HTTP request line in response to the actual authorization challenge

Authentication schemes may ignore method name and URI parameters if they are not relevant for the given authentication mechanism

Authentication schemes may be stateful involving a series of challenge-response exchanges
[中]此接口表示一个抽象的面向质询-响应的身份验证方案。
身份验证方案应能够支持以下功能:
*解析并处理Targetr服务器响应受保护资源请求而发送的质询
*提供其文本名称
*提供其参数(如果可用)
*提供此身份验证方案适用的领域(如果可用)
*为HTTP请求行中指定的给定凭据集、请求方法和URI生成授权字符串,以响应实际的授权质询
如果方法名和URI参数与给定的身份验证机制不相关,则身份验证方案可能会忽略它们
身份验证方案可能是有状态的,涉及一系列质询-响应交换

代码示例

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

final CredentialsProvider credsProvider) {
if (!authState.isValid()) {
 return;
String hostname = host.getHostName();
int port = host.getPort();
if (port < 0) {
 Scheme scheme = connManager.getSchemeRegistry().getScheme(host);
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 (authScheme.isComplete()) {
  this.log.debug("Authentication failed");
  creds = null;

代码示例来源: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()) {
      this.log.error("Proxy authentication error: " + ex.getMessage());

代码示例来源: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: 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: 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

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);
  context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
 if (target == null) {
  target = route.getTargetHost();
 } catch (AuthenticationException ex) {
  if (this.log.isWarnEnabled()) {
   this.log.warn("Authentication error: " +  ex.getMessage());
   return null;
 HttpHost proxy = route.getProxyHost();

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

final HttpResponse response,
  final HttpContext context) throws MalformedChallengeException {
Args.notNull(challenges, "Map of auth challenges");
Args.notNull(authhost, "Host");
Args.notNull(response, "HTTP response");
Args.notNull(context, "HTTP context");
final CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(
    ClientContext.CREDS_PROVIDER);
if (credsProvider == null) {
} catch (final AuthenticationException ex) {
  if (this.log.isWarnEnabled()) {
    this.log.warn(ex.getMessage(), ex);
final String id = authScheme.getSchemeName();
final Header challenge = challenges.get(id.toLowerCase(Locale.ROOT));
authScheme.processChallenge(challenge);
    authhost.getHostName(),
    authhost.getPort(),
    authScheme.getRealm(),
    authScheme.getSchemeName());

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

final HttpResponse response,
  final HttpContext context) throws MalformedChallengeException {
Args.notNull(challenges, "Map of auth challenges");
Args.notNull(authhost, "Host");
Args.notNull(response, "HTTP response");
Args.notNull(context, "HTTP context");
final HttpClientContext clientContext = HttpClientContext.adapt(context);
final Lookup<AuthSchemeProvider> registry = clientContext.getAuthSchemeRegistry();
if (registry == null) {
  this.log.debug("Auth scheme registry not set in the context");
  return options;
final CredentialsProvider credsProvider = clientContext.getCredentialsProvider();
if (credsProvider == null) {
  this.log.debug("Credentials provider not set in the context");
    authScheme.processChallenge(challenge);
        authhost.getHostName(),
        authhost.getPort(),
        authScheme.getRealm(),
        authScheme.getSchemeName());

代码示例来源: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()) {
          this.log.error("Proxy authentication error: " + ex.getMessage());
    context.getAttribute(ClientContext.CREDS_PROVIDER);
      } catch (AuthenticationException ex) {
        if (this.log.isWarnEnabled()) {
          this.log.warn("Authentication error: " +  ex.getMessage());
          break;

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

final HttpClientContext context,
  final HttpExecutionAware execAware) throws IOException, HttpException {
Args.notNull(route, "HTTP route");
Args.notNull(request, "HTTP request");
Args.notNull(context, "HTTP context");
final List<URI> redirectLocations = context.getRedirectLocations();
if (redirectLocations != null) {
  redirectLocations.clear();
final RequestConfig config = context.getRequestConfig();
final int maxRedirects = config.getMaxRedirects() > 0 ? config.getMaxRedirects() : 50;
HttpRoute currentRoute = route;
      if (!redirect.headerIterator().hasNext()) {
        final HttpRequest original = request.getOriginal();
        redirect.setHeaders(original.getAllHeaders());
        final AuthState proxyAuthState = context.getProxyAuthState();
        if (proxyAuthState != null) {
          final AuthScheme authScheme = proxyAuthState.getAuthScheme();
          if (authScheme != null && authScheme.isConnectionBased()) {
            this.log.debug("Resetting proxy auth state");
            proxyAuthState.reset();

代码示例来源: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: at.bestsolution.efxclipse.eclipse/org.apache.httpcomponents.httpclient

public void authSucceeded(
    final HttpHost authhost, final AuthScheme authScheme, final HttpContext context) {
  AuthCache authCache = (AuthCache) context.getAttribute(ClientContext.AUTH_CACHE);
  if (isCachable(authScheme)) {
    if (authCache == null) {
      authCache = new BasicAuthCache();
      context.setAttribute(ClientContext.AUTH_CACHE, authCache);
    }
    if (this.log.isDebugEnabled()) {
      this.log.debug("Caching '" + authScheme.getSchemeName() +
          "' auth scheme for " + authhost);
    }
    authCache.put(authhost, authScheme);
  }
}

代码示例来源:origin: org.apache.httpcomponents/httpclient-android

public void authSucceeded(
    final HttpHost authhost, final AuthScheme authScheme, final HttpContext context) {
  Args.notNull(authhost, "Host");
  Args.notNull(authScheme, "Auth scheme");
  Args.notNull(context, "HTTP context");
  final HttpClientContext clientContext = HttpClientContext.adapt(context);
  if (isCachable(authScheme)) {
    AuthCache authCache = clientContext.getAuthCache();
    if (authCache == null) {
      authCache = new BasicAuthCache();
      clientContext.setAuthCache(authCache);
    }
    if (Log.isLoggable(TAG, Log.DEBUG)) {
      Log.d(TAG, "Caching '" + authScheme.getSchemeName() +
          "' auth scheme for " + authhost);
    }
    authCache.put(authhost, authScheme);
  }
}

代码示例来源: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: com.hynnet/httpclient

public void authFailed(
    final HttpHost authhost, final AuthScheme authScheme, final HttpContext context) {
  final AuthCache authCache = (AuthCache) context.getAttribute(ClientContext.AUTH_CACHE);
  if (authCache == null) {
    return;
  }
  if (this.log.isDebugEnabled()) {
    this.log.debug("Removing from cache '" + authScheme.getSchemeName() +
        "' auth scheme for " + authhost);
  }
  authCache.remove(authhost);
}

代码示例来源:origin: at.bestsolution.efxclipse.eclipse/org.eclipse.ecf.provider.filetransfer.httpclient4

private static boolean isProxyType(HttpContext context, String scheme) {
  if (context == null)
    return false;
  AuthState authState = (AuthState) context.getAttribute(ClientContext.PROXY_AUTH_STATE);
  if (authState == null)
    return false;
  AuthScheme authScheme = authState.getAuthScheme();
  if (authScheme == null)
    return false;
  String schemeName = authScheme.getSchemeName();
  if (schemeName == null)
    return false;
  return schemeName.equalsIgnoreCase(scheme);
}

代码示例来源:origin: org.apache.httpcomponents/httpclient-android

private void doPreemptiveAuth(
    final HttpHost host,
    final AuthScheme authScheme,
    final AuthStateHC4 authState,
    final CredentialsProvider credsProvider) {
  final String schemeName = authScheme.getSchemeName();
  if (Log.isLoggable(TAG, Log.DEBUG)) {
    Log.d(TAG, "Re-using cached '" + schemeName + "' auth scheme for " + host);
  }
  final AuthScope authScope = new AuthScope(host.getHostName(), host.getPort(), AuthScope.ANY_REALM, schemeName);
  final Credentials creds = credsProvider.getCredentials(authScope);
  if (creds != null) {
    if ("BASIC".equalsIgnoreCase(authScheme.getSchemeName())) {
      authState.setState(AuthProtocolState.CHALLENGED);
    } else {
      authState.setState(AuthProtocolState.SUCCESS);
    }
    authState.update(authScheme, creds);
  } else {
    if (Log.isLoggable(TAG, Log.DEBUG)) {
      Log.d(TAG, "No credentials for preemptive authentication");
    }
  }
}

代码示例来源: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;
      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) {
        if (this.log.isWarnEnabled()) {
          this.log.warn(authScheme + " authentication error: " + ex.getMessage());
  try {
    final Header header = doAuth(authScheme, creds, request, context);
    request.addHeader(header);
  } catch (final AuthenticationException ex) {
    if (this.log.isErrorEnabled()) {
      this.log.error(authScheme + " authentication error: " + ex.getMessage());

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

private boolean isCachable(final AuthState authState) {
  final AuthScheme authScheme = authState.getAuthScheme();
  if (authScheme == null || !authScheme.isComplete()) {
    return false;
  }
  final String schemeName = authScheme.getSchemeName();
  return schemeName.equalsIgnoreCase(AuthPolicy.BASIC) ||
      schemeName.equalsIgnoreCase(AuthPolicy.DIGEST);
}

代码示例来源:origin: com.impetus.fabric/fabric-jdbc-driver-shaded

protected boolean isCachable(final AuthScheme authScheme) {
  if (authScheme == null || !authScheme.isComplete()) {
    return false;
  }
  final String schemeName = authScheme.getSchemeName();
  return schemeName.equalsIgnoreCase(AuthSchemes.BASIC);
}

相关文章