Home > Java > javaTutorial > body text

How to Retrieve Active User Details in Spring Controllers?

DDD
Release: 2024-11-05 17:17:02
Original
600 people have browsed it

How to Retrieve Active User Details in Spring Controllers?

Obtaining Active User UserDetails

In Spring controllers, accessing the active (logged in) user's details typically involves retrieving the UserDetails implementation through the SecurityContextHolder. However, Spring offers an alternative approach to simplify this process.

Using @AuthenticationPrincipal

From Spring Security 3.2 onwards, the @AuthenticationPrincipal annotation provides a convenient way to autowire the UserDetails into controllers or methods. By annotating a method parameter with @AuthenticationPrincipal, Spring automatically injects the Principal object, which contains the UserDetails implementation.

Example:

<code class="java">public ModelAndView someRequestHandler(@AuthenticationPrincipal User activeUser) { ... }</code>
Copy after login

Defining a Custom Argument Resolver (Pre-Spring 3.2)

If you're using an earlier version of Spring Security or need more control, you can implement a custom WebArgumentResolver or HandlerMethodArgumentResolver to retrieve the User object from the Principal.

Example (WebArgumentResolver):

<code class="java">public class CurrentUserWebArgumentResolver implements WebArgumentResolver {
    @Override
    public Object resolveArgument(MethodParameter methodParameter, NativeWebRequest webRequest) {
        if (methodParameter.isParameterType(User.class) && methodParameter.hasParameterAnnotation(ActiveUser.class)) {
            Principal principal = webRequest.getUserPrincipal();
            return (User) ((Authentication) principal).getPrincipal();
        } else {
            return WebArgumentResolver.UNRESOLVED;
        }
    }
}</code>
Copy after login

Spring 3.1 (HandlerMethodArgumentResolver):

<code class="java">public class CurrentUserHandlerMethodArgumentResolver implements HandlerMethodArgumentResolver {
    @Override
    public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) {
        if (this.supportsParameter(methodParameter)) {
            Principal principal = webRequest.getUserPrincipal();
            return (User) ((Authentication) principal).getPrincipal();
        } else {
            return WebArgumentResolver.UNRESOLVED;
        }
    }
}</code>
Copy after login

The above is the detailed content of How to Retrieve Active User Details in Spring Controllers?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!