Announcement Announcement Module
Collapse
No announcement yet.
How to get the current logged in user's username anywhere in the session? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to get the current logged in user's username anywhere in the session?

    Hi all,

    I've a very basic problem with Spring Security.

    I've implemented Spring Security 3 and have a login form which is working fine.

    But once inside I need to fetch data from the Database which is related to the current logged-in user.

    So how can I get the username of the current user?

    I've tried request.getRemoteUser() on the first request, but will it work on every request. I really doubt that.

    Please guide me. HELP. Any help is really appreciated.

  • #2
    I've found a solution to the problem:

    I've used SecurityContextHolder to get the principal & use in in my impl class.
    Works as a charm.
    Here's the code for anyone looking for the same problem.
    Code:
    User user = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    		String email = user.getUsername();
    But is there any way to inject it in my classes. How to do that? Please guide.

    Comment

    Working...
    X