How to configure Axis2 to support Basic, NTLM and Proxy authentication?
Submitted on June 12, 2006 - 04:19. Story :
•
Knowledge Base
Project :
•
Axis2 > Java Apache Axis2 has inbuilt support for Basic, NTLM and Proxy authentication via commons-httpclient-3.0. Basic and NTLM use preemptive authentication scheme of commons-httpclient.
Configuration for Proxy Authentication It has to be noted that Proxy Authentication via httpclient is available only with HTTP 1.0 version. Configuration for Proxy Authentication is available at deployment time (axis2.xml) or runtime.
1. Proxy Authentication at Deployment time In Axis2.xml, user will be able to find the following entry:
<parameter name="PROTOCOL" locked="false">HTTP/1.1 <parameter name="Transfer-Encoding" locked="false">chunked To enable proxy authentication:
<parameter name="PROTOCOL" locked="false">HTTP/1.0 <parameter name="PROXY" proxy_host="proxy_host_name" proxy_port="proxy_host_port" locked="true>userName:domain:passWord 2. Proxy Authenticaion at Runtime. In client or stub:
HttpTransportProperties.ProxyProperties proxyProperties = new HttpTransportProperties().new ProxyProperties();
proxyProperties.setProxyHostName("axis2"); proxyProperties.setProxyPort(9762); proxyproperties.setDomain("axis2/anon"); proxyproperties.setPassWord("passwd"); proxyproperties.setUserName("usernm"); //in order to makesure that we use HTTP 1.0 options.setProperty(MessageContextConstants.HTTP_PROTOCOL_VERSION, HTTPConstants.HEADER_PROTOCOL_10); options.setProperty(org.apache.axis2.transport.http.HTTPConstants.PROXY, proxyProperties); Basic and NTLM Authentication Axis2 uses commons-httpclient's preemptive authentication scheme to support Basic and NTLM Authentication.
1. To Enable Basic Authentication
HttpTransportProperties.BasicAuthentication basicAuthentication = new HttpTransportProperties().new BasicAuthentication(); basicAuthentication.setUsername("axis2"); basicAuthentication.setPassword("axis2"); options.setProperty( org.apache.axis2.transport.http.HTTPConstants.BASIC_AUTHENTICATION, basicAuthentication);
2. To Enable NTLM Authentication
HttpTransportProperties.NTLMAuthentication ntlmAuthentication = new HttpTransportProperties().new NTLMAuthentication(); ntlmAuthentication.setUsername("axis2"); ntlmAuthentication.setPassword("axis2"); ntlmAuthentication.setHost("axis2"); ntlmAuthentication.setPort(9443); ntlmAuthentication.setRealm("someRealm"); options.setProperty(
org.apache.axis2.transport.http.HTTPConstants.NTLM_AUTHENTICATION, ntlmAuthentication);
try { Service1Stub
spServer = new
Service1Stub("http://test001:9998/Service.asmx"); // NTLM Authentication List<String> authScheme = new ArrayList<String>(); authScheme.add(HttpTransportProperties.Authenticator.NTLM); HttpTransportProperties.Authenticator ntlm = new HttpTransportProperties.Authenticator(); ntlm.setAuthSchemes(authScheme); ntlm.setUsername("administrator"); ntlm.setPassword("1"); ntlm.setHost("test001"); ntlm.setPort(9998); ntlm.setDomain("sps2007"); Options options = spServer._getServiceClient().getOptions(); options.setProperty(HTTPConstants.AUTHENTICATE, ntlm); spServer._getServiceClient().setOptions(options); } catch (AxisFault e) { e.printStackTrace(); } // Base Authentication //
HttpTransportProperties.Authenticator authenticator = new
HttpTransportProperties.Authenticator(); //
List<String> authScheme = new ArrayList<String>();
//
authScheme.add(HttpTransportProperties.Authenticator.BASIC);
//
authenticator.setAuthSchemes(authScheme);
//
authenticator.setUsername("administrator");
//
authenticator.setPassword("1");
//
authenticator.setHost("test001");
//
authenticator.setDomain("sps2007");
//
authenticator.setPort(port);
// //
Options options = stub._getServiceClient().getOptions();
//
options.setProperty(HTTPConstants.AUTHENTICATE,
authenticator); //
stub._getServiceClient().setOptions(options);
//
PROXY Authentication
//
HttpTransportProperties.ProxyProperties proxyProperties
//
= new HttpTransportProperties.ProxyProperties();
//
proxyProperties.setProxyName("test001.sps2007.com");//test001
//
proxyProperties.setProxyPort(9998);
//
proxyProperties.setDomain("sps2007.com");
//
proxyProperties.setUserName("administrator");
//
proxyProperties.setPassWord("1");
//
Options options = stub._getServiceClient().getOptions();
//
options.setProperty(HTTPConstants.PROXY, proxyProperties);
//
stub._getServiceClient().setOptions(options);
//
public static void download(String url,String fileName){
// // //
//create an instance of HttpClient
// // ////
HttpClient httpClient = new HttpClient(); httpclient.getCredentialsProvider().setCredentials(
////
new AuthScope("myserver", -1),
////
new NTCredentials("username", "password",
"MYSERVER", "MYDOMAIN")); // //
NTCredentials c =
new NTCredentials("administrator", "1",
"10.10.0.227", "sps2007"); // ////
HttpState httpState = new HttpState(); httpState.setProxyCredentials(new
AuthScope("10.10.0.227",9998),n); //
httpState.setCredentials(new AuthScope("10.10.0.227",9998),
c); // // //
httpClient.setState(httpState);
//
//create a method instance
//
GetMethod getMethod = new GetMethod(url);
//
getMethod.setDoAuthentication(true);
//
try{
// //
//execute the method
//
int statusCode =
//
httpClient.executeMethod(getMethod);
// //
//TODO: check statusCode = 200
// //
//get the resonse as an InputStream
//
InputStream in =
//
getMethod.getResponseBodyAsStream();
// // // //
byte[] b = new byte[1024];
//
int len;
// //
FileOutputStream out = new
FileOutputStream("D:/dev/download/archive/data/"+fileName); //
while ((len = in.read(b)) != -1) {
//
//write byte to file
//
out.write(b, 0, len);
//
out.flush();
//
}
// //
out.close();
// ////
while ((len = in.read(b)) != -1) {
////
//write byte to System.out
////
for(int i = 0; i < len; i++){
////
System.out.print((char)b[i]);
//// ////
} }
// //
in.close();
// // //
}catch(HttpException e){ //do something
//
}catch(IOException e){
//
//do something else
//
}finally{
//
//release the connection
//
getMethod.releaseConnection();
// //
} }