我正在尝试使用JWT标记构建一个Sping Boot 应用程序,但它显示了此错误
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:521) ~[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.JavaInUseSpringSECURITY.JavaInUseSpringSECURITY.JWTTOKEN.JwtUtil.doGenerateToken(JwtUtil.java:49) ~[classes/:na]
at com.JavaInUseSpringSECURITY.JavaInUseSpringSECURITY.JWTTOKEN.JwtUtil.generateToken(JwtUtil.java:43) ~[classes/:na]
JwtUtil类为:我正在跟踪一个家伙使用的策略:https://www.javainuse.com/webseries/spring-security-jwt/chap4
@Service
public class JwtUtil {
private String secret;
private int jwtExpirationInMs;
@Value("${jwt.secret}")
public void setSecret(String secret) {
this.secret = secret;
}
@Value("${jwt.jwtExpirationInMs}")
public void setJwtExpirationInMs(int jwtExpirationInMs) {
this.jwtExpirationInMs = jwtExpirationInMs;
}
// generate token for user
public String generateToken(UserDetails userDetails) {
Map<String, Object> claims = new HashMap<>();
Collection<? extends GrantedAuthority> roles = userDetails.getAuthorities();
if (roles.contains(new SimpleGrantedAuthority("ROLE_ADMIN"))) {
claims.put("isAdmin", true);
}
if (roles.contains(new SimpleGrantedAuthority("ROLE_USER"))) {
claims.put("isUser", true);
}
return doGenerateToken(claims, userDetails.getUsername());
}
private String doGenerateToken(Map<String, Object> claims, String subject) {
return Jwts.builder().setClaims(claims).setSubject(subject)
.setIssuedAt(new Date(System.currentTimeMillis()))
.setExpiration(new Date(System.currentTimeMillis() + jwtExpirationInMs)).signWith(SignatureAlgorithm.HS512, secret).compact();
}
}
我期待从 Postman 使用POST,BODY,JSON并给出用户名和密码的令牌,但我得到:
{
"timestamp": "2022-10-31T16:35:00.188+00:00",
"status": 500,
"error": "Internal Server Error",
"path": "/authenticate"
}
2条答案
按热度按时间carvr3hs1#
确保添加了所有必需的依赖项:
添加这些依赖项,并确保您也添加了下面的jwt依赖项:
额外的依赖关系:
2nbm6dog2#
是否缺少依赖项?
快速在Internet上搜索,发现此类是this库的一部分。