我在Spring中有一个基于令牌的身份验证方案。但是JWTParser似乎很麻烦。以下代码中的JWTParser类实际上做什么?另外,我无法导入此类。

public class JWTAuthenticationToken extends AbstractAuthenticationToken {
    private static final long serialVersionUID = 1L;
    private final Object principal;
    private Object details;

    Collection authorities;

    public JWTAuthenticationToken( String jwtToken ) {
        super( null );
        super.setAuthenticated( true ); // must use super, as we override
        JWTParser parser = new JWTParser( jwtToken );

        this.principal = parser.getSub();

        this.setDetailsAuthorities();

    }

    @Override
    public Object getCredentials() {
        return "";
    }

    @Override
    public Object getPrincipal() {
        return principal;
    }

    private void setDetailsAuthorities() {
        String username = principal.toString();
        SpringUserDetailsAdapter adapter = new SpringUserDetailsAdapter( username );
        details = adapter;
        authorities = ( Collection ) adapter.getAuthorities();

    }

    @Override
    public Collection getAuthorities() {
        return authorities;
    }
}


有没有解决的办法?

最佳答案

JwtParser parser =new  DefaultJwtParser();
parser.parse(jwtToken);

<dependency>
 <groupId>io.jsonwebtoken</groupId>
 <artifactId>jjwt</artifactId>
 <version>0.6.0</version>
</dependency>

07-23 15:27