tags:

views:

35

answers:

1

Hello, in our app we use JSF & EJB 3.0 (EclipseLink 2.0). We need to use Oracle proxy authorization for every connection in our stateless session beans. For this we need to obtain DB username to connect throught proxy. DB username is constructed from JSF authenticated username by a rule.

Here is an article on topic http://blogs.oracle.com/olaf/2010/04/using_oracle_proxy_authenticat.html

So when an authenticated user calls a JSF managed bean method which in it's turn calls some session bean method his username must be somehow passed to session bean.

Right now I have two not very good solutions: - pass username as a parameter in every session bean method (not very neat but will work); - the solution from the article above: to use session bean's class member variable to store username (not thread safe and potentially dangerous)

P.S. I've found solution which is using thread local variables: _http://www.adam-bien.com/roller/abien/entry/how_to_pass_context_with It works fine, but there is still an issue. I need to put current username in every jsf session managed bean before every call to ejb session bean because it must be in the same thread.

A: 

If your users are authenticated by the container, you can access the username in the ejb by using an injected instance of javax.ejb.SessionContext like this:

@Resource
private SessionContext context;

private String getCurrentUsername() {
    return context.getCallerPrincipal().getName();
}

Edit: This works if the authentication is configured in web.xml via login-config and security-constraint elements. If you do the authentication yourself, you could use a ServletFilter and a HttpServletRequestWrapper that overrides getUserPrincipal, getRemoteUser and isUserInRole (tested in glassfish).

If you handle the authentication in a jsf bean then this probably would not work, since the ejb will already initialized and injected. You could however also create a session scoped ejb that just holds the username and inject this bean into your stateless beans.

Jörn Horstmann
Thank a lot, Jörn. Your solution seems to me very neat.P.S. Can you tell me when SessionContext will not contain current web layer username?
Andrey