S
S
s2018-07-09 03:12:11
Java
s, 2018-07-09 03:12:11

Is it possible to work with principal from Spring Security in Spring MVC?

After getting acquainted with Spring Security and the principal, in which the current user is stored, I was interested in the question: Can this user be passed and used in Spring MVC methods and how can this be done?
If this is not possible, what alternatives are there to carry out such work?
I'd love to hear your answer or your thoughts!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2018-07-09
@solovladys

You can just specify it in the controller's argument list and Spring will inject it:

@RequestMapping(...)
public String home(Model model, Principal principal) {
    ...
}

Or you can get it from SecurityContextHolder :
Principal principal = 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