public Request authenticate(Proxy proxy, Response response) throws IOException { List<Challenge> challenges = response.challenges(); Request request = response.request(); HttpUrl url = request.httpUrl(); int size = challenges.size(); for (int i = 0; i < size; i++) { Challenge challenge = (Challenge) challenges.get(i); if ("Basic".equalsIgnoreCase(challenge.getScheme())) { PasswordAuthentication auth = java.net.Authenticator .requestPasswordAuthentication(url.host(), getConnectToInetAddress(proxy, url), url.port(), url.scheme(), challenge.getRealm(), challenge .getScheme(), url.url(), RequestorType.SERVER); if (auth != null) { return request.newBuilder().header("Authorization", Credentials.basic(auth .getUserName(), new String(auth.getPassword()))).build(); } } } return null; }
public Request authenticateProxy(Proxy proxy, Response response) throws IOException { List<Challenge> challenges = response.challenges(); Request request = response.request(); HttpUrl url = request.httpUrl(); int size = challenges.size(); for (int i = 0; i < size; i++) { Challenge challenge = (Challenge) challenges.get(i); if ("Basic".equalsIgnoreCase(challenge.getScheme())) { InetSocketAddress proxyAddress = (InetSocketAddress) proxy.address(); PasswordAuthentication auth = java.net.Authenticator .requestPasswordAuthentication(proxyAddress.getHostName(), getConnectToInetAddress(proxy, url), proxyAddress.getPort(), url .scheme(), challenge.getRealm(), challenge.getScheme(), url.url(), RequestorType.PROXY); if (auth != null) { return request.newBuilder().header("Proxy-Authorization", Credentials.basic (auth.getUserName(), new String(auth.getPassword()))).build(); } } } return null; }
@Override public Request authenticate(Proxy proxy, Response response) throws IOException { List<Challenge> challenges = response.challenges(); Request request = response.request(); URL url = request.url(); for (int i = 0, size = challenges.size(); i < size; i++) { Challenge challenge = challenges.get(i); if (!"Basic".equalsIgnoreCase(challenge.getScheme())) continue; PasswordAuthentication auth = java.net.Authenticator.requestPasswordAuthentication( url.getHost(), getConnectToInetAddress(proxy, url), url.getPort(), url.getProtocol(), challenge.getRealm(), challenge.getScheme(), url, RequestorType.SERVER); if (auth == null) continue; String credential = Credentials.basic(auth.getUserName(), new String(auth.getPassword())); return request.newBuilder() .header("Authorization", credential) .build(); } return null; }
@Override public Request authenticateProxy(Proxy proxy, Response response) throws IOException { List<Challenge> challenges = response.challenges(); Request request = response.request(); URL url = request.url(); for (int i = 0, size = challenges.size(); i < size; i++) { Challenge challenge = challenges.get(i); if (!"Basic".equalsIgnoreCase(challenge.getScheme())) continue; InetSocketAddress proxyAddress = (InetSocketAddress) proxy.address(); PasswordAuthentication auth = java.net.Authenticator.requestPasswordAuthentication( proxyAddress.getHostName(), getConnectToInetAddress(proxy, url), proxyAddress.getPort(), url.getProtocol(), challenge.getRealm(), challenge.getScheme(), url, RequestorType.PROXY); if (auth == null) continue; String credential = Credentials.basic(auth.getUserName(), new String(auth.getPassword())); return request.newBuilder() .header("Proxy-Authorization", credential) .build(); } return null; }
public static Request processAuthHeader(Authenticator authenticator, Response response, Proxy proxy) throws IOException { if (response.code() == 407) { return authenticator.authenticateProxy(proxy, response); } return authenticator.authenticate(proxy, response); }
public static Request processAuthHeader(Authenticator paramAuthenticator, Response paramResponse, Proxy paramProxy) throws IOException { if (paramResponse.code == 407) { return paramAuthenticator.authenticateProxy(paramProxy, paramResponse); } return paramAuthenticator.authenticate(paramProxy, paramResponse); }
/** * 设置Http AUTH认证 * * @param authenticator * @return */ public static OkHttpManager setAuthenticator(Authenticator authenticator) { if (null != authenticator) { OkHttpClientManager.getInstance().setAuthenticator(authenticator); } return mInstance; }
/** * React to a failed authorization response by looking up new credentials. * Returns a request for a subsequent attempt, or null if no further attempts * should be made. */ public static Request processAuthHeader(Authenticator authenticator, Response response, Proxy proxy) throws IOException { return response.code() == HTTP_PROXY_AUTH ? authenticator.authenticateProxy(proxy, response) : authenticator.authenticate(proxy, response); }
private void configureAuthenticator(OkHttpClient client, AuthStrategy authStrategy) { Authenticator recoverableAuthenticator = new RecoverableAuthenticator(authStrategy, mCredentials); Authenticator authenticator = recoverableAuthenticator; if (mAuthenticationPolicy == AuthPolicy.FAIL_FAST) { authenticator = new SingleTimeAuthenticator(recoverableAuthenticator); } client.setAuthenticator(authenticator); }
public void setAuthenticator(Authenticator authenticator) { if (null != authenticator) { mClient.setAuthenticator(authenticator); } }
public SingleTimeAuthenticator(Authenticator authenticator) { this.authenticator = authenticator; }