At the moment we have 6 Maven modules:
webapp
security
core
(provides database access toUser
)common
module1
module2
The dependency tree is pretty obvious I think:
webapp
depends on everythingsecurity
depends on corecore
depends on commoncommon
depends on nothingmodule1
depends on core and commonmodule2
depends on core, module1 and common
Now I'd like to have some BaseEntity
: It should have a @PrePersist
which saves the current User
. Nearly every entity will use this BaseEntity
. That's why every module depends on core
.
And because everything depends on core
, it seems logical to put this BaseEntity
also in the core
module. (even if I'd prefer to use common
for this, but that seems impossible because of dependencies).
Now the problem occurs: To set the current User, I have to use access SecurityContextHolder.getContext().getAuthentication().getPrincipal()
. But with this I would have some unwanted dependency (or am I just too nitpicking?).
The problem get's even worse, if I want to have a custom implementation of UserDetails
. Where should I put it? core
or security
? Or is it common to just let the User
entity implement UserDetails
? I don't think so. The question occurs, because when authenticating a user, I have to create the UserDetails
object inside the security
module. And when I want to retrieve the current User
I'd have to cast the getPrincipal()
method to the custom UserDetails
class.
I'm really confused how to leave thing loosely coupled, but also achieve everything I need for the application.
The last idea that came to my mind was about using Dependency Injection, but I don't know if it works!? (Having a currentUser
Bean inside the security
module and everyone else can simply get it via @Autowired MyCustomUserDetails
)
So please help me getting those things right!
Thank you! :)