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,
  1. MessageContext msgCtx = MessageContext.getCurrentMessageContext();  
  2.   
  3. Vector results = (Vector) msgCtx  
  4.       .getProperty(WSHandlerConstants.RECV_RESULTS);  
  5.   
  6. if (results != null) {  
  7.   
  8.   WSHandlerResult result = (WSHandlerResult) results.get(0);  
  9.   WSSecurityEngineResult wsSecurityEngineResult =  
  10.       (WSSecurityEngineResult) result.getResults().get(1);  
  11.   String username =  
  12.       ((Principal) wsSecurityEngineResult.get(WSSecurityEngineResult.TAG_PRINCIPAL))  
  13.                   .getName();  
  14. }  

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.