Reading HTTPS page with username/password authentication and ignore certifiacte - java

I need to read some data from HTTPS page that has a local certificate and requires a username/password authentication.
Using "HttpURLConnection", is it possible to do username/password authentication and import the local certificate or ignore it?
Many Thanks,

try to use this. Work for me.
private void authenticadorRedeTendencia() {
Authenticator.setDefault(new Authenticator() {
#Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName(), password.toCharArray());
}
});
}

Related

NTLM Proxy Authentication with Axis 1.4 client

I need to add following functionality to a legacy Java client that uses Axis 1.4 to access web services:
The client should route all http requests (Axis web service requests) through a proxy that accepts NTLM authentication.
I have configured the proxy as follows:
System.setProperty("http.proxyUser", configuration.getUsername());
System.setProperty("http.proxyPassword", configuration.getPassword());
System.setProperty("https.proxyUser", configuration.getUsername());
System.setProperty("https.proxyPassword", configuration.getPassword());
This is working fine for basic authentication, but fails on my proxy with NTLM authentication.
Is is possible to configure Axis 1.4 to authenticate with NTLM? And if so, how would I go about this? After hours of research, I feel clueless as to how to approach this.
I have tried to set a custom Authenticator like this, but sadly to no effect.
Authenticator.setDefault(
new NtlmAuthenticator(configuration.getUsername(), configuration.getPassword())
);
Where NtlmAuthenticator looks like this:
class NtlmAuthenticator extends Authenticator {
private final String username;
private final char[] password;
public NtlmAuthenticator(final String username, final String password) {
super();
this.username = username;
this.password = password.toCharArray();
}
public PasswordAuthentication getPasswordAuthentication() {
return (new PasswordAuthentication(username, password));
}
}
I would greatly appreciate any help that points me in the right direction!

Proxy authenticator auntenticates HTTP But not HTTPS . Why?

I am using openjdk . For porxy authentication ,I am using Authenticator but for my first Request as HTTPS the authenticators doesn't authenticates and throws error. After connecting through HTTP , HTTPS working fine.
I have tried with setting system property, jdk.http.auth.tunneling.disabledSchemes="" and jdk.http.auth.proxying.disabledSchemes="".
private static void setProxy(String proxyHostName,int proxyport){
proxy=new Proxy(Proxy.Type.HTTP,new InetSocketAddress(proxyHostName,proxyport));
}
private static void setProxy(String proxyHostName,int proxyport,String username,String password){
setProxy(proxyHostName,proxyport);
if (username!=null && password!=null) {
System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");
System.setProperty("jdk.http.auth.proxying.disabledSchemes", "");
Authenticator authenticator = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return (new PasswordAuthentication(username, password.toCharArray()));
}
};
Authenticator.setDefault(authenticator);
}
}
After setting this property in my java_opts in .sh file . It worked fine for me.
Orelse Anyone want to set in their java code means set it in start method.

Proxy Authentication with JDK 11 HttpClient

