I have a Spring Boot web application that expects all incoming requests to have a cookie with a valid JWT ID token. The ID token holds the email address of the user, which is used to query the database for the User entity. The User entity is then used by multiple services.
Currently, the controllers receive the raw ID token as a parameter, and then passes it down to the services:
@PostMapping()
void foo(@CookieValue String rawIdToken) {
myService.bar(rawIdToken);
}
Then each service is parsing the token and querying the database:
void bar(String rawIdToken) {
// Parse the ID token
IdToken idToken = IdToken.from("RAW_ID_TOKEN");
// Query the database
User user = userRepository.findByEmail(idToken.getEmail());
// Code that requires the User entity..
}
Is there a way to avoid having to do this every time, and instead automatically make the User entity available to all controllers? So that, for example, the sample controller above can become something like that:
@PostMapping()
void foo(User user) {
myService.bar(user);
}
And the service:
void bar(User user) {
// Code that requires the User entity..
}
Please let me know if I am looking at this the wrong way.