• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

How to get user principal from hibernate domain object

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

 
Saloon Keeper
Posts: 25476
180
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch, Thomas!

I don't know what EAP is and attempts to resolve that name only come up with things like "Employee Assistance Program".

However, I can tell you this. The JEE UserPrincipal object (Interface) is constructed when a remote user logs in using JEE Container-Managed Security. Since it's only an Interface, the exact contents of that object are not directly knowable with the exception of the user's loginId. You obtain the UserPrincipal from the HttpServletRequest object passed in when a servlet or JSP is invoked. Or you can just invoke getRemoteUser() to obtain just the userID. If a user has not been logged in, you'll get back null as the userID.

Hibernate is a general-purpose ORM and has no particular interest specifically in whether it's running in a webapp or not (in which case JEE UserPrincipal wouldn't exist). I think what you are actually looking at is not Wildfly-related but a Spring webapp object, possibly related to Spring Boot (which doesn't normally run under Wildfly, since Spring Boot normally has its own embedded webapp server - typically Tomcat).
 
Sheriff
Posts: 22649
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:I don't know what EAP is and attempts to resolve that name only come up with things like "Employee Assistance Program".


JBoss Enterprise Application Platform
 
Greenhorn
Posts: 27
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I see that with Wildfly that is no longer possible.



Hi Thomas,
As Tim mentioned the JEE api provides a UserPrincipal object. That hasn't changed in Wildfly24.

Yes in EAP6.x your code would rely on SecurityContextAssociation to access above interface.
Wildfly24 it is still possible and relies on Context Dependency Injection (CDI) to expose objects and methods you need.

Take a look at this Wildfly 24 quickstart example that uses the JEE Security api.

https://github.com/wildfly/quickstart/blob/162cf351aecaf0ef0cd4d7d03b40c53df26540da/ee-security/src/main/java/org/jboss/as/quickstarts/ee_security/SecuredServlet.java

Jeremy
 
She still doesn't approve of my superhero lifestyle. Or this shameless plug:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic