Java Webservices : Authentication of WSI security

Posted by: admin in MLink Blog

Tagged in: webservice , java

admin

 

Java WebService Authentication Techniques:

 

Axis 1.4:

 Adding Authentication Create a file called Auth.java

public class Auth extends SOAPHeaderElement
{
public Auth(String namespace, String
localPart)
{
super(namespace, localPart);
}

protected void outputImpl(SerializationContext context) throws Exception
{ context.startElement(new QName(namespaceURI, name), attributes);
ArrayList children = super.getChildren();
if (children != null)
{
for (Iterator it = children.iterator(); it.hasNext();)
{
((MessageElement)it.next()).output(context);
}
}
context.endElement(); }

public static Auth setAuthHeader(String user, String pwd)
{ try {
Auth authHeader = new  Auth("","AuthenticationInfo");

MessageElement msgElem1 = new MessageElement("","userName");
//msgElem1.addTextNode("WebService-Reliability2");
msgElem1.addTextNode(user);
authHeader.addChildElement(msgElem1);

MessageElement msgElem2 = new MessageElement("","password");
//msgElem2.addTextNode("remedy");
msgElem2.addTextNode(pwd);
authHeader.addChildElement(msgElem2);

// Uncomment these sections if you need to send these login
//parameters to the AR Web Service

// MessageElement msgElem3 = new MessageElement("","authentication");

// msgElem3.addTextNode("ARSystem");
// authHeader.addChildElement(msgElem3);

// MessageElement msgElem4 = new MessageElement("","Locale");
// msgElem4.addTextNode("enUS");
// authHeader.addChildElement(msgElem4);

// MessageElement msgElem4 = new MessageElement("","timeZone");
// msgElem4.addTextNode("enUS");
// authHeader.addChildElement(msgElem4);

return authHeader;
}
catch (SOAPException e) {
e.printStackTrace();
}

return null;
}
 

}





Now open the Stub genetated file and add following lines after createCall if (super.cachedEndpoint == null) {
            throw new org.apache.axis.NoEndPointException();
        }
        org.apache.axis.client.Call _call = createCall();
        
           Auth aihdr =
            Auth.setAuthHeader(user,pwd);
            _call.addHeader(aihdr);

 

Axis 2.x:

 

 CXF:

  CredentialsManager mgr = BusFactory.getDefaultBus().getExtension(CredentialsManager.class);
            OutCredential cred = mgr.createOutCredential(CredentialType.USERNAME_PASSWORD, userId, password);
            OutCredentialsMap map = new OutCredentialsMap();
            map.get(SecurityProtocolType.SOAP).add(cred);
            Map<String, Object> requestContext = impl.getRequestContext();
            requestContext.put(OutCredentialsMap.class.getName(), map);




 

Comments (0)Add Comment

Write comment

busy