Spring Boot 应用程序在本地主机上运行,​​但在外部 Tomcat 上返回 404

时间:2021-04-21 05:41:12

标签: java spring-boot maven tomcat spring-security

我使用 Spring Security 创建了一个 Spring Boot 应用程序,以公开一些 REST API 端点。 我已经使用内置的 IntelliJ Ultimate Spring Initializr 创建了该项目。

Spring 版本:2.4.4
类型:Maven
Java版本:8
包装:战争

当我从 IntelliJ 中运行应用程序时,它会启动 Tomcat 9.0.44 的一个实例。
然后我使用 maven>package 创建了一个 WAR 文件,以在我 PC 的本地 XAMPP 安装上进行测试。 (我使用 Tomcat 8.5.53。原因是这是我最终要部署到的生产环境中可用的 Tomcat 版本。
我使用 tomcat 管理器进行部署,它在那里运行也没有任何问题。
然后我将战争转移到我们的生产服务器并使用 Tomcat 管理器 UI 进行部署。
我可以在管理器中看到它已部署并且正在运行。
生产服务器Tomcat Manager UI enter image description here

当我尝试使用 Postman 访问我的端点时,无论我尝试访问哪个端点,我总是收到 404 错误...

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.4.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.apiauthentication</groupId>
    <artifactId>auth-server</artifactId>
    <version>1.0.0</version>
    <packaging>war</packaging>
    <name>FLEET_Authentication_Server</name>
    <description>Authentication Server</description>
    <properties>
        <java.version>1.8</java.version>
        <jjwt.version>0.10.6</jjwt.version>
    </properties>
    <dependencies>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.12</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>


        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt-api</artifactId>
            <version>${jjwt.version}</version>
        </dependency>
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt-impl</artifactId>
            <version>${jjwt.version}</version>
        </dependency>
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt-jackson</artifactId>
            <version>${jjwt.version}</version>
        </dependency>


        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>2.10.10</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot</artifactId>
            <version>2.4.4</version>
        </dependency>


    </dependencies>

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

        <finalName>${artifactId}</finalName>
    </build>

</project>

主类-AuthServerApplication


@SpringBootApplication
public class AuthServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(AuthServerApplication.class, args);
    }
}

以下是 Tomcat 日志中的条目

catalina.log

21-Apr-2021 08:14:09.860 INFO [http-nio-8080-exec-2726] org.apache.catalina.startup.HostConfig.deployWAR Deploying web application archive /opt/tomcat/webapps/auth-server.war
21-Apr-2021 08:14:11.828 INFO [http-nio-8080-exec-2726] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
21-Apr-2021 08:14:16.257 INFO [http-nio-8080-exec-2726] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application archive /opt/tomcat/webapps/auth-server.war has finished in 6,397 ms

localhost.log

21-Apr-2021 08:14:11.839 INFO [http-nio-8080-exec-2726] org.apache.catalina.core.ApplicationContext.log 2 Spring WebApplicationInitializers detected on classpath
21-Apr-2021 08:14:13.419 INFO [http-nio-8080-exec-2726] org.apache.catalina.core.ApplicationContext.log Initializing Spring embedded WebApplicationContext

ma​​nager.log

21-Apr-2021 08:12:04.977 INFO [http-nio-8080-exec-2623] org.apache.catalina.core.ApplicationContext.log HTMLManager: list: Listing contexts for virtual host 'localhost'
21-Apr-2021 08:14:09.806 INFO [http-nio-8080-exec-2726] org.apache.catalina.core.ApplicationContext.log HTMLManager: install: Installing web application '/auth-server' from '/tmp-war/auth-server.war'
21-Apr-2021 08:14:16.257 INFO [http-nio-8080-exec-2726] org.apache.catalina.core.ApplicationContext.log HTMLManager: list: Listing contexts for virtual host 'localhost'

localhost_access_log 文件中,我看不到我尝试访问新部署的应用程序的条目。

我尝试过的一些事情:

  • 在我的主类上扩展 SpringBootServletInitializer 并覆盖配置方法
@SpringBootApplication
public class AuthServerApplication extends SpringBootServletInitializer {
    public static void main(String[] args) {
        SpringApplication.run(AuthServerApplication.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(AuthServerApplication.class);
    }
}
  • 在 pom.xml 的属性部分添加 start-class

    <properties>
        ...
        <start-class>
            com.apiauthentication.authserver.AuthServerApplication
        </start-class>
    </properties>
  • 向 spring-boot-starter-web 添加排除项,用于 spring-boot-starter-tomcat
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
  • 还有这些东西的不同组合

有人对可能是什么问题有任何提示吗? 基本上我可以在本地 XAMPP 上运行,但不能在生产服务器上运行

1 个答案:

答案 0 :(得分:1)

添加来自 Piotr P. Karwasz 的评论作为答案。

<块引用>

似乎奇怪的是访问日志中缺少条目。您确定您的生产服务器没有使用反向代理吗?

问题是我试图使用域名访问应用程序,但由于该域是使用 Plesk 管理的,并且该服务器上还有其他域,我必须转到该域的 Apache 设置并为 HTTP 和 HTTPS 添加一些新的 ProxyPass 和 ProxyPassReverse 指令