Jdk17 jwt报错 java.lang.ClassNotFoundException: javax.xml.bind.DatatypeConverter

版本

  • Spring Boot 3.3.2
  • Jdk17
  • 原先Jwt0.9.1

解决办法

  • 修改maven项目下的pom.xml文件
<!--		Jwt依赖-->
		<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;
		}
	}
}

参考

https://blog.csdn.net/u010748421/article/details/107363925

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部