当前位置: 首页 > 知识库问答 >
问题:

java.lang.NoClassDefoundError:io/JsonWebToken/Signaturealgorithm

陶胤运
2023-03-14

下面的代码试图创建一个jwt令牌。每当它尝试执行SignatureAlgorithm SignatureAlgorithm=SignatureAlgorithm.hs256时,总会出现如下所示的错误;

我已经手动删除了jwt jar文件,并再次更新maven以重新安装jar,但问题仍然存在。

//Sample method to construct a JWT
public static String createJWT(String id, String issuer, String subject, 
        String payload, String role, String name, long ttlMillis) {
    String jwt = "";

    try {
        //The JWT signature algorithm we will be using to sign the token
        SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256;

        long nowMillis = System.currentTimeMillis();
        Date now = new Date(nowMillis);

        //We will sign our JWT with our ApiKey secret
        byte[] apiKeySecretBytes = DatatypeConverter.parseBase64Binary(apiKey.getSecret());
        Key signingKey = new SecretKeySpec(apiKeySecretBytes, signatureAlgorithm.getJcaName());

        //Let's set the JWT Claims
        JwtBuilder builder = Jwts.builder()
                                    .setIssuedAt(now)
                                    .setSubject("test")
                                    .claim("role", role)
                                    .claim("name", name)
                                    .setIssuer("tester")
                                    .signWith(io.jsonwebtoken.SignatureAlgorithm.HS256, signingKey);
                                    //.signWith(signatureAlgorithm, apiKey.getSecret().getBytes("UTF-8"));
                                    //

        //if it has been specified, let's add the expiration
        if (ttlMillis >= 0) {
        long expMillis = nowMillis + ttlMillis;
            Date exp = new Date(expMillis);
            builder.setExpiration(exp);
        }

        //Builds the JWT and serializes it to a compact, URL-safe string
        jwt = builder.compact();

    } catch (Exception ex) {

    }
    return jwt;
}

错误日志

POM依赖项

<dependencies>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.8.8</version>
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-client</artifactId>
            <version>2.25.1</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-server</artifactId>
            <version>2.25</version>
        </dependency>

        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20160810</version>
        </dependency>
        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.9.3</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.jaxrs</groupId>
            <artifactId>jackson-jaxrs-json-provider</artifactId>
            <version>2.8.6</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.8.9</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-jackson</artifactId>
            <version>2.25.1</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>org.eclipse.persistence.moxy</artifactId>
            <version>2.6.4</version>
        </dependency>

        <dependency>
             <groupId>org.glassfish.jersey.containers</groupId>
             <artifactId>jersey-container-servlet</artifactId>
             <version>2.22.1</version>
        </dependency>

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

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.42</version>
        </dependency>

    </dependencies>

共有1个答案

孔彭祖
2023-03-14

NoClassDefoundError是运行时问题。尝试在manifest文件中添加jar条目,这将帮助您解决问题。

 类似资料:
  • 我目前正在使用React在一个网站上工作,我希望能够有用户登录。现在,我的策略是在提交时将表单数据发送到服务器(express),如果信息与我的数据库中的用户匹配,服务器将发回一个签名的JWT,其中没有敏感信息(只有用户名)。 一旦客户机接收到JWT,我就将它添加到localStorage中,并将它的解码数据添加到redux存储中。我计划让我的redux存储当前登录的用户。

  • 您好,我正在尝试使用passportjs和jsonwebtoken进行android应用程序的身份验证,但当我尝试生成一个令牌作为用户登录并使用postman it响应错误测试api时: 未指定默认引擎,也未提供扩展。在函数的新视图中(C:\newApp\awesomeProject\node\u modules\express\lib\View.js:61:11)。在ServerResponse

相关问答

相关文章

相关阅读