我正在尝试了解 Java Web 令牌的用法,所以我想制作一个简单的 Web 应用程序。我只是创建了一个使用 jwt 的简单登录系统,但我明白了java.lang.NoClassDefFoundError: io/jsonwebtoken/Jwts
。这是我的相关代码:
连接.java:
@WebServlet("/Connect")
public class Connect extends HttpServlet {
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
String username = req.getParameter("username");
String password = req.getParameter("password");
Jjwt jjwt = new Jjwt();
jjwt.authorize(username, password);
if(jjwt.flag == true){
DatabaseInteractions databaseInteractions = new DatabaseInteractions();
JsonOutput output = databaseInteractions.getAccountDetails(databaseInteractions.getID(username));
ServletContext context = getServletContext();
req.setAttribute("username", username);
req.setAttribute("btcAmount", output.getBtcAmount());
req.setAttribute("ethAmount", output.getEthAmount());
req.setAttribute("xrpAmount", output.getXrpAmount());
req.setAttribute("trxAmount", output.getTrxAmount());
req.setAttribute("adaAmount", output.getAdaAmount());
context.getRequestDispatcher("/Home.jsp").forward(req, resp);
}
}
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
}
}
Jjwt.java:
public class Jjwt{
public boolean flag;
public Jjwt(){
flag = false;
}
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response authorize(String username, String password){
DatabaseInteractions databaseInteractions = new DatabaseInteractions();
if(databaseInteractions.connectToDatabase() == false){
System.out.println("Error: Could not connect to database. Please try later ...");
return Response.status(Response.Status.UNAUTHORIZED).build();
}
if(databaseInteractions.login(username, password)){
String key = "key";
long time = System.currentTimeMillis();
String jwt = Jwts.builder()
.signWith(SignatureAlgorithm.HS256, key)
.setSubject(username)
.setIssuedAt(new Date(time))
.setExpiration(new Date(time+9000))
.claim("password",password)
.compact();
JsonObject jsonObject = Json.createObjectBuilder().add("JWT", jwt).build();
flag = true;
return Response.status(Response.Status.CREATED).entity(jsonObject).build();
}else{
return Response.status(Response.Status.UNAUTHORIZED).build();
}
}
}
在String jwt = Jwts.builder()
Jjwt.java 类中,我得到了上面提到的错误。我添加了所有必要的依赖项,这是我的 pom.xml(顺便说一下,这是一个 Maven 项目):
<repositories>
<repository>
<id>maven2-repository.java.net</id>
<name>Java.net Repository for Maven</name>
<url>http://download.java.net/maven/2/</url>
<layout>default</layout>
</repository>
<repository>
<id>maven-repository.java.net</id>
<name>Java.net Maven 1 Repository (legacy)</name>
<url>http://download.java.net/maven/1</url>
<layout>legacy</layout>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.9</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.6</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.6</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.6</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-servlet-api</artifactId>
<version>8.5.27</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.1-m09</version>
</dependency>
<dependency>
<groupId>javax.json</groupId>
<artifactId>javax.json-api</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.0</version>
</dependency>
</dependencies>
我在哪里做错了?
编辑:当我添加以下依赖项时:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.9</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.9</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.8.9</version>
</dependency>
现在它java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/JsonProcessingException
在同一点给出。