Update Entities with currentUser-ID

Cicero

Neues Mitglied
Hallo,

I'm trying to update each entity with an own @EntityListeners({ ModelListener.class }).
ModelListener implements @PrePersist with a method like this:

Java:
    @PrePersist
	protected void prePersist(final ModelBase modelBase) {
		modelBase.setUpdatedBy(currentUser);}
My question is how to get the current user.
I already tried several things:

1) transport the information over EJB

Java:
   	@EJB
	private UserModul userModul;
The userModul is always null, when prePersist is called. It seems the EJB-context is not available.

2) Fill a static ThreadLocal<User>

static ThreadLocal<User> currentUser = new ThreadLocal<User>();
Because of the static attribute each classloader (each request in webservers) holds its own instance. I tried with a @WebFilter to fill the ThreadLocal. But it seems I have no faces context.

Java:
 	public void doFilter(final ServletRequest request, ...){
		FacesContext.getCurrentInstance();} //always null
Also in request (com.sun.enterprise.web.pwc.connector.coyote.PwcCoyoteRequest) I don't find any information of loggedin user.

What is the most common way to save user information?

thanks Marco
 
What is your security solution? Do you use J2EE Web-Container based Security, Spring Security or another custom solution?

Depending on your security setup you can get the userid. If you you use J2EE Security you can get it by request.getRemoteUser(). If you use Spring Security you can use SecurityContextHolder....

If we don't know how your login works it's difficult to give help.
 

Zurück
Oben