Friday, September 26, 2008

Axis2 & Rampart - Access Username used for Authentication in a Web Service

Nandana in his blog post has explained how you could do this (Axis2 & Rampart - Access Username used for Authentication in a Web Service) with the latest Rampart code. But if you are using Rampart 1.4 that method will not work. Those users can use the following method,

MessageContext msgCtx = MessageContext.getCurrentMessageContext();

Vector results = (Vector) msgCtx
.getProperty(WSHandlerConstants.RECV_RESULTS);

if (results != null) {

WSHandlerResult result = (WSHandlerResult) results.get(0);
WSSecurityEngineResult wsSecurityEngineResult =
(WSSecurityEngineResult) result.getResults().get(1);
String username =
((Principal) wsSecurityEngineResult.get(WSSecurityEngineResult.TAG_PRINCIPAL))
.getName();
}

2 comments:

Pablo said...

In case anyone else is trying this out, I needed tp change the following line slightly
result.getResults().get(1);
So that it used index 0.
result.getResults().get(0);

Anonymous said...
This comment has been removed by a blog administrator.