Example usage for org.springframework.security.core.userdetails User User

List of usage examples for org.springframework.security.core.userdetails User User

Introduction

In this page you can find the example usage for org.springframework.security.core.userdetails User User.

Prototype

User

Source Link

Usage

From source file:com.mycompany.springrest.token.JwtUtil.java

/**
 * Tries to parse specified String as a JWT token. If successful, returns User object with username, id and role prefilled (extracted from token).
 * If unsuccessful (token is invalid or not containing all required user properties), simply returns null.
 * /*from   w  w  w. j ava 2s.c o m*/
 * @param token the JWT token to parse
 * @return the User object extracted from specified token or null if a token is invalid.
 */
public User parseToken(String token) {
    try {
        Claims body = Jwts.parser().setSigningKey(secret).parseClaimsJws(token).getBody();

        User u = new User();
        u.setUsername(body.getSubject());
        u.setId(Long.parseLong((String) body.get("userId")));
        u.setRole((String) body.get("role"));

        return u;

    } catch (JwtException | ClassCastException e) {
        return null;
    }
}