I have a filter for token validation and I am using JWT token for authentication and authorization.
Suppose I am passing user_id in the JWT token payload and I want this user_id to be available for all my resource methods that will pass my filter so that I can do query on the basis of user_id in my resource method.
Is this possible to do so?
Or how can I pass this user_id as a parameter to my resource methods from my filter ?
And whether it is a good practice to do so ?
public void filter(ContainerRequestContext requestContext)
throws IOException, ConnectException {
UriInfo uriInfo = requestContext.getUriInfo();
UserModel user = new UserModel();
Claims claim =null;
String temp=null;
requestContext.setProperty("before","before");
if (uriInfo.getPath().equals("profile")) {
String authorizationHeader =
requestContext.getHeaderString(HttpHeaders.AUTHORIZATION);
// Check if the HTTP Authorization header is present and formatted correctly
if (authorizationHeader == null || !authorizationHeader.startsWith("Bearer ")) {
throw new NotAuthorizedException("Authorization header must be provided");
}
// Extract the token from the HTTP Authorization header
String tokens = authorizationHeader.substring("Bearer".length()).trim();
try {
claim = user.validateToken(tokens);
} catch (SQLException ex) {
Logger.getLogger(AuthorizationRequestFilter.class.getName()).log(Level.SEVERE, null, ex);
}
requestContext.setProperty("id",claim.getId());
requestContext.setProperty("hello","hello");
}
}
I am able to get the value for "before" variable in my resource method. but I am getting null for "hello" and "id". I want to get the value "id" in my resource method.
Thanks