Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9139377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:22:47+00:00 2026-06-17T09:22:47+00:00

I want to access the current logged in user I am doing it like

  • 0

I want to access the current logged in user I am doing it like this (from a static method)

public static User getCurrentUser() {

final Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();

if (principal instanceof User) {
  return (User) principal;
  }
}

or injecting and casting like this :

@RequestMapping(value = "/Foo/{id}", method = RequestMethod.GET)
public ModelAndView getFoo(@PathVariable Long id, Principal principal) {
        User user = (User) ((Authentication) principal).getPrincipal();
..

Where user implements userdetails, both seem a bit lame is there a better way in Spring 3.2 ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:22:48+00:00Added an answer on June 17, 2026 at 9:22 am

    I don’t think that it has something new in spring 3.2 for that purpose. Have you thought about using a custom annotation?

    Something like this :

    The controller with the custom annotation :

    @Controller
    public class FooController {
    
        @RequestMapping(value="/foo/bar", method=RequestMethod.GET)
        public String fooAction(@LoggedUser User user) {
            System.out.print.println(user.getName());
            return "foo";
        }
    }
    

    The LoggedUser annotation :

    @Target(ElementType.PARAMETER)
    @Retention(RententionPolicy.RUNTIME)
    @Documented
    public @interface LoggedUser {}
    

    The WebArgumentResolver :

    public class LoggedUserWebArgumentResolver implements WebArgumentResolver {
    
        public Object resolveArgument(MethodParameter methodParameter, NativeWebRequest webRequest) {
            Annotation[] annotations = methodParameter.getParameterAnnotations();
    
            if (methodParameter.getParameterType().equals(User.class)) {
                for (Annotation annotation : annotations) {
                    if (LoggedUser.class.isInstance(annotation)) {
                        Principal principal = webRequest.getUserPrincipal();
                        return (User)((Authentication) principal).getPrincipal();
                    }
                }
            }
            return WebArgumentResolver.UNRESOLVED;
        }
    }
    

    Beans configuration :

    <bean id="loggedUserResolver" class="com.package.LoggedUserWebArgumentResolver" />
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="customArgumentResolver" ref="loggedUserResolver" />
    </bean>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to access a static variable from a static method: #!/usr/bin/env python class
I want to access id of current logged user name of current logged user
When I want to access the parent UIView of current UIView I declare object
I want to programmatically determine if the current user (or process) has access to
I want to access text-shadow property just like we access padding property. Means ,
i want to access a integer and a string from a class to all
I want to access the current ViewController. I have a TabBar with a NavBar
I want to add Params in current request in OnActionExecuting() filter i tried this
What I want is to access current HttpServlerRequest + HttpServletResponse + HttpSession + some
What would be the best way to allow a logged-in user only access to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.