How to apply custom validation to JWT token on each request for spring boot?

Viewed 70

I am trying to find a way to add more token validation with @preauthorize annotation. meaning that I need to validate the issuedAt field in the token against a field in the database every time a request happens, but I need to do it while spring boot checks the token validity to avoid parsing the token again which is terrible for performance... any solutions?

here is the function I want to add the validation to it

    @PostMapping("/add")
    @PreAuthorize("hasAuthority('user_add')")
    public void addUser(@RequestBody User user,HttpServletRequest request){
        userService.addNewUser(user,request);
    }

and here is the logic I want to perform at each request

String tokenIssuedAt= getIssuedAtDateFromToken(token,secretKey);


if(!tokenIssuedAt.equals(sessionID) ){ // token is not validated as it is not up-to-date
    throw new ResponseStatusException(HttpStatus.FORBIDDEN,"Session not up-to-date!!");
    }
0 Answers
Related