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
 

tagedieb

Top Contributor
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.
 
Ähnliche Java Themen
  Titel Forum Antworten Datum
A JPA/Hibernate: Update/Delete/Insert auf Entities von DB-Views Data Tier 3
C JPA FetchType.LAZY, Relation @OneToMany und Problem mit dem update Data Tier 1
G Hibernate select update no wait Data Tier 8
F GF server bei jedem update restart Data Tier 4
D jpq entity life cycle - insert, update... Data Tier 5
F Problem mit Hibernate Schema Update Data Tier 2
T [JPA] Update Entity in Entity Data Tier 2
M Hibernate: Skalare Funktionen und Update Data Tier 6
S Hibernate: Dauer-Update bei composite-element Data Tier 6
K Hibernate update-Problem Data Tier 36
N Hibernate - Problem mit Update/Insert Data Tier 4
D JPA JPA Entities EJB-fähig Data Tier 4
D JPA Entities mit Proxies ausserhalb einer Session (Lazy Loading) Data Tier 0
C JPA ClassCastException bei Entities-JAR im EAR Data Tier 0
A @ManyToMany - Kein Löschen bereits zugewiesener Entities Data Tier 6
D JPA - aktualisierung von gleichen Entities Data Tier 13
A frage zu entities Data Tier 6
N [EclipseLink] Best Practise zur Generierung von Entities aus Tabellen Data Tier 28
X [Hibernate] Zusammengesetzte Entities möglich? Data Tier 7
R JPA JP-QL-SELECT Befehl für Entities Data Tier 1
A Join-Queries über mehrere Entities Data Tier 3
D Abstrakte Basisklasse für alle Entities? Data Tier 3
S JPA Toplink Generate Tables from Entities erzeugt keine Outputfile Data Tier 9

Ähnliche Java Themen


Oben