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:
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 loadUserByUsernam
e 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.
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
For others seeking answer when JWT token dependency not getting imported: it seems like a bug in IntelliJ.
Add the dependency to another project and try again.
I am able to add these dependencies successfully to the maven project in IntelliJ IDE.
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
</dependency>
I ran below command inside project folder
gradlew clean build --refresh-dependencies
and added below line in gradle file
compile group: 'io.jsonwebtoken', name: 'jjwt', version: '0.9.1'
and it worked for me. thanks.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.