试图调用不存在的方法.STS [英] An attempt was made to call a method that does not exist. STS

查看:95
本文介绍了试图调用不存在的方法.STS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When i run the STS(SpringBoot) application i get the below error:

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 可从以下位置获得:

The method's class, javax.servlet.ServletContext, is available from the following locations:

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

它是从以下位置加载的:

It was loaded from the following location:

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

ide中的建议是:行动:

The suggestion in the ide is: Action:

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

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

I guess there is something wrong with my pom.xml the code is as below:

<?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>

导致错误的主要依赖是:

The main dependencies causing the error are:

<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>

我添加了上面的依赖后才得到这个错误,需要添加的依赖是下面的类:

I get the error only after adding the above dependencies, the need to add the dependencies is the below class:

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 是您的应用程序.

getVirtualServerName() was added in Servlet 3.1, but you included servlet-api-2.5.jar is your application.

选项:

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

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

Remove the servlet-api-2.5.jar dependency, since the correct version is included in the Embedded Tomcat file (tomcat-embed-core-9.0.33.jar).

实际上,您永远不应该将 servlet-api.jar 与您的应用程序一起发送,因为它将由 Servlet 容器提供.似乎您在 servlet-api 文件的依赖项标记中缺少 provided.

Actually, you should never ship servlet-api.jar with your application, since it will be provided by the Servlet Container. Seems you're missing <scope>provided</scope> in your dependency tag for the servlet-api file.

这篇关于试图调用不存在的方法.STS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