本文整理汇总了Java中org.apache.http.auth.Credentials类的典型用法代码示例。如果您正苦于以下问题:Java Credentials类的具体用法?Java Credentials怎么用?Java Credentials使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Credentials类属于org.apache.http.auth包,在下文中一共展示了Credentials类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: doPreemptiveAuth
import org.apache.http.auth.Credentials; //导入依赖的package包/类
private void doPreemptiveAuth(
final HttpHost host,
final AuthScheme authScheme,
final AuthState authState,
final CredentialsProvider credsProvider) {
String schemeName = authScheme.getSchemeName();
if (this.log.isDebugEnabled()) {
this.log.debug("Re-using cached '" + schemeName + "' auth scheme for " + host);
}
AuthScope authScope = new AuthScope(host, AuthScope.ANY_REALM, schemeName);
Credentials creds = credsProvider.getCredentials(authScope);
if (creds != null) {
authState.setState(AuthProtocolState.SUCCESS);
authState.update(authScheme, creds);
} else {
this.log.debug("No credentials for preemptive authentication");
}
}
示例2: authenticate
import org.apache.http.auth.Credentials; //导入依赖的package包/类
/**
* Produces basic authorization header for the given set of {@link Credentials}.
*
* @param credentials The set of credentials to be used for authentication
* @param request The request being authenticated
* @throws InvalidCredentialsException if authentication credentials are not
* valid or not applicable for this authentication scheme
* @throws AuthenticationException if authorization string cannot
* be generated due to an authentication failure
*
* @return a basic authorization string
*/
@Override
public Header authenticate(
final Credentials credentials,
final HttpRequest request,
final HttpContext context) throws AuthenticationException {
if (credentials == null) {
throw new IllegalArgumentException("Credentials may not be null");
}
if (request == null) {
throw new IllegalArgumentException("HTTP request may not be null");
}
String charset = AuthParams.getCredentialCharset(request.getParams());
return authenticate(credentials, charset, isProxy());
}
示例3: matchCredentials
import org.apache.http.auth.Credentials; //导入依赖的package包/类
/**
* Find matching {@link Credentials credentials} for the given authentication scope.
*
* @param map the credentials hash map
* @param authscope the {@link AuthScope authentication scope}
* @return the credentials
*
*/
private static Credentials matchCredentials(
final Map<AuthScope, Credentials> map,
final AuthScope authscope) {
// see if we get a direct hit
Credentials creds = map.get(authscope);
if (creds == null) {
// Nope.
// Do a full scan
int bestMatchFactor = -1;
AuthScope bestMatch = null;
for (AuthScope current: map.keySet()) {
int factor = authscope.match(current);
if (factor > bestMatchFactor) {
bestMatchFactor = factor;
bestMatch = current;
}
}
if (bestMatch != null) {
creds = map.get(bestMatch);
}
}
return creds;
}
示例4: setProxy
import org.apache.http.auth.Credentials; //导入依赖的package包/类
public void setProxy(String proxyHost, Integer proxyPort, Credentials credentials) {
this.proxyHost = proxyHost;
this.proxyPort = proxyPort;
if (this.proxyHost.length() > 0 && !this.proxyPort.equals(0)) {
HttpClientBuilder clientBuilder = HttpClients.custom()
.useSystemProperties()
.setProxy(new HttpHost(proxyHost, proxyPort, "http"));
if (credentials != null) {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(proxyHost, proxyPort), credentials);
clientBuilder.setDefaultCredentialsProvider(credsProvider);
Loggers.SERVER.debug("MsTeamsNotification ::using proxy credentials " + credentials.getUserPrincipal().getName());
}
this.client = clientBuilder.build();
}
}
示例5: process
import org.apache.http.auth.Credentials; //导入依赖的package包/类
public void process(HttpRequest request, HttpContext context) throws HttpException,
IOException {
AuthState authState = (AuthState) context.getAttribute("http.auth.target-scope");
CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute("http.auth" +
".credentials-provider");
HttpHost targetHost = (HttpHost) context.getAttribute("http.target_host");
if (authState.getAuthScheme() == null) {
Credentials creds = credsProvider.getCredentials(new AuthScope(targetHost.getHostName
(), targetHost.getPort()));
if (creds != null) {
authState.setAuthScheme(new BasicScheme());
authState.setCredentials(creds);
}
}
}
示例6: getHttpClient
import org.apache.http.auth.Credentials; //导入依赖的package包/类
private SystemDefaultHttpClient getHttpClient() {
final SystemDefaultHttpClient httpClient = new SystemDefaultHttpClient();
httpClient.getAuthSchemes().register(AuthPolicy.SPNEGO, new SPNegoSchemeFactory(true));
Credentials use_jaas_creds = new Credentials() {
public String getPassword() {
return null;
}
public Principal getUserPrincipal() {
return null;
}
};
httpClient.getCredentialsProvider().setCredentials(
AuthScope.ANY, use_jaas_creds);
return httpClient;
}
示例7: build
import org.apache.http.auth.Credentials; //导入依赖的package包/类
public static HttpClient build(byte[] token) {
HttpClientBuilder builder = HttpClientBuilder.create();
Lookup<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider>create()
.register(AuthSchemes.SPNEGO, new CollaredSPNegoSchemeFactory(token)).build();
builder.setDefaultAuthSchemeRegistry(authSchemeRegistry);
BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(new AuthScope(null, -1, null), new Credentials() {
@Override
public Principal getUserPrincipal() {
return null;
}
@Override
public String getPassword() {
return null;
}
});
builder.setDefaultCredentialsProvider(credentialsProvider);
return builder.build();
}
示例8: buildClientWithCredentials
import org.apache.http.auth.Credentials; //导入依赖的package包/类
@Test
public void buildClientWithCredentials() throws Exception {
final Config config = ConfigFactory.parseString("{ url = \"http://example.com\", output.path = /home/user/downloads, saveAs = \"%1$s.pdf\", " +
"credentials { username = theUser, password = thePassword } }");
CloseableHttpClient client = FileDownload.buildClient(config);
Field field = client.getClass().getDeclaredField("credentialsProvider");
field.setAccessible(true);
Object provider = field.get(client);
assertThat(provider).isInstanceOf(BasicCredentialsProvider.class);
BasicCredentialsProvider basicProvider = (BasicCredentialsProvider) provider;
Credentials credentials = basicProvider.getCredentials(new AuthScope(null, -1));
assertThat(credentials.getUserPrincipal().getName()).isEqualTo("theUser");
assertThat(credentials.getPassword()).isEqualTo("thePassword");
}
示例9: getResponseAsString
import org.apache.http.auth.Credentials; //导入依赖的package包/类
public static String getResponseAsString(String url) throws IOException {
String result = null;
try (CloseableHttpResponse yearResponse = NetUtils.openConnection(url, (Credentials) null)) {
switch (yearResponse.getStatusLine().getStatusCode()) {
case 200:
result = EntityUtils.toString(yearResponse.getEntity());
break;
case 401:
Logger.getRootLogger().info("The supplied credentials are invalid!");
break;
default:
Logger.getRootLogger().info("The request was not successful. Reason: %s", yearResponse.getStatusLine().getReasonPhrase());
break;
}
}
return result;
}
示例10: process
import org.apache.http.auth.Credentials; //导入依赖的package包/类
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) {
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);
}
}
}
示例11: doPreemptiveAuth
import org.apache.http.auth.Credentials; //导入依赖的package包/类
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");
}
}
}
示例12: authenticate
import org.apache.http.auth.Credentials; //导入依赖的package包/类
/**
* Produces a digest authorization string for the given set of
* {@link Credentials}, method name and URI.
*
* @param credentials A set of credentials to be used for athentication
* @param request The request being authenticated
*
* @throws org.apache.http.auth.InvalidCredentialsException if authentication credentials
* are not valid or not applicable for this authentication scheme
* @throws AuthenticationException if authorization string cannot
* be generated due to an authentication failure
*
* @return a digest authorization string
*/
@Override
public Header authenticate(
final Credentials credentials,
final HttpRequest request,
final HttpContext context) throws AuthenticationException {
Args.notNull(credentials, "Credentials");
Args.notNull(request, "HTTP request");
if (getParameter("realm") == null) {
throw new AuthenticationException("missing realm in challenge");
}
if (getParameter("nonce") == null) {
throw new AuthenticationException("missing nonce in challenge");
}
// Add method name and request-URI to the parameter map
getParameters().put("methodname", request.getRequestLine().getMethod());
getParameters().put("uri", request.getRequestLine().getUri());
final String charset = getParameter("charset");
if (charset == null) {
getParameters().put("charset", getCredentialsCharset(request));
}
return createDigestHeader(credentials, request);
}
示例13: matchCredentials
import org.apache.http.auth.Credentials; //导入依赖的package包/类
/**
* Find matching {@link Credentials credentials} for the given authentication scope.
*
* @param map the credentials hash map
* @param authscope the {@link AuthScope authentication scope}
* @return the credentials
*
*/
private static Credentials matchCredentials(
final Map<AuthScope, Credentials> map,
final AuthScope authscope) {
// see if we get a direct hit
Credentials creds = map.get(authscope);
if (creds == null) {
// Nope.
// Do a full scan
int bestMatchFactor = -1;
AuthScope bestMatch = null;
for (final AuthScope current: map.keySet()) {
final int factor = authscope.match(current);
if (factor > bestMatchFactor) {
bestMatchFactor = factor;
bestMatch = current;
}
}
if (bestMatch != null) {
creds = map.get(bestMatch);
}
}
return creds;
}
示例14: testDontTryToAuthenticateEndlessly
import org.apache.http.auth.Credentials; //导入依赖的package包/类
/**
* Tests that the client will stop connecting to the server if
* the server still keep asking for a valid ticket.
*/
@Test
public void testDontTryToAuthenticateEndlessly() throws Exception {
this.serverBootstrap.registerHandler("*", new PleaseNegotiateService());
final HttpHost target = start();
final AuthSchemeProvider nsf = new NegotiateSchemeProviderWithMockGssManager();
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
final Credentials use_jaas_creds = new UseJaasCredentials();
credentialsProvider.setCredentials(new AuthScope(null, -1, null), use_jaas_creds);
final Registry<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider>create()
.register(AuthSchemes.SPNEGO, nsf)
.build();
this.httpclient = HttpClients.custom()
.setDefaultAuthSchemeRegistry(authSchemeRegistry)
.setDefaultCredentialsProvider(credentialsProvider)
.build();
final String s = "/path";
final HttpGet httpget = new HttpGet(s);
final HttpResponse response = this.httpclient.execute(target, httpget);
EntityUtils.consume(response.getEntity());
Assert.assertEquals(HttpStatus.SC_UNAUTHORIZED, response.getStatusLine().getStatusCode());
}
示例15: getCredentials
import org.apache.http.auth.Credentials; //导入依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
public Credentials getCredentials(final AuthScope authscope) {
// iterate over all active proxy configurations at the moment of getting the credential
for (final ServiceRegistration registration : registeredConfigurations.values()) {
final Object proxyConfigurationObject = bundleContext.getService(registration.getReference());
if (proxyConfigurationObject != null) {
final ProxyConfiguration proxyConfiguration = (ProxyConfiguration) proxyConfigurationObject;
if (proxyConfiguration.isEnabled()) {
final AuthScope actual = new AuthScope(proxyConfiguration.getHostname(), proxyConfiguration.getPort());
if (authscope.match(actual) >= 12) {
return new UsernamePasswordCredentials(proxyConfiguration.getUsername(), proxyConfiguration.getPassword());
}
}
}
}
// credentials no longer available!
return null;
}