Home > Java > javaTutorial > body text

How to Retrieve the Active User's UserDetails in a Spring Application?

Patricia Arquette
Release: 2024-11-05 04:37:02
Original
491 people have browsed it

How to Retrieve the Active User's UserDetails in a Spring Application?

Retrieving the Active User's UserDetails

When working with controllers in a Spring application, retrieving the details of the active user can be achieved by accessing the UserDetails implementation.

<code class="java">User activeUser = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();</code>
Copy after login

While this approach is functional, it can be simplified using dependency injection.

Using @AuthenticationPrincipal Annotation

In Spring Security 3.2 and later, the @AuthenticationPrincipal annotation can be utilized. This elegant solution enables direct injection of the UserDetails object into the controller or method.

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

Using ArgumentResolvers

Prior to Spring Security 3.2, or if customization is required, argument resolvers can be employed. A WebArgumentResolver, like CurrentUserWebArgumentResolver, can be created to automatically resolve the UserDetails object.

<code class="java">public class CurrentUserWebArgumentResolver implements WebArgumentResolver {
    @Override
    public Object resolveArgument(...) {
        if (...) {
            return (User) ((Authentication) principal).getPrincipal();
        } else {
            return WebArgumentResolver.UNRESOLVED;
        }
    }
}</code>
Copy after login

This resolver needs to be registered in the application's configuration.

Spring 3.1 : HandlerMethodArgumentResolver

In Spring 3.1 , the HandlerMethodArgumentResolver, like CurrentUserHandlerMethodArgumentResolver, is recommended. It provides similar functionality to the WebArgumentResolver.

<code class="java">public class CurrentUserHandlerMethodArgumentResolver implements HandlerMethodArgumentResolver {
    @Override
    public Object resolveArgument(...) {
        if (...) {
            return (User) ((Authentication) principal).getPrincipal();
        } else {
            return WebArgumentResolver.UNRESOLVED;
        }
    }
}</code>
Copy after login

This resolver also needs to be registered in the configuration.

By utilizing these methods, you can elegantly obtain the UserDetails of the active user in your controllers, enhancing the maintainability and readability of your Spring application's code.

The above is the detailed content of How to Retrieve the Active User's UserDetails in a Spring Application?. 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
Latest Articles by Author
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!