okhttp3.Credentials类的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(10.9k)|赞(0)|评价(0)|浏览(203)

本文整理了Java中okhttp3.Credentials类的一些代码示例,展示了Credentials类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Credentials类的具体详情如下:
包路径:okhttp3.Credentials
类名称:Credentials

Credentials介绍

[英]Factory for HTTP authorization credentials.
[中]HTTP授权凭据的工厂。

代码示例

代码示例来源:origin: square/okhttp

@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();

代码示例来源:origin: googlemaps/google-maps-services-java

@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();
 }
});

代码示例来源:origin: fabric8io/kubernetes-client

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;
  }
}

代码示例来源:origin: square/okhttp

@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();
 }
})

代码示例来源:origin: gradle.plugin.com.enonic.gradle/xp-gradle-plugin

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";

代码示例来源:origin: org.opennms.gizmo/gizmo-utils

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;
    }
  }
}

代码示例来源:origin: com.arm.mbed.cloud.sdk/iam

@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);
  }
}

代码示例来源:origin: biezhi/java-library-examples

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());
  }
}

代码示例来源:origin: prestodb/presto

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());
}

代码示例来源:origin: apache/nifi

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;
}

代码示例来源:origin: square/okhttp

BasicAuthInterceptor(String host, String username, String password) {
 this.credentials = Credentials.basic(username, password);
 this.host = host;
}

代码示例来源:origin: apache/nifi

@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();
  }
}

代码示例来源:origin: io.fabric8/openshift-client

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;
  }
}

代码示例来源:origin: SonarSource/sonarqube

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();

代码示例来源:origin: gradle.plugin.com.enonic.xp/xp-gradle-plugin

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";

代码示例来源:origin: com.arm.mbed.cloud.sdk/mds

@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);
  }
}

代码示例来源:origin: dyc87112/spring-cloud-config-admin

@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();
}

代码示例来源:origin: fabric8io/kubernetes-client

@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))

代码示例来源:origin: org.scijava/scijava-io-http

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());

代码示例来源:origin: square/okhttp

/** Returns an auth credential for the Basic scheme. */
public static String basic(String username, String password) {
 return basic(username, password, ISO_8859_1);
}

相关文章

Credentials类方法