okhttp3.Credentials.basic()方法的使用及代码示例

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

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

Credentials.basic介绍

[英]Returns an auth credential for the Basic scheme.
[中]返回基本方案的身份验证凭据。

代码示例

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

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

代码示例来源: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);
}

代码示例来源:origin: com.squareup.okhttp3/okhttp

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

代码示例来源: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: Graylog2/graylog2-server

ProxyAuthenticator(String user, String password) {
  this.credentials = Credentials.basic(requireNonNull(user, "user"), requireNonNull(password, "password"));
}

代码示例来源: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: 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: 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: apache/nifi

@Override
  public Request authenticate(Route route, Response response) throws IOException {
    final String credential=Credentials.basic(proxyConfig.getProxyUserName(), proxyConfig.getProxyUserPassword());
    return response.request().newBuilder()
        .header("Proxy-Authorization", credential)
        .build();
  }
});

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

private static Request buildHttpRequest(HttpUrl url, WebhookPayload payload) {
 Request.Builder request = new Request.Builder();
 request.url(url);
 request.header(PROJECT_KEY_HEADER, payload.getProjectKey());
 if (isNotEmpty(url.username())) {
  request.header("Authorization", Credentials.basic(url.username(), url.password(), UTF_8));
 }
 RequestBody body = RequestBody.create(JSON, payload.getJson());
 request.post(body);
 return request.build();
}

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

String credential = Credentials.basic(
  auth.getUserName(), new String(auth.getPassword()), challenge.charset());
return request.newBuilder()

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

private void setProxy(OkHttpClient.Builder builder) {
  ProxyConfiguration config = proxyConfigurationService.getConfiguration();
  if (!config.getProxyType().equals(Proxy.Type.DIRECT)) {
    final Proxy proxy = config.createProxy();
    builder.proxy(proxy);
    if (config.hasCredential()){
      builder.proxyAuthenticator((route, response) -> {
        final String credential= Credentials.basic(config.getProxyUserName(), config.getProxyUserPassword());
        return response.request().newBuilder()
            .header("Proxy-Authorization", credential)
            .build();
      });
    }
  }
}

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

private HttpConnector(Builder builder) {
 this.baseUrl = HttpUrl.parse(builder.url.endsWith("/") ? builder.url : format("%s/", builder.url));
 checkArgument(this.baseUrl != null, "Malformed URL: '%s'", builder.url);
 OkHttpClientBuilder okHttpClientBuilder = new OkHttpClientBuilder();
 okHttpClientBuilder.setUserAgent(builder.userAgent);
 if (!isNullOrEmpty(builder.login)) {
  // password is null when login represents an access token. In this case
  // the Basic credentials consider an empty password.
  okHttpClientBuilder.setCredentials(Credentials.basic(builder.login, nullToEmpty(builder.password), UTF_8));
 }
 this.systemPassCode = builder.systemPassCode;
 okHttpClientBuilder.setProxy(builder.proxy);
 okHttpClientBuilder.setProxyLogin(builder.proxyLogin);
 okHttpClientBuilder.setProxyPassword(builder.proxyPassword);
 okHttpClientBuilder.setConnectTimeoutMs(builder.connectTimeoutMs);
 okHttpClientBuilder.setReadTimeoutMs(builder.readTimeoutMs);
 okHttpClientBuilder.setSSLSocketFactory(builder.sslSocketFactory);
 okHttpClientBuilder.setTrustManager(builder.sslTrustManager);
 this.okHttpClient = okHttpClientBuilder.build();
 this.noRedirectOkHttpClient = newClientWithoutRedirect(this.okHttpClient);
}

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

@Test
public void send_basic_authentication_header_if_url_contains_credentials() throws Exception {
 HttpUrl url = server.url("/ping").newBuilder().username("theLogin").password("thePassword").build();
 Webhook webhook = new Webhook(WEBHOOK_UUID, PROJECT_UUID, CE_TASK_UUID, RandomStringUtils.randomAlphanumeric(40),"my-webhook", url.toString());
 server.enqueue(new MockResponse().setBody("pong"));
 WebhookDelivery delivery = newSender().call(webhook, PAYLOAD);
 assertThat(delivery.getWebhook().getUrl())
  .isEqualTo(url.toString())
  .contains("://theLogin:thePassword@");
 RecordedRequest recordedRequest = takeAndVerifyPostRequest("/ping");
 assertThat(recordedRequest.getHeader("Authorization")).isEqualTo(Credentials.basic(url.username(), url.password()));
}

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

