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!
Related
I'm new to Elastic search. Integrated my Spring boot application with Elastic search through Java High Level Rest Client.
I've configured JHLRC bean as below and it worked fine:
#Bean(destroyMethod = "close")
public RestHighLevelClient client() {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http")));
return client;
}
Started exploring the security for Elasticsearch, after setup certificate and passwords, I've enabled security by providing below properties :
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true
xpack.security.transport.ssl.verification_mode: certificate
xpack.security.transport.ssl.keystore.path: elastic-certificates.p12
xpack.security.transport.ssl.truststore.path: elastic-certificates.p12
I'm able to login in kibana by using a created username and password but getting 401 Unauthorized while hitting any Elastic search API through JHLRC.
Can someone please help me on what further changes I've to make while configuring Java High Level Rest Client to hit secure Elastic search?
It worked after making below changes in JHLRC:
#Bean(destroyMethod = "close")
public RestHighLevelClient client() {
final BasicCredentialsProvider basicCredentialsProvider = new BasicCredentialsProvider();
basicCredentialsProvider
.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("elastic", "password_generated_by_elastic_search"));
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http"))
.setHttpClientConfigCallback(new HttpClientConfigCallback() {
#Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
httpClientBuilder.disableAuthCaching();
return httpClientBuilder.setDefaultCredentialsProvider(basicCredentialsProvider);
}
})
);
return restHighLevelClient;
}
You need to include the Basic credentials which you are giving while accessing the kibana, below code shows you can pass the username and password in JHLRC.
First, create the encoded string from your username and password, you can use the superuser elastic which has all the access by using the below code.
private String getEncodedString(String username, String password) {
return HEADER_PREFIX + Base64.getEncoder().encodeToString(
(username + ":" + password)
.getBytes());
}
Now in your request option, you pass the auth header which will include the base 64 encoded string which you will get from the above method.
RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder()
.addHeader(AUTH_HEADER_NAME, getEncodedString(basicCredentials));
Last, you just need to build the object of above requestion options builder and pass it to your client in any request like below:
GetResponse getResponse = restHighLevelClient.get(getRequest, builder.build());
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.
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());
}
});
}
I want to create my DbxRequestConfig Object with a StandardHttpRequestor, because I need it to use a Proxy.
The Proxy is a http Proxy, Port 80, and needs authentication.
Proxyaddress: http://myproxy.com
Proxyport: 80
Proxyusername: username
Proxypassword: password
So I tried to use the global Java Proxy setup:
System.setProperty("http.proxy","proxyaddress") //... http.proxyUser, http.ProxyPassword
//and so on
It did not work.
After looking into the StandardHttpRequestor I realized I need to use this Object as well as a Proyx Object:
Proxy proxy = new Proxy(Proxy.Type.HTTP,new InetSocketAddress(ip,port));
StandardHttpRequestor requ = new StandardHttpRequestor(proxy);
Which is wrong, because it has no authentication.
For authentication, the net and google show me the following. Putting all together, my current code looks like the following:
String ip = "http://myproxy.com";
int port = 80;
final String authUser = "username";
final String authPassword = "password";
Authenticator.setDefault(new Authenticator() {
#Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(authUser, authPassword.toCharArray());
}
});
System.setProperty("http.proxyUser", authUser);
System.setProperty("http.proxyPassword", authPassword);
Proxy proxy = new Proxy(Proxy.Type.HTTP,new InetSocketAddress(ip,port));
StandardHttpRequestor requ = new StandardHttpRequestor(proxy);
return requ;
But this does not work as well.
What am I doing wrong?
I can't seem to get the Proxy to work.
One problem was the http:// in String ip = "http://myproxy.com";
My current code looks the following, and works sometimes. Sometimes not. I have no idea why. Sometimes I have to reallow the App to be connected to my DropBox Account, because the authKey doesn't come through the proxy...
Well at least I got an example working for you guys having the same trouble. Maybe the rest of the problem is on the proxy side? I'll have a further look into this. But here comes my code:
public HttpRequestor getProxy(){
if("true".equals(config.getProperty("proxy","false"))){
String ip = "proxy.myproxy.com";
int port = 80;
final String authUser = "username";
final String authPassword = "password";
Authenticator.setDefault(new Authenticator() {
#Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(authUser, authPassword.toCharArray());
}
});
Proxy proxy = new Proxy(Proxy.Type.HTTP,new InetSocketAddress(ip,port));
HttpRequestor req = new StandardHttpRequestor(proxy);
return req;
}
return null;
}
As you can see I don't use the StandardHttpRequestor anymore. For the Dropbox code it is the following:
HttpRequestor requ = con.getProxy();
if(requ!=null)
config = new DbxRequestConfig(APP_NAME, Locale.getDefault().toString(),requ);
else
config = new DbxRequestConfig(APP_NAME, Locale.getDefault().toString());
As I already said, sometimes it works. Sometimes not. I'm going to write more info about that as soon as I know if it's because of the code or because of the proxy itself.
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