0

In the onMessage method of a MDB, how can I retrieve the name of the JAAS User Principal that initiated the request? My application is using Servlet login and a jdbcRealm.

I have considered adding this as a Message Property, but I would rather ensure that this is handled by JAAS.

Graham
  • 7,431
  • 18
  • 59
  • 84

1 Answers1

1

It is handled inherently, all that you have to do is to inject and use MessageDrivenContext:

..
@Resource
private MessageDrivenContext mdc;

public void onMessage(Message message) {
    Principal principal = mdc.getCallerPrincipal();
    System.out.println(principal.getName());
    //ALTERNATIVELY, YOU CAN USE A METHOD mdc.isCallerInRole("<role name>");
}
Miljen Mikic
  • 14,765
  • 8
  • 58
  • 66
  • You're welcome! Regarding documentation, you can find introduction about MDBs and example in the official Oracle documentation, but I agree, it can be more detailed definitely. – Miljen Mikic Nov 24 '12 at 15:19