Jdk17 jwt报错 java.lang.ClassNotFoundException: javax.xml.bind.DatatypeConverter
版本
- Spring Boot 3.3.2
- Jdk17
- 原先Jwt0.9.1
解决办法
-
修改maven项目下的pom.xml文件
<dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt-api</artifactId> <version>0.11.2</version> </dependency> <!-- Jwt依赖--> <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt-impl</artifactId> <version>0.11.2</version> </dependency> <!-- Jwt依赖--> <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt-jackson</artifactId> <version>0.11.2</version> </dependency>
-
修改Jwt相关代码
import java.util.Base64;
import java.util.Date;
import java.util.HashMap;
import javax.crypto.SecretKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;
@Component
public class JwtTool {static private Logger log = LoggerFactory.getLogger(JwtTool.class); private static SecretKey key; static { String secertString = "hello,world"; byte[] keyBytes256 = new byte[32]; int encodeNum = Base64.getEncoder().encode(secertString.getBytes(),keyBytes256); key = Keys.hmacShaKeyFor(keyBytes256); } public static String getJwt(HashMap<String, Object> peopleMap) { try { String jwt = Jwts.builder() .setClaims(peopleMap) .setExpiration(new Date(System.currentTimeMillis()+3600*1000)) .signWith(key) .compact(); log.info(jwt); return jwt; } catch (Exception e) { // TODO: handle exception e.printStackTrace(); return null; } } public static String parseJwt(String jwt) { try { Claims claims = Jwts.parserBuilder() .setSigningKey(key) .build() .parseClaimsJws(jwt) .getBody(); log.info("解析jwt成功"); return claims.toString(); } catch (Exception e) { e.printStackTrace(); log.info("解析jwt失败"); return null; } }
}