How to correctly and conveniently organize authentication in Spring REST Api through JWT? I do not want to use the session because it is REST and JWT. Verification of the token should take place with every request that requires authentication. The token is either stored in cookies or transmitted in headers (this is not so important).
The question is how to make authentication as easy as possible to use, what to write, say, a filter, and forget about it. What would be inside each method where authentication is needed, do not pull something like getUser (). I just want to write and forget, but that this user would already be inside the method and could work with him.
Before writing Api on SparkJava, everything was very simple there. Instead of the standard Route, where it was necessary, I returned the SecuredRoute samopisny, which in turn was implemented from the standard Route, and redefined its standard handle method, in which the authorization check logic was written, and if the user is authorized, the handleSecured was passed on, in which parameters already was the user himself.
The code for clarity that was on spark
public interface SecuredRoute extends Route { @Override default Object handle(Request request, Response response) { // Логика проверки... if (юзер не прошел провеку) { response.removeCookie("/", "accessToken"); return new ErrorResponse(response).errorResponseUnauthenticated(); } return handleSecured(request, response, user); } Object handleSecured(Request request, Response response, User user); } Is it possible to do something like this on Spring?
It seems to me that, ideally, it would be to create some kind of annotation that would be put in front of the method in which authorization is needed, and how that injection of this user into the method