org.apache.http.auth.AuthState.setCredentials()方法的使用及代码示例

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

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

AuthState.setCredentials介绍

[英]Sets user Credentials to be used for authentication
[中]设置用于身份验证的用户凭据

代码示例

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

authState.setCredentials(creds);

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

authState.setCredentials(creds);

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

authState.setCredentials(creds);

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

httpClient.addRequestInterceptor(new PreemptiveAuthInterceptor(), 0);

(...)

static class PreemptiveAuthInterceptor implements HttpRequestInterceptor {

  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 avaialble yet, try to initialize it
    // preemptively
    if (authState.getAuthScheme() == null) {
      CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER);
      HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
      Credentials creds = credsProvider.getCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()));
      if (creds == null)
        throw new HttpException("No credentials for preemptive authentication");
      authState.setAuthScheme(new BasicScheme());
      authState.setCredentials(creds);
    }

  }

}

代码示例来源:origin: rnewson/couchdb-lucene

public void process(final HttpRequest request, final HttpContext context)
      throws HttpException, IOException {
    final AuthState authState = (AuthState) context
        .getAttribute(ClientContext.TARGET_AUTH_STATE);
    final CredentialsProvider credsProvider = (CredentialsProvider) context
        .getAttribute(ClientContext.CREDS_PROVIDER);
    final HttpHost targetHost = (HttpHost) context
        .getAttribute(ExecutionContext.HTTP_TARGET_HOST);
    // If not auth scheme has been initialized yet
    if (authState.getAuthScheme() == null) {
      AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
      // Obtain credentials matching the target host
      Credentials creds = credsProvider.getCredentials(authScope);
      // If found, generate BasicScheme preemptively
      if (creds != null) {
        authState.setAuthScheme(new BasicScheme());
        authState.setCredentials(creds);
      }
    }
  }
}

代码示例来源:origin: net.sf.ahtutils/ahtutils-jsf

@Override
  public void process(org.apache.http.HttpRequest request, HttpContext context) throws HttpException, IOException
  {
    AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
    authState.setAuthScope(org.apache.http.auth.AuthScope.ANY);
    authState.setCredentials(new UsernamePasswordCredentials(username,password));
    authState.setAuthScheme(new BasicScheme());
    
  }
}

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

if (authState.getAuthScheme() == null) {
    AuthScope authScope = new Au    HttpRequestInterceptor preemptiveAuth = new HttpRequestInterceptor() {
  public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
    CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(
        ClientContext.CREDS_PROVIDER);
    HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);thScope(targetHost.getHostName(), targetHost.getPort());
    Credentials creds = credsProvider.getCredentials(authScope);
    if (creds != null) {
      authState.setAuthScheme(new BasicScheme());
      authState.setCredentials(creds);
    }
  }
}    
};
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.addRequestInterceptor(preemptiveAuth, 0);

代码示例来源:origin: yiwent/Mobike

public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
  AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
  CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(
      ClientContext.CREDS_PROVIDER);
  HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
  if (authState.getAuthScheme() == null) {
    AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
    Credentials creds = credsProvider.getCredentials(authScope);
    if (creds != null) {
      authState.setAuthScheme(new BasicScheme());
      authState.setCredentials(creds);
    }
  }
}

代码示例来源:origin: yiwent/Mobike

@Override
  public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
    CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(
        ClientContext.CREDS_PROVIDER);
    HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
    if (authState.getAuthScheme() == null) {
      AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
      Credentials creds = credsProvider.getCredentials(authScope);
      if (creds != null) {
        authState.setAuthScheme(new BasicScheme());
        authState.setCredentials(creds);
      }
    }
  }
}, 0);

代码示例来源:origin: lightcouch/LightCouch

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 avaialble yet, try to initialize it preemptively
  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) {
      authState.setAuthScheme(authScheme);
      Credentials creds = credsProvider.getCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()));
      if (creds != null) {
        authState.setCredentials(creds);
      }
    }
  }
}

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

HttpRequestInterceptor httpRequestInterceptor = new HttpRequestInterceptor() {
  public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
    CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(
        ClientContext.CREDS_PROVIDER);
    HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);

    if (authState.getAuthScheme() == null) {
      AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
      Credentials creds = credsProvider.getCredentials(authScope);
      if (creds != null) {
        authState.setAuthScheme(new BasicScheme());
        authState.setCredentials(creds);
      }
    }
  }    
};

代码示例来源:origin: net.java.dev.jets3t/jets3t

public void process(final HttpRequest request, final HttpContext context) {
    AuthState authState = (AuthState) context.getAttribute(
        ClientContext.TARGET_AUTH_STATE);
    CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(
        ClientContext.CREDS_PROVIDER);
    HttpHost targetHost = (HttpHost) context.getAttribute(
        ExecutionContext.HTTP_TARGET_HOST);
    // If not auth scheme has been initialized yet
    if (authState.getAuthScheme() == null) {
      AuthScope authScope = new AuthScope(targetHost.getHostName(),
          targetHost.getPort());
      // Obtain credentials matching the target host
      Credentials creds = credsProvider.getCredentials(authScope);
      // If found, generate BasicScheme preemptively
      if (creds != null) {
        authState.setAuthScheme(new BasicScheme());
        authState.setCredentials(creds);
      }
    }
  }
} //PreemptiveInterceptor

代码示例来源:origin: org.jfrog/build-info-client

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) {
      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: org.jfrog.buildinfo/build-info-client

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) {
      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: com.heroku.api/heroku-http-apache

@Override
  public void process(HttpRequest request, 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) {
      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: org.hornetq.rest/hornetq-rest

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)
   {
    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: com.ibm.sbt/com.ibm.sbt.core

@Override
  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) {
      AuthScheme scheme = new BasicSchemeFactory().newInstance(new BasicHttpParams());
      authState.setAuthScheme(scheme);
      authState.setCredentials(credentials);
    }
  }
}

代码示例来源:origin: com.ibm.sbt/com.ibm.sbt.core

@Override
  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) {
      AuthScheme scheme = new BasicSchemeFactory().newInstance(new BasicHttpParams());
      authState.setAuthScheme(scheme);
      authState.setCredentials(credentials);
    }
  }
}

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

httclient.addRequestInterceptor(new HttpRequestInterceptor() {
  public void process(HttpRequest arg0, HttpContext context) throws HttpException, IOException {
    AuthState state = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
    if (state.getAuthScheme() == null) {
      BasicScheme scheme = new BasicScheme();
      CredentialsProvider credentialsProvider = (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER);
      Credentials credentials = credentialsProvider.getCredentials(AuthScope.ANY);
      if (credentials == null) {
        System.out.println("Credential >>" + credentials);
        throw new HttpException();
      }
      state.setAuthScope(AuthScope.ANY);
      state.setAuthScheme(scheme);
      state.setCredentials(credentials);
    }
  }
}, 0);

相关文章