ilovetolearn
ilovetolearn

Reputation: 2060

Set the UUID and User ID into the current request thread

I would like to enhance my production logging for my rest application. Is there anyway I can access the current request thread to perform the following:

This is allow me to view the details via logging or jstack.

Upvotes: 2

Views: 1628

Answers (1)

Aliaksei Stadnik
Aliaksei Stadnik

Reputation: 1968

As you are using Spring Security You could use SecurityContextHolder from Spring

User user = (org.springframework.security.core.userdetails.User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
String name = user.getUsername(); //get logged in username

And then use JDK function for generating UUID

java.util.UUID.randomUUID()

Updated

I would not recommend to use thread local for storing this info, ideally you could store this info in current http session. If you use stateless session you could use session provided by your auth server and use access token to store this information. Most of the auth services aloud to create custom fields in the access token.

Upvotes: 1

Related Questions