Issue
i am trying to build a spring boot app using JWT token, but it shows me this error
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]
the JwtUtil class is : I am following everything from a guy using this strategy: 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();
}
}
I was expecting to get the token from postman using POST,BODY,JSON and giving username and password, but i am getting :
{
"timestamp": "2022-10-31T16:35:00.188+00:00",
"status": 500,
"error": "Internal Server Error",
"path": "/authenticate"
}
Solution
Could it be that you are missing a dependency?
A quick internet search shows that this class is part of this library
Answered By - Borislav Stoilov
Answer Checked By - Terry (JavaFixing Volunteer)