Difficulty importing JWT (JSON Web Token) in Spring Boot Gradle project

Dog :

I have a Spring Boot gradle project and in the build.gradle dependencies, I import JSON Web Token as:

compile group: 'io.jsonwebtoken', name: 'jjwt', version: '0.2'

Following Spring Security and a video tutorial, I have then built out a successful Authentication method. However, when I use Jwts in this method, it does not prompt me to import the same file as in the video tutorial and in fact does not have a prompt for any import options. Specifically, in the video (which uses Maven), the prompt is added to import:

import io.jsonwebtoken

Yet in my app I am never given this option and importing this manually or as import io.jsonwebtoken.*; does not work. As the class indicates that:

import io cannot be resolved 

Similarly, the SignatureAlgorithm method does not contain an import from JWT.

How can I successfully import JSON web token into my gradle project (or at least import io). The method from the video tutorial is below. Jwts is the implementation of the web token and it is the package I am having difficulty with.

@Override
protected void successfulAuthentication(HttpServletRequest req,
                HttpServletResponse res, 
                FilterChain chain, 
                Authentication auth) throws IOException, ServletException {

    String userName = ((User) auth.getPrincipal()).getUsername();

    String token = Jwts.builder()
            .setSubject(userName)
            .setExpiration(new Date(System.currentTimeMillis() + SecurityConstants.EXPIRATION_TIME))
            .signWith(SignatureAlgorithm.HS512, SecurityConstants.TOKEN_SECRET)
            .compact();

    res.addHeader(SecurityConstants.HEADER_STRING, SecurityConstants.TOKEN_PREFIX + token);
}

The project itself I am putting together is on Github and the above noted class is at:

https://github.com/jwolfe890/SpringBootProject1/blob/master/src/main/java/sbootproject/security/AuthenticationFilter.java

Chi Dov :

So I got your problem you should use 0.2 instead of 0.2.0

For gradle 4.10 it would be good to use implementation instead of compile.

implementation('io.jsonwebtoken:jjwt:0.2')

enter image description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=129664&siteId=1