From 7da248c9dbe38c0f486f150fb98fd5f1f35d7d5d Mon Sep 17 00:00:00 2001 From: binary-joe Date: Thu, 22 Aug 2019 22:38:33 +0200 Subject: [PATCH 1/2] BAEL-2976 http basic auth --- .../core-java-networking-2/pom.xml | 13 +++- .../com/baeldung/url/auth/HttpClient.java | 72 +++++++++++++++++++ .../baeldung/url/auth/HttpClientUnitTest.java | 52 ++++++++++++++ 3 files changed, 135 insertions(+), 2 deletions(-) create mode 100644 core-java-modules/core-java-networking-2/src/main/java/com/baeldung/url/auth/HttpClient.java create mode 100644 core-java-modules/core-java-networking-2/src/test/java/com/baeldung/url/auth/HttpClientUnitTest.java diff --git a/core-java-modules/core-java-networking-2/pom.xml b/core-java-modules/core-java-networking-2/pom.xml index 8a26f6ab9f..bea832aa14 100644 --- a/core-java-modules/core-java-networking-2/pom.xml +++ b/core-java-modules/core-java-networking-2/pom.xml @@ -1,5 +1,5 @@ + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 core-java-networking-2 core-java-networking-2 @@ -12,9 +12,18 @@ + + org.apache.httpcomponents + httpclient + ${httpclient.version} + core-java-networking-2 - + + + + 4.5.9 + diff --git a/core-java-modules/core-java-networking-2/src/main/java/com/baeldung/url/auth/HttpClient.java b/core-java-modules/core-java-networking-2/src/main/java/com/baeldung/url/auth/HttpClient.java new file mode 100644 index 0000000000..dccaf3de84 --- /dev/null +++ b/core-java-modules/core-java-networking-2/src/main/java/com/baeldung/url/auth/HttpClient.java @@ -0,0 +1,72 @@ +package com.baeldung.url.auth; + +import java.io.IOException; +import java.net.Authenticator; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.PasswordAuthentication; +import java.net.ProtocolException; +import java.net.URL; +import java.nio.charset.StandardCharsets; + +import org.apache.commons.codec.binary.Base64; + +public class HttpClient { + + private final String user; + private final String password; + + public HttpClient(String user, String password) { + this.user = user; + this.password = password; + } + + public int sendRquestWithAuthHeader(String url) throws IOException { + HttpURLConnection connection = null; + try { + connection = createConnection(url); + connection.setRequestProperty("Authorization", createBasicAuthHeaderValue()); + return connection.getResponseCode(); + } finally { + if (connection != null) { + connection.disconnect(); + } + } + } + + public int sendRquestWithAuthenticator(String url) throws IOException { + setAuthenticator(); + + HttpURLConnection connection = null; + try { + connection = createConnection(url); + return connection.getResponseCode(); + } finally { + if (connection != null) { + connection.disconnect(); + } + } + } + + private void setAuthenticator() { + Authenticator.setDefault(new Authenticator() { + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication(user, password.toCharArray()); + } + }); + } + + private String createBasicAuthHeaderValue() { + String auth = user + ":" + password; + byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(StandardCharsets.UTF_8)); + String authHeaderValue = "Basic " + new String(encodedAuth); + return authHeaderValue; + } + + private HttpURLConnection createConnection(String urlString) throws MalformedURLException, IOException, ProtocolException { + URL url = new URL(String.format(urlString)); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("GET"); + return connection; + } +} diff --git a/core-java-modules/core-java-networking-2/src/test/java/com/baeldung/url/auth/HttpClientUnitTest.java b/core-java-modules/core-java-networking-2/src/test/java/com/baeldung/url/auth/HttpClientUnitTest.java new file mode 100644 index 0000000000..0ccb6e5a54 --- /dev/null +++ b/core-java-modules/core-java-networking-2/src/test/java/com/baeldung/url/auth/HttpClientUnitTest.java @@ -0,0 +1,52 @@ +package com.baeldung.url.auth; + +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +public class HttpClientUnitTest { + + @Test + public void sendRquestWithAuthHeader() throws Exception { + HttpClient httpClient = new HttpClient("user1", "pass1"); + + int status = httpClient.sendRquestWithAuthHeader("https://httpbin.org/basic-auth/user1/pass1"); + + assertTrue(isSuccess(status)); + } + + @Test + public void sendRquestWithAuthHeader_whenIncorrectCredentials_thenNotSuccessful() throws Exception { + HttpClient httpClient = new HttpClient("John", "Smith"); + + int status = httpClient.sendRquestWithAuthHeader("https://httpbin.org/basic-auth/user1/pass1"); + + assertTrue(isUnauthorized(status)); + } + + @Test + public void sendRquestWithAuthenticator() throws Exception { + HttpClient httpClient = new HttpClient("user2", "pass2"); + + int status = httpClient.sendRquestWithAuthenticator("https://httpbin.org/basic-auth/user2/pass2"); + + assertTrue(isSuccess(status)); + } + + @Test + public void sendRquestWithAuthenticator_whenIncorrectCredentials_thenNotSuccessful() throws Exception { + HttpClient httpClient = new HttpClient("John", "Smith"); + + int status = httpClient.sendRquestWithAuthenticator("https://httpbin.org/basic-auth/user2/pass2"); + + assertTrue(isUnauthorized(status)); + } + + private boolean isSuccess(int status) { + return (status >= 200) && (status < 300); + } + + private boolean isUnauthorized(int status) { + return status == 401; + } +} From 60c990b8c1e1df51203b8d592a39eea2ca439687 Mon Sep 17 00:00:00 2001 From: binary-joe Date: Sat, 14 Sep 2019 19:37:32 +0200 Subject: [PATCH 2/2] fix merge issue; refactoring --- .../core-java-networking-2/pom.xml | 2 ++ .../com/baeldung/url/auth/HttpClient.java | 24 ++++++++++--------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/core-java-modules/core-java-networking-2/pom.xml b/core-java-modules/core-java-networking-2/pom.xml index 3837b77eba..2d404a553b 100644 --- a/core-java-modules/core-java-networking-2/pom.xml +++ b/core-java-modules/core-java-networking-2/pom.xml @@ -16,6 +16,8 @@ org.apache.httpcomponents httpclient ${httpclient.version} + + org.apache.commons commons-lang3 ${commons-lang3.version} diff --git a/core-java-modules/core-java-networking-2/src/main/java/com/baeldung/url/auth/HttpClient.java b/core-java-modules/core-java-networking-2/src/main/java/com/baeldung/url/auth/HttpClient.java index dccaf3de84..779f8aa898 100644 --- a/core-java-modules/core-java-networking-2/src/main/java/com/baeldung/url/auth/HttpClient.java +++ b/core-java-modules/core-java-networking-2/src/main/java/com/baeldung/url/auth/HttpClient.java @@ -48,12 +48,11 @@ public class HttpClient { } } - private void setAuthenticator() { - Authenticator.setDefault(new Authenticator() { - protected PasswordAuthentication getPasswordAuthentication() { - return new PasswordAuthentication(user, password.toCharArray()); - } - }); + private HttpURLConnection createConnection(String urlString) throws MalformedURLException, IOException, ProtocolException { + URL url = new URL(String.format(urlString)); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("GET"); + return connection; } private String createBasicAuthHeaderValue() { @@ -63,10 +62,13 @@ public class HttpClient { return authHeaderValue; } - private HttpURLConnection createConnection(String urlString) throws MalformedURLException, IOException, ProtocolException { - URL url = new URL(String.format(urlString)); - HttpURLConnection connection = (HttpURLConnection) url.openConnection(); - connection.setRequestMethod("GET"); - return connection; + private void setAuthenticator() { + Authenticator.setDefault(new BasicAuthenticator()); + } + + private final class BasicAuthenticator extends Authenticator { + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication(user, password.toCharArray()); + } } }