How to get user principal from hibernate domain object

63 views
Skip to first unread message

Tom Tibbetts

unread,
May 2, 2022, 10:58:02 AM5/2/22
to WildFly
Hi,
I'm migrating an application from EAP 6 to Wildfly 24.  We are using hibernate.  We have a base domain class that, upon prepersist or preupdate populate a couple of fields with the username taken from the user principal.  I see that with Wildfly that is no longer possible.  I've searched all over and have not found a solution.  Can someone here help?  Here is the sample code I'm migrating:

@MappedSuperclass
public class BaseAudited {
   
    private String createdBy;
    private    LocalDateTime createdTime;
    private String modifiedBy;
    private LocalDateTime modifiedTime;
    private String inactivatedBy;
    private LocalDateTime inactivatedTime;
   
    /*
    various getters and setters
    */
   
    @PrePersist
    public void prePersist() {
        createdTime = LocalDateTime.now();
        createdBy ="Anonymous";
        modifiedBy = "Anonymous";
    }
   
    @PreUpdate
    public void preUpdate() {
        modifiedTime = LocalDateTime.now();
       
        Principal principal = SecurityContextAssociation.getPrincipal();
       
        if(principal != null) {
            modifiedBy = principal.getName();
         }
        else {
            modifiedBy = "Anonymous";
        }
       
    }
}
Reply all
Reply to author
Forward
0 new messages