带有Angular应用程序的tomcat 9服务器上的Spring Boot 404和CORS

Posted

技术标签:

【中文标题】带有Angular应用程序的tomcat 9服务器上的Spring Boot 404和CORS【英文标题】:Spring Boot 404 and CORS on tomcat 9 server with Angular application 【发布时间】:2021-12-01 05:23:01 【问题描述】:

我运行一个 WAMP 服务器和一个包含 Angular 应用程序和 Springboot 应用程序的 tomcat 服务器。我有 404,而我认为我满足所有要求,不会出现此类问题。同时,我的前端应用程序正在返回 cors 错误。

    在我的pom xml中添加依赖来生成war 更新我的主类以允许战争生成 管理对公共路线的访问 处理 cors 管理 在 WebSecurity 中管理 Cors

我有这个 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.5.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>fr.dsidiff.proxy.app</groupId>
    <artifactId>springboot-proxy</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-proxy</name>
    <description>springboot-proxy</description>
    <packaging>war</packaging>

    <properties>
        <java.version>11</java.version>
        <tomcat.version>9.0.54</tomcat.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-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-tomcat -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <!-- https://mvnrepository.com/artifact/io.jsonwebtoken/jjwt -->
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
            <version>0.9.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.dataformat/jackson-dataformat-xml -->
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
            <version>2.11.1</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.13</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.modelmapper</groupId>
            <artifactId>modelmapper</artifactId>
            <version>2.3.8</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-hateoas -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-hateoas</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>

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

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <finalName>$project.artifactId</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.5.3</version>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

我还在 application.properties 中定义了一些参数(甚至是上下文路径):

spring.datasource.username=root
spring.datasource.password=root
spring.datasource.url=jdbc:mysql://localhost:3306/my-db
spring.jpa.hibernate.ddl-auto=update
server.port=8080
spring.h2.console.enabled=true
server.servlet.context-path=/springboot-proxy

没什么特别的。由于要建jar,所以不忘修改主类:

@SpringBootApplication
public class SpringbootProxyApplication extends SpringBootServletInitializer 

    // allow to generate a war file
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) 
        return builder.sources(SpringbootProxyApplication.class);
    

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

这很好。由于它是一个休息 API,我还添加了一个 get 调用以用于测试目的:

@Slf4j
@RestController
@RequestMapping("/ws")
public class DummyController 
    @GetMapping
    public String dummy() throws IOException 
        return "Hello World";
    

我实现了 Spring Security,我希望这些路由是公开的,所以我实现了配置方法

@Override
protected  void configure(HttpSecurity http) throws Exception 
    // define public & private entry points
    http
            .cors().and()
            .csrf().disable()
            .authorizeRequests()
            .antMatchers(HttpMethod.GET, "/ws").permitAll()
            .antMatchers(HttpMethod.POST, "/ws/**").permitAll()

在运行我的 mvn clean 包时,我会构建我的 API。我确实使用tomcat管理器上传了我的jar。它工作正常。当然,我还添加了 Cors 管理...

我在我的网络安全中实现了这一点:

public CorsConfigurationSource corsConfigurationSource() 
    final CorsConfiguration corsConfiguration = new CorsConfiguration();
    final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();

    corsConfiguration.setAllowedOrigins(Collections.singletonList("*"));
    corsConfiguration.setAllowedMethods(Collections.singletonList("*"));
    corsConfiguration.setAllowCredentials(true);
    corsConfiguration.setAllowedHeaders(Arrays.asList("Authorization", "Cache-Control", "Content-Type"));

    source.registerCorsConfiguration("/**", corsConfiguration);
    return source;

如果还不够:

@Configuration
public class WebConfig implements WebMvcConfigurer 
    @Override
    public void addCorsMappings(CorsRegistry registry) 
        registry
                .addMapping("/**")
                .allowedMethods("*")
                .allowedOrigins("*");
    

在我面前我有这样的回应:

Access to XMLHttpRequest at 'http://localhost:8080/springboot-proxy/ws/getCategories' from origin 'http://localhost' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

当我尝试从我的 dummyController 调用 get 方法时,我遇到了一个不错的 404 错误。我真的不明白出了什么问题...

【问题讨论】:

【参考方案1】:

我在通过管理器上传战争文件之前运行了catalina.bat run,它成功了。

【讨论】:

以上是关于带有Angular应用程序的tomcat 9服务器上的Spring Boot 404和CORS的主要内容,如果未能解决你的问题,请参考以下文章

如何将带有 angular 2 前端(angular-cli build)的 spring-boot webapp 部署到 Tomcat?

将 WAR 部署到 Tomcat(Spring Boot + Angular)

如何在Tomcat 9的rewrite.config文件中为多个角度项目添加多个重写规则

尝试在外部在tomcat 9上运行带有rest控制器的spring boot war文件

Angular 9将新项目添加到列表而不刷新列表

运行 grunt 和 tomcat 服务器的 Web 应用程序(Spring、Angular、Grunt、Maven、Tomcat)