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.
Related
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!
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
I have WSDL. I need to make HTTP basic (preemptive) authentication.
What to do?
I tried :
Authenticator myAuth = new Authenticator() {
#Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("user", "pass".toCharArray());
}
};
Authenticator.setDefault(myAuth);
But it does not work: Caused by:
java.io.IOException: Server returned HTTP response code: 401 for URL ..
P.S. I use Apache CXF 2.6.2 and JBoss 5.0.1
What you specified for your authentication is not enough. You should do something like this:
private YourService proxy;
public YourServiceWrapper() {
try {
final String username = "username";
final String password = "password";
Authenticator.setDefault(new Authenticator() {
#Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
username,
password.toCharArray());
}
});
URL url = new URL("http://yourserviceurl/YourService?WSDL");
QName qname = new QName("http://targetnamespace/of/your/wsdl", "YourServiceNameInWsdl");
Service service = Service.create(url, qname);
proxy = service.getPort(YourService.class);
Map<String, Object> requestContext = ((BindingProvider) proxy).getRequestContext();
requestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, url.toString());
requestContext.put(BindingProvider.USERNAME_PROPERTY, username);
requestContext.put(BindingProvider.PASSWORD_PROPERTY, password);
Map<String, List<String>> headers = new HashMap<String, List<String>>();
requestContext.put(MessageContext.HTTP_REQUEST_HEADERS, headers);
} catch (Exception e) {
LOGGER.error("Error occurred in web service client initialization", e);
}
}
Properties:
YourService - your generated web service client interface.
YourServiceWrapper() - wrapper class constructor which initializes your service.
url - URL to your web service with ?WSDL extension.
qname - first constructor argument: target namespace from your WSDL file. Second: your service name from WSDL.
Then you will be able to call your web service methods like this:
proxy.whatEverMethod();