Basic Authentication with Java 11 HttpClient

ashok mathan
Nov 4 · 2 min read

In this article, we will create Java 11 HttpClient that accesses Basic Auth protected REST API resource using sync and async mode. We will use Kotlin for a reference implementation.

Spring Boot 2 based Basic Auth Server

You can directly download the Basic Auth Server from Github Repository and run it locally using the below command.

Starting the server using Gradle

$ ./gradlew bootRun

Server will expose http://localhost:8080/api/health endpoint, which can be tested using the below curl command.

$ curl -i --user admin:password -X GET http://localhost:8080/api/health

Java 11 HttpClient

Java 11 HttpClient supports Basic Authentication using authenticator.

We can use either send or sendAsync api for making synchronous and asynchronous (fully non-blocking) requests.

HttpClient basic authentication — sync client

fun basicAuthSync() {
val httpClient: HttpClient = HttpClient.newBuilder()
.connectTimeout(Duration.ofSeconds(10))
.authenticator(object : Authenticator() {
override fun getPasswordAuthentication(): PasswordAuthentication {
return PasswordAuthentication("admin", "password".toCharArray())
}
})
.version(HttpClient.Version.HTTP_1_1)
.build()
val request = HttpRequest.newBuilder()
.GET()
.uri(URI.create("http://localhost:8080/api/health"))
.build()
val httpResponse = httpClient.send(request, BodyHandlers.ofString())
println("httpResponse statusCode = ${httpResponse.statusCode()}")
println(httpResponse.body())
}

PasswordAuthentication is configured for handling HTTP Basic Authentication.

HttpClient — async client

httpClient.sendAsync(request, BodyHandlers.ofString())
.thenApply(HttpResponse<String>::body)
.thenAccept(System.out::println)
.join()

Server Response

httpResponse statusCode = 200
{"status":"UP"}

What's new in Spring Boot 2

Spring boot 2 is liberated from legacy baggage of deprecated Java releases. Java 8 is the minimum baseline, with Java 9 support. Under the hood Spring Boot, 2 uses Spring 5. Refer Spring boot interview questions

Reactive web programming support with Spring Webflux.

Auto-configuration and starter POMs for reactive Spring Data Cassandra, MongoDB, Couchbase and Redis.

Supports embedded Netty, along with HTTP/2 support for Tomcat, Undertow, and Jetty

Kotlin we supported in Spring Boot 2, along with Junit 5. Now you can write Spring Boot 2 application from scratch in Kotlin.

A brand new actuator architecture, with support for Spring MVC, WebFlux and Jersey.

Simplified security auto-configuration.

Uses Spring Framework 5 under the hood.

Gradle 4 required, works well with Gradle 5.

ashok mathan

Written by

http://candidjava.com/training/advanced-java-training-in-chennai/

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade