Thursday, September 25, 2008

Accessing the username used for Authentication (Username Token / Rampart) with in a web service in Apache Axis2

Sometimes we need to access the user name used for authentication with in a web service for auditing purposes, authorization and etc. As Rampart stores all the security related data in the Message context one can get the username used for authentication parsing those data but then you need to do lot of work and have to have an idea about the structure of those data. Users needed a much easier way to do this and feature request was made in the Apache Rampart JIRA. Now the username is stored as a Message Context property by Rampart module and can be easily obtained withing the service using following code


import org.apache.rampart.RampartMessageData;

public void myMethod() {

// Getting the current MessageContext
MessageContext msgContext = MessageContext.getCurrentMessageContext();

// Getting HttpServletRequest from Message Context
String username = (String)msgContext
.getProperty(RampartMessageData.USERNAME);

}

0 comments: