-1

I am using Spring (Web MVC - REST) 4.3.3, spring security 3.2.4, Spring security Oauth 1.0.0 and OAuth2 for Authorization.

I am storing user's id as principle of authentication object.

My Question is, I want to know which user called the web service in the Service layer. How can I get that?

I want to use it like this,

Suppose someone creates an user. The creator will be stored in the new user. One way is to pass creator in the user object from client but I want that to be got from token itself because it already has that information.

EDIT

A brief explanation of downvote is welcomed.

Akshay
  • 3,558
  • 4
  • 43
  • 77

1 Answers1

0

You can access your authentication info by

Authentication auth = SecurityContextHolder.getContext().getAuthentication();

SecurityContextHolder contains a ThreadLocal variable, so it's available anywhere during request processing. The Authentication object should contain information from the token.

Ján Halaša
  • 8,167
  • 1
  • 36
  • 36
  • 1
    You are not able to get User ID from SecurityContextHolder.getContext().getAuthentication(); - when you use oAuth – Irakli May 30 '18 at 18:02