L
L
Loligan2016-05-18 13:37:34
Java
Loligan, 2016-05-18 13:37:34

How can I not have to search for a user every time in Spring?

In general, every time I access a controller in Spring, I have to look for a user, create an object, and already work with it. Those. let's say I go to /profile, I pull out the name of the authorized user, I look for it in the Base, if everything is fine, then I work with it. I click on /index, then again I have to look for the user in the database, create an object and again build a page for it. How to avoid searching and creating a new object?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vayho, 2016-05-18
@vayho

If you are using Spring Security, then the current authorized user is in the SecurityContextHolder, we get it like this:

(UserWrapper) SecurityContextHolder.getContext().getAuthentication().getPrincipal()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question