Back Forum Reply New

How do I stuff information from a servlet into a Hibernate Interceptor?

Lemme clarify...For auditting purposes, we want to include the username of the logged in user on certain data records that are changed. ie, if I update a Dog record, we want Dog.updateBy() to be set to quot;tremelunequot; in the database. The only layer that knows about who is logged in is the servlet layer (stored in from session). Is there a way I can automagically get the logged in username to any DAOs doing the updates? I would rather not explicitly pass the username around, as it will involve more code and room for error. ie, I don't want to do this:

dogHandler.saveDog( dog, username );

The way I've done it in the past was to pass in a username when the session is created...In a main controller servlet, I would have something like this (session-per-request):

sessionFactory.createSession( new AuditorInterceptor( getLoggedInUsername() ) );

How can I get info from a servlet into Spring such that it will inject an interceptor with this information? For the record, I'm using Wicket for the web tier.

To be clear: The username could change per-request, so the info would have to be injected at runtime for each request, before the Hibernate session is opened.

You could use a request scoped bean.  However, I would go for a Servlet Filter and a Thread Local variable.

You're saying jump over Spring entirely with a request filter and thread local?


Originally Posted by TremeluneYou're saying jump over Spring entirely with a request filter and thread local?

I wouldn't say it's jumping over Spring.  If you look at Acegi security it does a similar thing with the Authentication credentials.
¥
Back Forum Reply New