Tag Archives: Jjwt error

Jjwt error: ERROR 9856 — [nio-8083-exec-2] o.a.c.c. [. [. [. [/]. [dispatch server]

**

Solve jjwt generation error

**
recently, when learning springboot + JWT + Vue, an error occurred while generating JWT. The error code is as follows:

2022-01-07 11:17:32.335 ERROR 9856 --- [nio-8083-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Filter execution threw an exception] with root cause

java.lang.ClassNotFoundException: javax.xml.bind.DatatypeConverter
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641) ~[na:na]
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188) ~[na:na]
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520) ~[na:na]
	at io.jsonwebtoken.impl.Base64Codec.decode(Base64Codec.java:26) ~[jjwt-0.9.1.jar:0.9.1]
	at io.jsonwebtoken.impl.DefaultJwtBuilder.signWith(DefaultJwtBuilder.java:99) ~[jjwt-0.9.1.jar:0.9.1]
	at com.okwind.utils.JwtUtils.generateToken(JwtUtils.java:31) ~[classes/:na]
	at com.okwind.security.LoginSuccessHandler.onAuthenticationSuccess(LoginSuccessHandler.java:32) ~[classes/:na]

Analysis error code: at com okwind.utils.JwtUtils. Generatetoken (jwtutils.Java: 31) ~ [classes/: Na] knows that the error occurred in jwtutils Generatetoken (jwtutils.Java: 31) is on line 31 of the tool class.

 26        return Jwts.builder()
 27               .setHeaderParam("typ", "JWT")
 28               .setSubject(username)
 29               .setIssuedAt(nowDate)
 30               .setExpiration(expireDate)
 31               .signWith(SignatureAlgorithm.HS256, app_secret)
 32               .compact();

It means that an exception occurred in generateToken() and it is related to SignatureAlgorithm.HS256.
after analysis, it is referenced:

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

jjwt version 0.91 is not supported for new JDK versions. There are two solutions.

Method 1: jdk drops to below 11.
Method 2: keep the jdk version unchanged from the higher version and introduce in pom.xml.

<dependency>
    <groupId>io.jsonwebtoken</groupId>
    <artifactId>jjwt</artifactId>
    <version>0.9.1</version>
</dependency>
<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.4.0-b180830.0359</version>
</dependency>
<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-impl</artifactId>
    <version>3.0.0-M4</version>
</dependency>
<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-core</artifactId>
    <version>3.0.0-M4</version>
</dependency>
<dependency>
    <groupId>javax.activation</groupId>
    <artifactId>activation</artifactId>
    <version>1.1.1</version>
</dependency>

So far, the problem is solved.