BAEL-2976 http basic auth
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user