Refine search
@Override public Request authenticate(Route route, Response response) throws IOException { List<Challenge> challenges = response.challenges(); Request request = response.request(); HttpUrl url = request.url(); boolean proxyAuthorization = response.code() == 407; Proxy proxy = route.proxy(); String credential = Credentials.basic( auth.getUserName(), new String(auth.getPassword()), challenge.charset()); return request.newBuilder() .header(proxyAuthorization ? "Proxy-Authorization" : "Authorization", credential) .build();
@Override public Request authenticate(Route route, Response response) throws IOException { String credential = Credentials.basic(userName, password); return response .request() .newBuilder() .header("Proxy-Authorization", credential) .build(); } });
private String authorize() throws IOException { OkHttpClient.Builder builder = client.newBuilder(); builder.interceptors().remove(this); OkHttpClient clone = builder.build(); String credential = Credentials.basic(config.getUsername(), new String(config.getPassword())); URL url = new URL(URLUtils.join(config.getMasterUrl(), AUTHORIZE_PATH)); Response response = clone.newCall(new Request.Builder().get().url(url).header(AUTHORIZATION, credential).build()).execute(); response.body().close(); response = response.priorResponse() != null ? response.priorResponse() : response; response = response.networkResponse() != null ? response.networkResponse() : response; String token = response.header(LOCATION); if (token == null || token.isEmpty()) { throw new IOException("Unexpected response(" + response.code() + " " + response.message() + "), to the authorization request. Missing header:[" + LOCATION + "]!"); } token = token.substring(token.indexOf(BEFORE_TOKEN) + BEFORE_TOKEN.length()); token = token.substring(0, token.indexOf(AFTER_TOKEN)); return token; } }
@Override public Request authenticate(Route route, Response response) throws IOException { if (response.request().header("Authorization") != null) { return null; // Give up, we've already attempted to authenticate. } System.out.println("Authenticating for response: " + response); System.out.println("Challenges: " + response.challenges()); String credential = Credentials.basic("jesse", "password1"); return response.request().newBuilder() .header("Authorization", credential) .build(); } })
build(); final Request request = new Request.Builder(). url( this.ext.getUpload().getEndpoint() ). header( "Authorization", Credentials.basic( "su", "password") ). post( body ). if ( response.isSuccessful() ) final int code = response.code(); final ResponseBody resBody = response.body(); final String text = resBody != null ? resBody.string() : "Empty message";
public static String get(InetSocketAddress httpAddr, String username, String password, String path) throws IOException { try { final OkHttpClient client = new OkHttpClient(); Request.Builder builder = new Request.Builder(); if (username != null && password != null) { builder.header("Authorization", Credentials.basic(username, password)); } Request request = builder.url(String.format("http://%s:%d%s", httpAddr.getHostString(), httpAddr.getPort(), path)) .build(); LOG.info("Calling URL: {}", request.url()); Response response = client.newCall(request).execute(); return response.isSuccessful() ? response.body().string() : null; } catch (IOException e) { LOG.info("Calling URL failed: {}", e.getMessage()); return null; } } }
@Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); // If the request already have an authorization (eg. Basic auth), do nothing if (request.header("Authorization") == null) { String credentials = Credentials.basic(username, password); request = request.newBuilder() .addHeader("Authorization", credentials) .build(); } return chain.proceed(request); } }
public static void main(String[] args) throws IOException { OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .url("http://httpbin.org/basic-auth/biezhi/passwd") .addHeader("Authorization", Credentials.basic("biezhi", "passwd")) .build(); try (Response response = client.newCall(request).execute()) { System.out.println(response.body().string()); } }
public static Interceptor basicAuth(String user, String password) { requireNonNull(user, "user is null"); requireNonNull(password, "password is null"); if (user.contains(":")) { throw new ClientException("Illegal character ':' found in username"); } String credential = Credentials.basic(user, password); return chain -> chain.proceed(chain.request().newBuilder() .header(AUTHORIZATION, credential) .build()); }
protected Response sendRequestToElasticsearch(OkHttpClient client, URL url, String username, String password, String verb, RequestBody body) throws IOException { final ComponentLog log = getLogger(); Request.Builder requestBuilder = new Request.Builder() .url(url); if ("get".equalsIgnoreCase(verb)) { requestBuilder = requestBuilder.get(); } else if ("put".equalsIgnoreCase(verb)) { requestBuilder = requestBuilder.put(body); } else if ("post".equalsIgnoreCase(verb)) { requestBuilder = requestBuilder.post(body); } else { throw new IllegalArgumentException("Elasticsearch REST API verb not supported by this processor: " + verb); } if(!StringUtils.isEmpty(username) && !StringUtils.isEmpty(password)) { String credential = Credentials.basic(username, password); requestBuilder = requestBuilder.header("Authorization", credential); } Request httpRequest = requestBuilder.build(); log.debug("Sending Elasticsearch request to {}", new Object[]{url}); Response responseHttp = client.newCall(httpRequest).execute(); // store the status code and message int statusCode = responseHttp.code(); if (statusCode == 0) { throw new IllegalStateException("Status code unknown, connection hasn't been attempted."); } log.debug("Received response from Elasticsearch with status code {}", new Object[]{statusCode}); return responseHttp; }
BasicAuthInterceptor(String host, String username, String password) { this.credentials = Credentials.basic(username, password); this.host = host; }
@Nullable @Override public Request authenticate(Route route, Response response) throws IOException { String credential = Credentials.basic(proxyUsername, proxyPassword); return response.request() .newBuilder() .header("Proxy-Authorization", credential) .build(); } }
private String authorize() throws IOException { OkHttpClient.Builder builder = client.newBuilder(); builder.interceptors().remove(this); OkHttpClient clone = builder.build(); String credential = Credentials.basic(config.getUsername(), new String(config.getPassword())); URL url = new URL(URLUtils.join(config.getMasterUrl(), AUTHORIZE_PATH)); Response response = clone.newCall(new Request.Builder().get().url(url).header(AUTHORIZATION, credential).build()).execute(); response.body().close(); response = response.priorResponse() != null ? response.priorResponse() : response; response = response.networkResponse() != null ? response.networkResponse() : response; String token = response.header(LOCATION); if (token == null || token.isEmpty()) { throw new IOException("Unexpected response(" + response.code() + " " + response.message() + "), to the authorization request. Missing header:[" + LOCATION + "]!"); } token = token.substring(token.indexOf(BEFORE_TOKEN) + BEFORE_TOKEN.length()); token = token.substring(0, token.indexOf(AFTER_TOKEN)); return token; } }
if (proxyLogin != null) { builder.proxyAuthenticator((route, response) -> { if (response.request().header(PROXY_AUTHORIZATION) != null) { if (HttpURLConnection.HTTP_PROXY_AUTH == response.code()) { String credential = Credentials.basic(proxyLogin, nullToEmpty(proxyPassword), UTF_8); return response.request().newBuilder().header(PROXY_AUTHORIZATION, credential).build();
build(); final Request request = new Request.Builder(). url( this.ext.getUpload().getEndpoint() ). header( "Authorization", Credentials.basic( "su", "password") ). post( body ). if ( response.isSuccessful() ) final int code = response.code(); final ResponseBody resBody = response.body(); final String text = resBody != null ? resBody.string() : "Empty message";
@Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); // If the request already have an authorization (eg. Basic auth), do nothing if (request.header("Authorization") == null) { String credentials = Credentials.basic(username, password); request = request.newBuilder() .addHeader("Authorization", credentials) .build(); } return chain.proceed(request); } }
@SneakyThrows private String callGet(String url) { log.info("call get : " + url); Request request = new Request.Builder() .addHeader("Authorization", Credentials.basic(sccaConfigServerProperties.getUsername(), sccaConfigServerProperties.getPassword())) .url(url) .get() .build(); Call call = okHttpClient.newCall(request); Response response = call.execute(); ResponseBody responseBody = response.body(); return responseBody.string(); }
@Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); if (Utils.isNotNullOrEmpty(config.getUsername()) && Utils.isNotNullOrEmpty(config.getPassword())) { Request authReq = chain.request().newBuilder().addHeader("Authorization", Credentials.basic(config.getUsername(), config.getPassword())).build(); return chain.proceed(authReq); } else if (Utils.isNotNullOrEmpty(config.getOauthToken())) { Request authReq = chain.request().newBuilder().addHeader("Authorization", "Bearer " + config.getOauthToken()).build(); return chain.proceed(authReq); } return chain.proceed(request); } }).addInterceptor(new ImpersonatorInterceptor(config))
final String credentials = Credentials.basic(httpUrl.username(), httpUrl .password()); headersBuilder.add("Authorization", credentials); final Request request = new Request.Builder().url(httpUrl).get().headers( headersBuilder.build()).build(); final Response tmpResult = client().newCall(request).execute(); if (tmpResult.code() == 200) { else if (tmpResult.code() == 206) { .code());
/** Returns an auth credential for the Basic scheme. */ public static String basic(String username, String password) { return basic(username, password, ISO_8859_1); }