I'm trying to use JDK 11 HttpClient to make requests through a corporate proxy which requires authentication by login and password. According to JDK's intro, I'm building an instance of client by means of:
HttpClient httpClient = HttpClient.newBuilder()
.version(HTTP_1_1)
.proxy(ProxySelector.of(new InetSocketAddress("proxy.mycompany.com", 3128)))
.authenticator(authenticator)
.build();
, where authenticator is:
Authenticator authenticator = new Authenticator() {
#Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("login", "password".toCharArray());
}
};
And then I execute the request itself:
HttpRequest outRequest = HttpRequest.newBuilder()
.version(HTTP_1_1)
.GET()
.uri(URI.create("https://httpwg.org/asset/http.svg")) // no matter which URI to request
.build();
HttpResponse<String> inResponse = httpClient.send(outRequest, BodyHandlers.ofString());
But instead of valid response from the target server (https://httpwg.org) I receive HTTP 407 (Proxy Authentication Required), i.e. HttpClient does not use the provided authenticator.
I've tried various solutions mentioned here and here but none of them helped.
What is the correct way to make it work?
You have to set the "Proxy-Authorization" header on the request.
HttpClient httpClient = HttpClient.newBuilder()
.version(HTTP_1_1)
.proxy(ProxySelector.of(new InetSocketAddress("proxy.mycompany.com", 3128)))
.build();
String encoded = new String(Base64.getEncoder().encode("login:password".getBytes()));
HttpRequest outRequest = HttpRequest.newBuilder()
.version(HTTP_1_1)
.GET()
.uri(URI.create("https://httpwg.org/asset/http.svg")) // no matter which URI to request
.setHeader("Proxy-Authorization", "Basic " + encoded)
.build();
By default, basic authentication with the proxy is disabled when tunneling through an authenticating proxy since java 8u111.
You can re-enable it by specifying -Djdk.http.auth.tunneling.disabledSchemes="" on the java command line.
See the jdk 8u111 release notes
Use
System.setProperty("jdk.http.auth.proxying.disabledSchemes", "");
System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");
Construct your http client with proxy selector and authenticator
HttpClient client = HttpClient.newBuilder()
.authenticator(yourCustomAuthenticator)
.proxy(yourCustomProxySelector)
.build();
In your Authenticator override
#Override
protected PasswordAuthentication getPasswordAuthentication() {
if (getRequestorType().equals(RequestorType.PROXY)) {
return getPasswordAuthentication(getRequestingHost());
} else {
return null;
}
}
protected PasswordAuthentication getPasswordAuthentication(String proxyHost) {
// your logic to find username and password for proxyHost
return new PasswordAuthentication(proxyUsername, proxyPassword.toCharArray());
// or return null
}

How do I retrieve credential passed by user in soap wsdl Java Webservice

I am sending my credentials using,
Authenticator.setDefault(new Authenticator() {
#Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("username", "password".toCharArray());
}
});
My question is now how can I retrieve this credentials in Soap webservice Java?
Got solution,
ArrayList<String> authList = (ArrayList<String>) http_headers.get("Authorization");
It returns Credentials in Base64.

Getting error in java mail api PasswordAuthentication method

I am trying to send mail using java mail api..Every thing is ok in the code except the Authenticator class .It is giving warning as ...
Constructor PasswordAuthentication can not be applied to given types.
required java.lang.String,java.lang.char[]
Here is my code snippet where i am getting warning error but not able to resolve the issue...
Authenticator auth = new Authenticator() {
#Override
public PasswordAuthentication getPasswordAuthentication() {
error return new PasswordAuthentication(userName, password);
}
};
error Session session = Session.getInstance(properties, auth);
These two lines with //error is giving error in the code..
Please help me.
Thanks in advance..
PasswordAuthentication constructor only accept a String and a char array as arguments. So you should do :
return new PasswordAuthentication(userName, password.toCharArray());
Edit :
The problem is that Session.getInstance(java.util.Properties props, Authenticator authenticator) requires an Authentificator object from the javax.mail package.
I think you've imported the wrong package. It should be javax.mail.Authenticator and not java.net.Authenticator
So you should use the object PasswordAuthentication from the javax.mail package (which accept two Strings as argument), instead of the object PasswordAuthentification from the java.net package (which accept a String and a char array).
when you call the constructor PasswordAuthentication(String a, char[] b)
the Exception is telling you that you are passing a wrong type in the parameters, for example:
your code: return new PasswordAuthentication(userName, password);
userName or password are wrong type, maybe userName is not a String or password is not a char[], take a look carefully.
Try
Session session = Session.getInstance(properties,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
If you are using gmail account, you must disable the two step authentication or you can either set on your gmail account app password and use the app password instead in your application.
https://support.google.com/accounts/answer/185833?hl=en

Categories