use of org.apache.ws.security.WSUsernameTokenPrincipal in project nhin-d by DirectProject.
the class WSAuthenticationInInterceptor method handleMessage.
@SuppressWarnings("unchecked")
@Override
public /**
* Extract the username/password from the incoming message,
* validate it, and store the user context where CXF can get at it.
*/
void handleMessage(SoapMessage message) throws Fault {
try {
// Let the WSS4J parent do it's thing first
super.handleMessage(message);
Vector<WSHandlerResult> results = (Vector<WSHandlerResult>) message.getContextualProperty(WSHandlerConstants.RECV_RESULTS);
if (results != null && !results.isEmpty()) {
for (WSHandlerResult result : results) {
// loop through security engine results
for (WSSecurityEngineResult securityResult : (Vector<WSSecurityEngineResult>) result.getResults()) {
int action = (Integer) securityResult.get(WSSecurityEngineResult.TAG_ACTION);
// Was this a usernametoken
if (action == WSConstants.UT) {
WSUsernameTokenPrincipal principal = (WSUsernameTokenPrincipal) securityResult.get(WSSecurityEngineResult.TAG_PRINCIPAL);
if (principal.getPassword() == null) {
principal.setPassword("");
}
Authentication auth = new UsernamePasswordAuthenticationToken(principal.getName(), principal.getPassword());
auth = getAuthenticationManager().authenticate(auth);
if (auth.isAuthenticated()) {
_log.info("Authentication succeeds for request: User: " + principal.getName());
} else {
_log.warn("Authentication failed for request: User: " + principal.getName());
}
SecurityContextHolder.getContext().setAuthentication(auth);
}
}
}
}
} catch (RuntimeException ex) {
_log.error("Runtime Exception caught:", ex);
SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(null, null));
}
}
Aggregations