JWT令牌解析

时间:2017-05-21 09:31:52

标签: spring-security token jwt

请告诉我如何正确解析JWT令牌。 我需要得到角色......

   static Authentication getAuthentication(HttpServletRequest request) {
    String token = request.getHeader(HEADER_STRING);
    if (token != null) {
        // parse the token.
        String user = Jwts.parser()
                .setSigningKey(SECRET)
                .parseClaimsJws(token.replace(TOKEN_PREFIX, ""))
                .getBody()
                .getSubject();
        Claims claims = Jwts.parser()
                .setSigningKey(SECRET)
                .parseClaimsJws(token.replace(TOKEN_PREFIX, ""))
                .getBody();
        List<String> roles = (ArrayList<String>)claims.get("roles");
        System.out.println("ROLE IS: " + roles);
        return user != null ?
                new UsernamePasswordAuthenticationToken(user, null, Collections.emptyList()) :
                null;
    }
    return null;
}

输出ROLE IS:[{authority = ROLE_ADMIN},{authority = ROLE_USER} 如何正确地将其转换为Collection以用作UsernamePasswordAuthenticationToken中的参数。 谢谢。

1 个答案:

答案 0 :(得分:0)

f1(df, matchval, matchfield, qfields...) = isempty(qfields)