Home > Net >  i have a spring boot app with JWT token and im giving an arror
i have a spring boot app with JWT token and im giving an arror

Time:11-01

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"
}

CodePudding user response:

Make sure you added all the required dependencies:

add these dependencies, and make sure you add the jwt dependencies below as well:

 <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-core</artifactId>
        <version>2.3.0.1</version>
    </dependency>
    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>2.3.1</version>
    </dependency>

extra dependencies:

<!-- https://mvnrepository.com/artifact/io.jsonwebtoken/jjwt-api -->
            <dependency>
                <groupId>io.jsonwebtoken</groupId>
                <artifactId>jjwt-api</artifactId>
                <version>0.11.5</version>
            </dependency>
            <dependency>
                <groupId>io.jsonwebtoken</groupId>
                <artifactId>jjwt-impl</artifactId>
                <version>0.11.5</version>
                <scope>runtime</scope>
            </dependency>
            <dependency>
                <groupId>io.jsonwebtoken</groupId>
                <artifactId>jjwt-jackson</artifactId> <!-- or jjwt-gson if Gson is preferred -->
                <version>0.11.5</version>
                <scope>runtime</scope>
            </dependency>

CodePudding user response:

Could it be that you are missing a dependency?

A quick internet search shows that this class is part of this library

  • Related