Merge pull request #7753 from binary-joe/master

BAEL-2976 http basic auth
This commit is contained in:
rpvilao 2019-09-22 10:26:07 +02:00 committed by GitHub
commit 925b93b91c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 133 additions and 1 deletions

View File

@ -1,5 +1,5 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-networking-2</artifactId>
<name>core-java-networking-2</name>
@ -12,6 +12,11 @@
</parent>
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${httpclient.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
@ -29,6 +34,7 @@
</build>
<properties>
<httpclient.version>4.5.9</httpclient.version>
<javax.mail.version>1.5.0-b01</javax.mail.version>
</properties>
</project>

View File

@ -0,0 +1,74 @@
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 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() {
String auth = user + ":" + password;
byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(StandardCharsets.UTF_8));
String authHeaderValue = "Basic " + new String(encodedAuth);
return authHeaderValue;
}
private void setAuthenticator() {
Authenticator.setDefault(new BasicAuthenticator());
}
private final class BasicAuthenticator extends Authenticator {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password.toCharArray());
}
}
}

View File

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