0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Basic Authentication with Java 11 HttpClient

Posted at

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.

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.

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.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.
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.

Related blog:

Best Java training in chennai

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?