@Test
public void credentials_are_propagated_to_POST_redirects() throws Exception {
 HttpUrl url = server.url("/redirect").newBuilder().username("theLogin").password("thePassword").build();
 Webhook webhook = new Webhook(WEBHOOK_UUID, PROJECT_UUID, CE_TASK_UUID, RandomStringUtils.randomAlphanumeric(40),"my-webhook", url.toString());
 // /redirect redirects to /target
 server.enqueue(new MockResponse().setResponseCode(307).setHeader("Location", server.url("target")));
 server.enqueue(new MockResponse().setResponseCode(200));
 WebhookDelivery delivery = newSender().call(webhook, PAYLOAD);
 assertThat(delivery.getHttpStatus().get()).isEqualTo(200);
 RecordedRequest redirectedRequest = takeAndVerifyPostRequest("/redirect");
 assertThat(redirectedRequest.getHeader("Authorization")).isEqualTo(Credentials.basic(url.username(), url.password()));
 RecordedRequest targetRequest = takeAndVerifyPostRequest("/target");
 assertThat(targetRequest.getHeader("Authorization")).isEqualTo(Credentials.basic(url.username(), url.password()));
}

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

@Test
public void use_basic_authentication_with_null_password() throws Exception {
 answerHelloWorld();
 underTest = HttpConnector.newBuilder()
  .url(serverUrl)
  .credentials("theLogin", null)
  .build();
 GetRequest request = new GetRequest("api/issues/search");
 underTest.call(request);
 RecordedRequest recordedRequest = server.takeRequest();
 assertThat(recordedRequest.getHeader("Authorization")).isEqualTo(basic("theLogin", ""));
}

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

@Test
public void use_basic_authentication() throws Exception {
 answerHelloWorld();
 underTest = HttpConnector.newBuilder()
  .url(serverUrl)
  .credentials("theLogin", "thePassword")
  .build();
 GetRequest request = new GetRequest("api/issues/search");
 underTest.call(request);
 RecordedRequest recordedRequest = server.takeRequest();
 assertThat(recordedRequest.getHeader("Authorization")).isEqualTo(basic("theLogin", "thePassword"));
}

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

/**
 * Access token replaces the couple {login,password} and is sent through
 * the login field
 */
@Test
public void use_access_token() throws Exception {
 answerHelloWorld();
 underTest = HttpConnector.newBuilder()
  .url(serverUrl)
  .token("theToken")
  .build();
 GetRequest request = new GetRequest("api/issues/search");
 underTest.call(request);
 RecordedRequest recordedRequest = server.takeRequest();
 assertThat(recordedRequest.getHeader("Authorization")).isEqualTo(basic("theToken", ""));
}

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

@Test
public void use_proxy_authentication_wrong_crendentials() throws Exception {
 try (MockWebServer proxy = new MockWebServer()) {
  proxy.start();
  underTest = HttpConnector.newBuilder()
   .url(serverUrl)
   .proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxy.getHostName(), proxy.getPort())))
   .proxyCredentials("theProxyLogin", "wrongPassword")
   .build();
  GetRequest request = new GetRequest("api/issues/search");
  proxy.enqueue(new MockResponse().setResponseCode(407));
  proxy.enqueue(new MockResponse().setResponseCode(407));
  proxy.enqueue(new MockResponse().setResponseCode(407));
  underTest.call(request);
  RecordedRequest recordedRequest = proxy.takeRequest();
  assertThat(recordedRequest.getHeader("Proxy-Authorization")).isNull();
  recordedRequest = proxy.takeRequest();
  assertThat(recordedRequest.getHeader("Proxy-Authorization")).isEqualTo(basic("theProxyLogin", "wrongPassword"));
  assertThat(proxy.getRequestCount()).isEqualTo(2);
 }
}

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

@Test
public void use_proxy_authentication() throws Exception {
 try (MockWebServer proxy = new MockWebServer()) {
  proxy.start();
  underTest = HttpConnector.newBuilder()
   .url(serverUrl)
   .proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxy.getHostName(), proxy.getPort())))
   .proxyCredentials("theProxyLogin", "theProxyPassword")
   .build();
  GetRequest request = new GetRequest("api/issues/search");
  proxy.enqueue(new MockResponse().setResponseCode(407));
  proxy.enqueue(new MockResponse().setBody("OK!"));
  underTest.call(request);
  RecordedRequest recordedRequest = proxy.takeRequest();
  assertThat(recordedRequest.getHeader("Proxy-Authorization")).isNull();
  recordedRequest = proxy.takeRequest();
  assertThat(recordedRequest.getHeader("Proxy-Authorization")).isEqualTo(basic("theProxyLogin", "theProxyPassword"));
 }
}

相关文章

Credentials类方法