gpt4 book ai didi

java - 尝试调用不存在的方法。超导系统

转载 作者:行者123 更新时间:2023-12-01 17:43:18 26 4
gpt4 key购买 nike

当我运行 STS(SpringBoot) 应用程序时,出现以下错误:

The attempt was made from the following location:      org.apache.catalina.authenticator.AuthenticatorBase.startInternal(AuthenticatorBase.java:1321)

以下方法不存在:

javax.servlet.ServletContext.getVirtualServerName()Ljava/lang/String;

该方法的类 javax.servlet.ServletContext 可从以下位置获取:

jar:file:/home/talha/.m2/repository/javax/servlet/servlet-api/2.5/servlet-api-2.5.jar!/javax/servlet/ServletContext.class
jar:file:/home/talha/.m2/repository/org/apache/tomcat/embed/tomcat-embed-core/9.0.33/tomcat-embed-core-9.0.33.jar!/javax/servlet/ServletContext.class

它是从以下位置加载的:

file:/home/talha/.m2/repository/javax/servlet/servlet-api/2.5/servlet-api-2.5.jar

ide中的建议是:行动:

更正应用程序的类路径,使其包含单个兼容版本的 javax.servlet.ServletContext

我猜我的pom.xml有问题,代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.uni</groupId>
<artifactId>authorize</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>authorize</name>
<description>API for user registration and login with validation</description>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/com.sun.mail/javax.mail -->
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-mongodb -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/joda-time/joda-time -->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
<dependency>
<groupId>com.googlecode.jsontoken</groupId>
<artifactId>jsontoken</artifactId>
<version>1.0</version>
</dependency>
<!-- Thanks for using https://jar-download.com -->

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<build>
<finalName>authrize</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

导致错误的主要依赖项是:

<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
<dependency>
<groupId>com.googlecode.jsontoken</groupId>
<artifactId>jsontoken</artifactId>
<version>1.0</version>
</dependency>

添加上述依赖项后才出现错误,需要添加依赖项的是以下类:

package com.uni.authorize.service;

import java.security.InvalidKeyException;
import java.security.SignatureException;
import java.util.List;

import org.joda.time.DateTime;
import org.joda.time.Instant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import com.google.gson.JsonObject;
import com.uni.authorize.model.TokenKeys;
import com.uni.authorize.pojo.TokenObject;
import com.uni.authorize.repository.TokenKeysRepository;
import com.uni.authorize.service.CreateToken;

import net.oauth.jsontoken.JsonToken;
import net.oauth.jsontoken.crypto.HmacSHA256Signer;

@Configuration
public class CreateToken {

private static final Logger LOGGER = LoggerFactory.getLogger(CreateToken.class);

private static final String ISSUER = "UnI United Tech";

@Autowired
TokenKeysRepository tokenKeyRepository;

public TokenObject createToken(String userId) {

List<TokenKeys> tokenList = tokenKeyRepository.findAll();
TokenKeys tokenKeys = tokenList.get(0);

long accessTokenValidity = tokenKeys.getAccessTokenValidity();
long refreshTokenValidiry = tokenKeys.getRefreshTokenValidity();

String accessTokenKey = tokenKeys.getAccessKey();

String refreshTokenKey = tokenKeys.getRefreshKey();

String accessToken = generateAccessToken(userId, accessTokenKey, accessTokenValidity);
String refreshToken = generateRefreshToken(userId, refreshTokenKey, refreshTokenValidiry);

TokenObject tokenObject = new TokenObject();

tokenObject.setAccess_token(accessToken);
tokenObject.setRefresh_token(refreshToken);
tokenObject.setToken_type("bearer");
tokenObject.setExpires_in(accessTokenValidity);
tokenObject.setScope("read write trust");
return tokenObject;
}

private String generateAccessToken(String userId, String accessTokenKey, long accessTokenValidity) {

HmacSHA256Signer signer;

try {
signer = new HmacSHA256Signer(ISSUER, null, accessTokenKey.getBytes());
} catch (InvalidKeyException e) {
throw new RuntimeException(e);
}

// Configure JSON token
JsonToken token = new net.oauth.jsontoken.JsonToken(signer);
// token.setAudience(AUDIENCE);

DateTime dateTime = new DateTime();

long dateTimeMillis = dateTime.getMillis();

// DateTime currentTimeDateTime = new DateTime(dateTimeMillis);
// DateTime expiryTimeDateTime = new DateTime(dateTimeMillis + accessTokenValidity);

Instant currentTimeInstant = new org.joda.time.Instant(dateTimeMillis);
Instant expirationTimeInstant = new org.joda.time.Instant(dateTimeMillis + accessTokenValidity);

LOGGER.debug("Current Time Instant" + currentTimeInstant);
LOGGER.debug("Expiration Tine Instant" + expirationTimeInstant);

token.setIssuedAt(currentTimeInstant);
token.setExpiration(expirationTimeInstant);

// Configure request object, which provides information of the item
JsonObject request = new JsonObject();
request.addProperty("userId", userId);

JsonObject payload = token.getPayloadAsJsonObject();
payload.add("info", request);

try {
return token.serializeAndSign();
} catch (SignatureException e) {
throw new RuntimeException(e);
}
}

private String generateRefreshToken(String userId, String refreshTokenKey, long refreshTokenValidiry) {
HmacSHA256Signer signer;

try {
signer = new HmacSHA256Signer(ISSUER, null, refreshTokenKey.getBytes());
} catch (InvalidKeyException e) {
throw new RuntimeException(e);
}

// Configure JSON token
JsonToken token = new net.oauth.jsontoken.JsonToken(signer);
// token.setAudience(AUDIENCE);

DateTime dateTime = new DateTime();

long dateTimeMillis = dateTime.getMillis();

// DateTime currentTimeDateTime = new DateTime(dateTimeMillis);
// DateTime expiryTimeDateTime = new DateTime(dateTimeMillis + refreshTokenValidiry);

Instant currentTimeInstant = new org.joda.time.Instant(dateTimeMillis);
Instant expirationTimeInstant = new org.joda.time.Instant(dateTimeMillis + refreshTokenValidiry);

LOGGER.debug("Current Time Instant" + currentTimeInstant);
LOGGER.debug("Expiration Tine Instant" + expirationTimeInstant);

token.setIssuedAt(currentTimeInstant);
token.setExpiration(expirationTimeInstant);

// Configure request object, which provides information of the item
JsonObject request = new JsonObject();
request.addProperty("userId", userId);

JsonObject payload = token.getPayloadAsJsonObject();
payload.add("info", request);

try {
return token.serializeAndSign();
} catch (SignatureException e) {
throw new RuntimeException(e);
}
}

}

请帮我解决这个问题。提前致谢

最佳答案

getVirtualServerName() 已添加于 Servlet 3.1 ,但你包括了 servlet-api-2.5.jar是您的应用程序。

选项:

  • 更改您的依赖项以包含 servlet-api-3.1.jar (或稍后)

  • 删除 servlet-api-2.5.jar依赖项,因为嵌入式 Tomcat 文件 ( tomcat-embed-core-9.0.33.jar ) 中包含正确的版本。

实际上,您永远不应该发货 servlet-api.jar与您的应用程序一起使用,因为它将由 Servlet 容器提供。看来你失踪了<scope>provided</scope>servlet-api 的依赖标记中文件。

关于java - 尝试调用不存在的方法。超导系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60909834/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com