我有一个 Spring Boot gradle 项目,在 build.gradle 依赖项中,我将 JSON Web Token 导入为:
compile group: 'io.jsonwebtoken', name: 'jjwt', version: '0.2'
根据 Spring Security 和视频教程,我构建了一个成功的身份验证方法。但是,当我在这种方法中使用 Jwts 时,它不会提示我导入与视频教程中相同的文件,并且实际上没有任何导入选项的提示。具体来说,在视频中(其中使用了Maven),添加了import的提示:
import io.jsonwebtoken
然而,在我的应用程序中,我从未获得过此选项并手动导入或导入 io.jsonwebtoken.*;不起作用。正如类(class)指出的那样:
import io cannot be resolved
同样,SignatureAlgorithm 方法不包含来自 JWT 的导入。
如何将 JSON Web token 成功导入到我的 gradle 项目中(或至少导入 io)。视频教程中的方法如下。 Jwts 是网络 token 的实现,它是我遇到困难的包。
@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);
}
我正在整理的项目本身在 Github 上,上面提到的类(class)在:
https://github.com/jwolfe890/SpringBootProject1/blob/master/src/main/java/sbootproject/security/AuthenticationFilter.java
最佳答案
关于java - 在 Spring Boot Gradle 项目中导入 JWT(JSON Web Token)有困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53809244/