Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Framework - Where to parse JWT for custom claim?

I have created a Spring JWT authorization application. JWT contains some custom claims. On a resource server side, I wonder, where should I parse the JWT token to collect and check these claims? Should I do this in a controller or in some filter? Whats the best practice? Maybe you have some example?

like image 631
dplesa Avatar asked Jul 21 '17 07:07

dplesa


People also ask

How do I add a claim to JWT token spring?

Adding JWT support to a Spring Boot application is very simple. All you need to do is to add a short XML code snippet to a pom. xml file. You can find the JWT support dependency XML code snippet here: JSON Web Token Support For The JVM.

Where does spring boot store JWT tokens?

It is stored in-memory by default.


1 Answers

I'm using this:

private Claim getClaim(String claimKey) {
    Authentication token = SecurityContextHolder.getContext().getAuthentication();
    try {
        DecodedJWT jwt = JWT.decode(token.getCredentials().toString());
        return jwt.getClaim(claimKey);
    } catch (JWTVerificationException ex) {
        throw new RuntimeException(ex);
    }
}
like image 146
Stephen Paul Avatar answered Sep 26 '22 08:09

Stephen Paul