本文整理汇总了Java中org.apache.http.client.config.AuthSchemes类的典型用法代码示例。如果您正苦于以下问题:Java AuthSchemes类的具体用法?Java AuthSchemes怎么用?Java AuthSchemes使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
AuthSchemes类属于org.apache.http.client.config包,在下文中一共展示了AuthSchemes类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: useCredentials
import org.apache.http.client.config.AuthSchemes; //导入依赖的package包/类
private void useCredentials(CredentialsProvider credentialsProvider, String host, int port, Collection<? extends Authentication> authentications) {
Credentials httpCredentials;
for (Authentication authentication : authentications) {
String scheme = getAuthScheme(authentication);
PasswordCredentials credentials = getPasswordCredentials(authentication);
if (authentication instanceof AllSchemesAuthentication) {
NTLMCredentials ntlmCredentials = new NTLMCredentials(credentials);
httpCredentials = new NTCredentials(ntlmCredentials.getUsername(), ntlmCredentials.getPassword(), ntlmCredentials.getWorkstation(), ntlmCredentials.getDomain());
credentialsProvider.setCredentials(new AuthScope(host, port, AuthScope.ANY_REALM, AuthSchemes.NTLM), httpCredentials);
LOGGER.debug("Using {} and {} for authenticating against '{}:{}' using {}", credentials, ntlmCredentials, host, port, AuthSchemes.NTLM);
}
httpCredentials = new UsernamePasswordCredentials(credentials.getUsername(), credentials.getPassword());
credentialsProvider.setCredentials(new AuthScope(host, port, AuthScope.ANY_REALM, scheme), httpCredentials);
LOGGER.debug("Using {} for authenticating against '{}:{}' using {}", credentials, host, port, scheme);
}
}
示例2: request_has_proxy_config_when_proxy_auth_enabled
import org.apache.http.client.config.AuthSchemes; //导入依赖的package包/类
@Test
public void request_has_proxy_config_when_proxy_auth_enabled() throws Exception {
List<ProxyAuthenticationMethod> authMethods = Arrays.asList(ProxyAuthenticationMethod.BASIC,
ProxyAuthenticationMethod.DIGEST,
ProxyAuthenticationMethod.KERBEROS,
ProxyAuthenticationMethod.NTLM,
ProxyAuthenticationMethod.SPNEGO);
List<String> expectedAuthMethods = Arrays.asList(AuthSchemes.BASIC,
AuthSchemes.DIGEST,
AuthSchemes.KERBEROS,
AuthSchemes.NTLM,
AuthSchemes.SPNEGO);
ClientConfiguration configuration = new ClientConfiguration().withProxyHost("localhost")
.withProxyPort(80)
.withProxyUsername("user")
.withProxyPassword("password")
.withProxyAuthenticationMethods(authMethods);
HttpClientSettings settings = HttpClientSettings.adapt(configuration);
HttpRequestBase requestBase = requestFactory.create(newDefaultRequest(HttpMethodName.POST), settings);
Assert.assertEquals(expectedAuthMethods, requestBase.getConfig().getProxyPreferredAuthSchemes());
}
示例3: setUsernamePassword
import org.apache.http.client.config.AuthSchemes; //导入依赖的package包/类
@Override public void setUsernamePassword(AuthenticationType authType, String username,
String password) {
this.credentials = new UsernamePasswordCredentials(
Objects.requireNonNull(username), Objects.requireNonNull(password));
this.credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, credentials);
RegistryBuilder<AuthSchemeProvider> authRegistryBuilder = RegistryBuilder.create();
switch (authType) {
case BASIC:
authRegistryBuilder.register(AuthSchemes.BASIC, new BasicSchemeFactory());
break;
case DIGEST:
authRegistryBuilder.register(AuthSchemes.DIGEST, new DigestSchemeFactory());
break;
default:
throw new IllegalArgumentException("Unsupported authentiation type: " + authType);
}
this.authRegistry = authRegistryBuilder.build();
}
示例4: build
import org.apache.http.client.config.AuthSchemes; //导入依赖的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();
}
示例5: ProxyClient
import org.apache.http.client.config.AuthSchemes; //导入依赖的package包/类
/**
* @since 4.3
*/
public ProxyClient(
final HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connFactory,
final ConnectionConfig connectionConfig,
final RequestConfig requestConfig) {
super();
this.connFactory = connFactory != null ? connFactory : ManagedHttpClientConnectionFactory.INSTANCE;
this.connectionConfig = connectionConfig != null ? connectionConfig : ConnectionConfig.DEFAULT;
this.requestConfig = requestConfig != null ? requestConfig : RequestConfig.DEFAULT;
this.httpProcessor = new ImmutableHttpProcessor(
new RequestTargetHostHC4(), new RequestClientConnControl(), new RequestUserAgentHC4());
this.requestExec = new HttpRequestExecutor();
this.proxyAuthStrategy = new ProxyAuthenticationStrategy();
this.authenticator = new HttpAuthenticator();
this.proxyAuthState = new AuthStateHC4();
this.authSchemeRegistry = new AuthSchemeRegistry();
this.authSchemeRegistry.register(AuthSchemes.BASIC, new BasicSchemeFactoryHC4());
this.authSchemeRegistry.register(AuthSchemes.DIGEST, new DigestSchemeFactoryHC4());
this.authSchemeRegistry.register(AuthSchemes.NTLM, new NTLMSchemeFactory());
this.reuseStrategy = new DefaultConnectionReuseStrategyHC4();
}
示例6: setupCredentials
import org.apache.http.client.config.AuthSchemes; //导入依赖的package包/类
/**
* Configure credentials and auth scheme on client if an authorization is
* available for url
* @param client {@link HttpClient}
* @param url URL to test
* @param credentialsProvider {@link CredentialsProvider}
* @param localHost host running JMeter
*/
public void setupCredentials(HttpClient client, URL url,
CredentialsProvider credentialsProvider, String localHost) {
Authorization auth = getAuthForURL(url);
if (auth != null) {
String username = auth.getUser();
String realm = auth.getRealm();
String domain = auth.getDomain();
if (log.isDebugEnabled()){
log.debug(username + " > D="+domain+" R="+realm + " M="+auth.getMechanism());
}
if (Mechanism.KERBEROS.equals(auth.getMechanism())) {
((AbstractHttpClient) client).getAuthSchemes().register(
AuthSchemes.SPNEGO,
new FixedSPNegoSchemeFactory(isStripPort(url)));
credentialsProvider.setCredentials(new AuthScope(null, -1, null), USE_JAAS_CREDENTIALS);
} else {
credentialsProvider.setCredentials(
new AuthScope(url.getHost(), url.getPort(), realm.length()==0 ? null : realm),
new NTCredentials(username, auth.getPass(), localHost, domain));
}
}
}
示例7: ProxyClient
import org.apache.http.client.config.AuthSchemes; //导入依赖的package包/类
/**
* @since 4.3
*/
public ProxyClient(
final HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connFactory,
final ConnectionConfig connectionConfig,
final RequestConfig requestConfig) {
super();
this.connFactory = connFactory != null ? connFactory : ManagedHttpClientConnectionFactory.INSTANCE;
this.connectionConfig = connectionConfig != null ? connectionConfig : ConnectionConfig.DEFAULT;
this.requestConfig = requestConfig != null ? requestConfig : RequestConfig.DEFAULT;
this.httpProcessor = new ImmutableHttpProcessor(
new RequestTargetHost(), new RequestClientConnControl(), new RequestUserAgent());
this.requestExec = new HttpRequestExecutor();
this.proxyAuthStrategy = new ProxyAuthenticationStrategy();
this.authenticator = new HttpAuthenticator();
this.proxyAuthState = new AuthState();
this.authSchemeRegistry = new AuthSchemeRegistry();
this.authSchemeRegistry.register(AuthSchemes.BASIC, new BasicSchemeFactory());
this.authSchemeRegistry.register(AuthSchemes.DIGEST, new DigestSchemeFactory());
this.authSchemeRegistry.register(AuthSchemes.NTLM, new NTLMSchemeFactory());
this.authSchemeRegistry.register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory());
this.authSchemeRegistry.register(AuthSchemes.KERBEROS, new KerberosSchemeFactory());
this.reuseStrategy = new DefaultConnectionReuseStrategy();
}
示例8: testDontTryToAuthenticateEndlessly
import org.apache.http.client.config.AuthSchemes; //导入依赖的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());
}
示例9: createBuilder
import org.apache.http.client.config.AuthSchemes; //导入依赖的package包/类
private static HttpClientBuilder createBuilder() {
if (isWinAuthAvailable()) {
final Registry<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider>create()
.register(AuthSchemes.BASIC, new BasicSchemeFactory())
.register(AuthSchemes.DIGEST, new DigestSchemeFactory())
.register(AuthSchemes.NTLM, new WindowsNTLMSchemeFactory(null))
.register(AuthSchemes.SPNEGO, new WindowsNegotiateSchemeFactory(null))
.build();
final CredentialsProvider credsProvider = new WindowsCredentialsProvider(new SystemDefaultCredentialsProvider());
return HttpClientBuilder.create()
.setDefaultCredentialsProvider(credsProvider)
.setDefaultAuthSchemeRegistry(authSchemeRegistry);
} else {
return HttpClientBuilder.create();
}
}
示例10: setUp
import org.apache.http.client.config.AuthSchemes; //导入依赖的package包/类
@Before @Override
public void setUp() throws Exception {
super.setUp();
this.serverBootstrap.registerHandler("/", new HttpRequestHandler() {
@Override
public void handle(
final HttpRequest request,
final HttpResponse response,
final HttpContext context) throws HttpException, IOException {
response.addHeader(AUTH.WWW_AUTH, AuthSchemes.SPNEGO);
response.setStatusCode(HttpStatus.SC_UNAUTHORIZED);
}
});
}
示例11: getHttpClient
import org.apache.http.client.config.AuthSchemes; //导入依赖的package包/类
private HttpClient getHttpClient() {
Credentials use_jaas_creds = new Credentials() {
public String getPassword() {
return null;
}
public Principal getUserPrincipal() {
return null;
}
};
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(null, -1, null), use_jaas_creds);
Registry<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider>create().register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory(true)).build();
CloseableHttpClient httpclient = HttpClients.custom().setDefaultAuthSchemeRegistry(authSchemeRegistry).setDefaultCredentialsProvider(credsProvider).build();
return httpclient;
}
示例12: checkUserCredentials
import org.apache.http.client.config.AuthSchemes; //导入依赖的package包/类
public static void checkUserCredentials(String username, String password, AuthScheme authScheme) throws NoSuchFieldException,
IllegalAccessException
{
CredentialsProvider provider = getCredentialsProvider();
String httpScheme = AuthScope.ANY_SCHEME;
if (authScheme == AuthScheme.BASIC) {
httpScheme = AuthSchemes.BASIC;
} else if (authScheme == AuthScheme.DIGEST) {
httpScheme = AuthSchemes.DIGEST;
}
AuthScope authScope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM, httpScheme);
Credentials credentials = provider.getCredentials(authScope);
Assert.assertNotNull("Credentials", credentials);
Assert.assertTrue("Credentials type is user", UsernamePasswordCredentials.class.isAssignableFrom(credentials.getClass()));
UsernamePasswordCredentials pwdCredentials = (UsernamePasswordCredentials)credentials;
Assert.assertEquals("Username", username, pwdCredentials.getUserName());
Assert.assertEquals("Password", password, pwdCredentials.getPassword());
}
示例13: doRequest
import org.apache.http.client.config.AuthSchemes; //导入依赖的package包/类
private void doRequest(URL url) {
WinRmClientBuilder builder = WinRmClient.builder(url);
builder.authenticationScheme(AuthSchemes.BASIC);
WinRmClient client = builder.build();
StringWriter out = new StringWriter();
StringWriter err = new StringWriter();
int code;
try (ShellCommand shell = client.createShell()) {
code = shell.execute("echo myline", out, err);
}
assertEquals(out.toString(), "myline\r\n");
assertEquals(err.toString(), "");
assertEquals(code, 0);
}
示例14: httpClient
import org.apache.http.client.config.AuthSchemes; //导入依赖的package包/类
@Bean
@ConditionalOnMissingBean(HttpClient.class)
public HttpClient httpClient(){
HttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(30000)
.setConnectTimeout(30000)
.setTargetPreferredAuthSchemes(ImmutableList.of(AuthSchemes.BASIC))
.setProxyPreferredAuthSchemes(ImmutableList.of(AuthSchemes.BASIC))
.build();
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, getUsernamePasswordCredentials());
return HttpClients.custom().setConnectionManager(cm)
.setDefaultCredentialsProvider(credentialsProvider)
.setDefaultRequestConfig(requestConfig)
.build();
}
示例15: getHttpClient
import org.apache.http.client.config.AuthSchemes; //导入依赖的package包/类
protected final CloseableHttpClient getHttpClient(final boolean useSpnego) throws Exception {
final CredentialsProvider credsProvider = new BasicCredentialsProvider();
final HttpClientBuilder hcb = HttpClients.custom();
if (useSpnego) {
//SPNEGO/Kerberos setup
log.debug("SPNEGO activated");
final AuthSchemeProvider nsf = new SPNegoSchemeFactory(true);// new NegotiateSchemeProvider();
final Credentials jaasCreds = new JaasCredentials();
credsProvider.setCredentials(new AuthScope(null, -1, null, AuthSchemes.SPNEGO), jaasCreds);
credsProvider.setCredentials(new AuthScope(null, -1, null, AuthSchemes.NTLM), new NTCredentials("Guest", "Guest", "Guest",
"Guest"));
final Registry<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider> create()
.register(AuthSchemes.SPNEGO, nsf).register(AuthSchemes.NTLM, new NTLMSchemeFactory()).build();
hcb.setDefaultAuthSchemeRegistry(authSchemeRegistry);
}
hcb.setDefaultCredentialsProvider(credsProvider);
hcb.setDefaultSocketConfig(SocketConfig.custom().setSoTimeout(10 * 1000).build());
final CloseableHttpClient httpClient = hcb.build();
return httpClient;
}