简体   繁体   English

难以在 Spring Boot Gradle 项目中导入 JWT(JSON Web Token)

[英]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:我有一个 Spring Boot gradle 项目,在 build.gradle 依赖项中,我将 JSON Web Token 导入为:

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.在 Spring Security 和视频教程之后,我构建了一个成功的身份验证方法。 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.但是,当我在此方法中使用 Jwts 时,它不会提示我导入与视频教程中相同的文件,实际上也没有任何导入选项的提示。 Specifically, in the video (which uses Maven), the prompt is added to import:具体在视频中(使用Maven),添加了import的提示:

import io.jsonwebtoken

Yet in my app I am never given this option and importing this manually or as import io.jsonwebtoken.*;然而在我的应用程序中,我从来没有得到这个选项并手动导入它或作为 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.同样,SignatureAlgorithm 方法不包含来自 JWT 的导入。

How can I successfully import JSON web token into my gradle project (or at least import io).我怎样才能成功地将 JSON 网络令牌导入我的 gradle 项目(或至少导入 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. Jwts 是网络令牌的实现,它是我遇到困难的包。

@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:我放在一起的项目本身在 Github 上,上面提到的类在:

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

So I got your problem you should use 0.2 instead of 0.2.0所以我遇到了你的问题,你应该使用 0.2 而不是 0.2.0

For gradle 4.10 it would be good to use implementation instead of compile.对于 gradle 4.10,最好使用实现而不是编译。

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

在此处输入图像描述

Following errors I observed from your github project repo:我从您的 github 项目回购中观察到以下错误:
1. need to add import io.jsonwebtoken.SignatureAlgorithm; 1.需要添加import io.jsonwebtoken.SignatureAlgorithm; in AuthenticationFilter.java在 AuthenticationFilter.java 中
2. UserService.java has method UserDetails loadUserByUsername(String email) throws Exception; 2. UserService.java 有方法UserDetails loadUserByUsername(String email) throws Exception; which is similar to UserDetails loadUserByUsername(String username) throws UsernameNotFoundException;这类似于UserDetails loadUserByUsername(String username) throws UsernameNotFoundException; in UserDetailsService interface, I removed since both serves same purposeUserDetailsService接口中,我删除了,因为两者的用途相同
3. Updated method loadUserByUsernam e in UserServiceImpl.java and throws UsernameNotFoundException exception instead of Exception (since the method is derived from UserDetailsService interface) 3. 更新UserServiceImpl.java中的方法loadUserByUsernam并抛出UsernameNotFoundException异常而不是Exception (因为该方法是从UserDetailsService接口派生的)

I raised merge request to your repo and changes are: https://github.com/jwolfe890/SpringBootProject1/pull/1/files我向您的回购提出了合并请求,更改是: 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它在这里工作 - 参考 - 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如果你的应用程序是 maven,那么插入pom.xml文件

在此处输入图像描述

For others seeking answer when JWT token dependency not getting imported: it seems like a bug in IntelliJ.对于其他在未导入 JWT 令牌依赖项时寻求答案的人:这似乎是 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.我能够将这些依赖项成功添加到 IntelliJ IDE 中的 maven 项目中。

    <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并在 gradle 文件中添加以下行

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

and it worked for me.它对我有用。 thanks.谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM