下面列出了怎么用org.apache.http.auth.AuthScheme的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
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.update(authScheme, creds);
}
}
}
@Override
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
AuthState authState = (AuthState) context.getAttribute(HttpClientContext.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(HttpClientContext.CREDS_PROVIDER);
HttpHost targetHost = (HttpHost) context.getAttribute(HttpCoreContext.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.update(authScheme, creds);
}
}
}
@Override
public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
Credentials creds;
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) {
creds =
credsProvider.getCredentials(new AuthScope(
targetHost.getHostName(),
targetHost.getPort()));
if (creds == null) {
throw new HttpException("No credentials for preemptive authentication");
}
authState.update(authScheme, creds);
}
}
}
@Override
public void process(final HttpRequest request, final HttpContext context) {
// never ever send credentials preemptively to a host which is not the configured Gerrit host
if (!isForGerritHost(request)) {
return;
}
AuthState authState = (AuthState) context.getAttribute(HttpClientContext.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);
UsernamePasswordCredentials creds = new UsernamePasswordCredentials(authData.getLogin(), authData.getPassword());
authState.update(authScheme, creds);
}
}
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);
if (DEBUG) {
Logger.debug("Authorization challenge processed");
}
}
private AuthScheme getAuthScheme() {
final String scheme = getProtocolScheme();
if (Constants.BASIC.equals(scheme)) {
return new BasicScheme();
} else if (Constants.DIGEST.equals(scheme)) {
return new DigestScheme();
} else if (Constants.NTLM.equals(scheme)) {
final Properties props = new Properties();
getWebConfig().getConfigParameterMap(ConfigName.CONFIG).entrySet().stream()
.filter(e -> e.getKey().startsWith(Config.JCIFS_PREFIX)).forEach(e -> {
props.setProperty(e.getKey(), e.getValue());
});
return new NTLMScheme(new JcifsEngine(props));
} else if (Constants.FORM.equals(scheme)) {
final Map<String, String> parameterMap = ParameterUtil.parse(getParameters());
return new FormScheme(parameterMap);
}
return null;
}
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);
}
}
}
public static void enableAuth(final AbstractHttpClient client, final Keychain keychain, final KeyId keyId) {
if (client == null) {
throw new NullPointerException("client");
}
if (keychain == null) {
throw new NullPointerException("keychain");
}
client.getAuthSchemes().register(Constants.SCHEME, new AuthSchemeFactory() {
public AuthScheme newInstance(HttpParams params) {
return new Http4SignatureAuthScheme();
}
});
Signer signer = new Signer(keychain, keyId);
client.getCredentialsProvider().setCredentials(AuthScope.ANY, new SignerCredentials(signer));
client.getParams().setParameter(AuthPNames.TARGET_AUTH_PREF,
Arrays.asList(Constants.SCHEME));
HttpClientParams.setAuthenticating(client.getParams(), true);
}
@Substitute
@Override
public void put(final HttpHost host, final AuthScheme authScheme) {
Args.notNull(host, "HTTP host");
if (authScheme == null) {
return;
}
this.map.put(getKey(host), authScheme);
}
@Override
public void put(final HttpHost host, final AuthScheme authScheme) {
Args.notNull(host, "HTTP host");
if (authScheme == null) {
return;
}
this.map.put(getKey(host), authScheme);
}
@Override
public void authSucceeded(final HttpHost authhost, final AuthScheme authScheme, final HttpContext context) {
final HttpClientContext clientContext = HttpClientContext.adapt(context);
final Credentials credentials = clientContext.getAttribute(PROXY_CREDENTIALS_INPUT_ID, Credentials.class);
if(null != credentials) {
clientContext.removeAttribute(PROXY_CREDENTIALS_INPUT_ID);
if(log.isInfoEnabled()) {
log.info(String.format("Save passphrase for proxy %s", authhost));
}
keychain.addCredentials(authhost.toURI(), credentials.getUsername(), credentials.getPassword());
}
super.authSucceeded(authhost, authScheme, context);
}
@NotNull
public HttpClientContext buildClientContext(@NotNull HttpHost httpHost, @NotNull AuthScheme authScheme) {
AuthCache basicAuthCache = new BasicAuthCache();
basicAuthCache.put(httpHost, authScheme);
HttpClientContext httpClientContext = HttpClientContext.create();
httpClientContext.setAuthCache(basicAuthCache);
httpClientContext.setTargetHost(httpHost);
return httpClientContext;
}
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);
}
}
}
@Substitute
@Override
public AuthScheme get(final HttpHost host) {
Args.notNull(host, "HTTP host");
return this.map.get(getKey(host));
}
public AuthScheme newInstance(HttpParams params) {
return new NTLMScheme(new JCIFSEngine());
}
public AuthScheme newInstance(HttpParams params) {
return new NTLMScheme(new JCIFSEngine());
}
@Override
public synchronized void put(final HttpHost host, final AuthScheme authScheme) {
super.put(host, authScheme);
}
@Override
public synchronized AuthScheme get(final HttpHost host) {
return super.get(host);
}
@Override
public AuthScheme create(HttpContext context) {
return new NTLMScheme(new JCIFSEngine());
}
@Override
public AuthScheme get(final HttpHost host) {
Args.notNull(host, "HTTP host");
return this.map.get(getKey(host));
}
@Override
public void put(HttpHost host, AuthScheme authScheme) {
map.put(host, authScheme);
}
@Override
public AuthScheme get(HttpHost host) {
return map.get(host);
}
public AuthScheme newInstance(
final HttpParams params ) {
return new SPNegoScheme(gssClient, servicePrincipalName, servicePrincipalOid);
}
public AuthScheme create(
final HttpContext context ) {
return new SPNegoScheme(gssClient, servicePrincipalName, servicePrincipalOid);
}
@Override
public AuthScheme create(final HttpContext context) {
return new BackportWindowsNegotiateScheme(AuthSchemes.NTLM, servicePrincipalName);
}
@Override
public void authFailed(final HttpHost authhost, final AuthScheme authScheme, final HttpContext context) {
keychain.deleteCredentials(authhost.getHostName());
super.authFailed(authhost, authScheme, context);
}
@Override
public AuthScheme create(final HttpContext context) {
return new BackportWindowsNegotiateScheme(AuthSchemes.SPNEGO, servicePrincipalName);
}
public PreemptiveAuth(AuthScheme authScheme) {
this.authScheme = authScheme;
}
public AuthScheme newInstance(HttpParams params) {
return new NTLMScheme(new JCIFSEngine());
}
public AuthScheme newInstance(HttpParams params) {
return new NTLMScheme(new JCIFSEngine());
}