使用JJWT JSON Web令牌时java.lang.ClassNotFoundException:io.jsonwebtoken.Jwts

时间:2017-06-07 15:31:59

标签: jwt jersey-2.0 jjwt glassfish-4.1.1

当我尝试从Stormpath使用JJWT时,它会抛出一个运行时异常java.lang.ClassNotFoundException: io.jsonwebtoken.Jwts。我在GlassFish 4.1上使用Jersey2嵌入;这是抛出异常的代码:

private String issueToken(String login) {
    Key key = keyGenerator.generateKey();
    //Key key = MacProvider.generateKey();
        String jwtToken = Jwts.builder()
                .setIssuer(uriInfo.getAbsolutePath().toString())
                //.setIssuer("http://trustyapp.com/")
                .setSubject(login)
                .setIssuedAt(new Date())
                .setExpiration(toDate(LocalDateTime.now().plusMinutes(15L)))
                .signWith(SignatureAlgorithm.HS512, key)
                .compact();
        logger.info("#### generating token for a key : " + jwtToken + " - " + key);
        return jwtToken;
}

我导入了io.jsonwebtoken.Jwts,我的pom.xml有:

<dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.8.2</version>
        <scope>compile</scope>
    </dependency>

我也试过没有上面的依赖,以防我的pom.xml上的下面的依赖就足够了:

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

我尝试了thisthis的建议,但它没有用,请帮忙

1 个答案:

答案 0 :(得分:0)

在我的pom.xml中添加以下依赖项后问题得以解决:

<dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-common</artifactId>
        <version>${version.jersey}</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-jdk-http</artifactId>
        <version>${version.jersey}</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-client</artifactId>
        <version>${version.jersey}</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-server</artifactId>
        <version>${version.jersey}</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        <version>${version.jersey}</version>
    </dependency>

我认为不需要这样的依赖,因为我使用的是嵌入在GlassFish4.1.1服务器上的Jersey 2。