-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Auditing with interceptors and EJB's
PostPosted: Sat Jun 03, 2006 7:30 am 
Newbie

Joined: Wed Feb 11, 2004 7:50 pm
Posts: 10
Hibernate version: 3.1.2

Application server: JBoss 4.0.2

Name and version of the database you are using:Oracle 8.1.7

Hi,

We are building a web application with EJB session beans and Hibernate in the backend. For auditing purposes we would like to use Hibernate interceptors. For every table manipulation two columns have to be set: modify date and modify user.
The modify date is easy. But how can I get a hold on the user (in the interceptor) that is logged on in the web application that is in the EJB context ?

Thanks anyway,

Joris Wijlens


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 04, 2006 5:34 am 
Newbie

Joined: Wed Oct 06, 2004 2:49 am
Posts: 12
Code a PrincipalHolder that has a ThreadLocal attribute and static accessors where you can store the Principal in the ThreadLocal. Then you have basically two hooks where you can retrieve and set the Principal on the PrincipalHolder:

in the Session Bean using 'getSessionContext().getCallerPrincipal();'

in a Servlet Filter casting the ServletRequest to a HttpServletRequest and using 'request.getUserPrincipal()'. This of course only works if your web layer is running in the same JVM as your Beans.

In the hibernate interceptor you can use the PrincipalHolder to get the logged in user.


Top
 Profile  
 
 Post subject: principalholder and jboss interceptor
PostPosted: Tue Jun 13, 2006 7:13 pm 
Newbie

Joined: Wed Feb 11, 2004 7:50 pm
Posts: 10
Thank you very much for your reply, it send me in the right direction. Instead of using a servlet filter or 'getSessionContext().getCallerPrincipal();' in the session beans, I implemented a jboss interceptor that intercepts every invocation to session beans and puts the principal on the principalholder this works very well. The code looks like this:

Code:
package com.wijlens;

import java.security.Principal;

public class PrincipalHolder {
   private static ThreadLocal principal = new ThreadLocal();

   public static Principal getPrincipal() {
      return (Principal)principal.get();
   }

   public static void setPrincipal(Principal principal) {
      if(principal != null)
      PrincipalHolder.principal.set(principal);
   }
}


and:

Code:
package com.wijlens;

import org.jboss.ejb.plugins.AbstractInterceptor;
import org.jboss.invocation.Invocation;


public class PrincipalStorageInterceptor extends AbstractInterceptor {

   public Object invoke(Invocation invocation) throws Exception {
      PrincipalHolder.setPrincipal(invocation.getPrincipal());
      return super.invoke(invocation);
   }
}


Joris Wijlens


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.