Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 942
Dog Avatar asked Dec 17 '18 05:12

Dog


3 Answers

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

like image 105
Chi Dov Avatar answered Nov 07 '22 03:11

Chi Dov


Following errors I observed from your github project repo:
1. need to add import io.jsonwebtoken.SignatureAlgorithm; in AuthenticationFilter.java
2. UserService.java has method UserDetails loadUserByUsername(String email) throws Exception; which is similar to UserDetails loadUserByUsername(String username) throws UsernameNotFoundException; in UserDetailsService interface, I removed since both serves same purpose
3. Updated method loadUserByUsername in UserServiceImpl.java and throws UsernameNotFoundException exception instead of Exception(since the method is derived from UserDetailsService interface)

I raised merge request to your repo and changes are: https://github.com/jwolfe890/SpringBootProject1/pull/1/files

Do let me know if this is not what you expected.

like image 25
dkb Avatar answered Nov 07 '22 05:11

dkb


It here worked - Reference - https://mvnrepository.com/artifact/io.jsonwebtoken/jjwt/0.2

     <dependency>
        <groupId>io.jsonwebtoken</groupId>
        <artifactId>jjwt</artifactId>
        <version>0.2</version>
    </dependency>

If your application to maven, then insert in pom.xml file

enter image description here

like image 1
jonathasborges1 Avatar answered Nov 07 '22 05:11

jonathasborges1