Spring Boot 不会将 url 路径转发到 index.html React 包 [英] Spring boot not forwarding url path to index.html React bundle

查看:70
本文介绍了Spring Boot 不会将 url 路径转发到 index.html React 包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Spring boot 为我的 REST API 部署了一个 JAR.该应用程序还通过 /static 目录提供 React Web 应用程序包.

I deployed a JAR for my REST API using Spring boot. The app also serves the React web app bundle through the /static directory.

问题是当我尝试输入此链接时:http://SERVER-IP/password/change/1d0afe95-a643-437e-8307-ed7688b6a756

The problem is that when I try to enter this link: http://SERVER-IP/password/change/1d0afe95-a643-437e-8307-ed7688b6a756

我看到 Whitelabel Error Page 404 消息被映射而不是预期的页面.

I see Whitelabel Error Page 404 message mapped instead of the expected page.

这个问题不会发生在我的本地环境中,只是在生产环境中(Ubuntu 16 vps).

This issue isn't happening in my local environment, just in production (Ubuntu 16 vps).

这是我的 WebMvcConfig 类的代码:

Here is the code of my WebMvcConfig class:

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    private final long MAX_AGE_SECS = 3600;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("HEAD", "OPTIONS", "GET", "POST", "PUT", "PATCH", "DELETE")
                .maxAge(MAX_AGE_SECS);
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/{spring:\\w+}")
                .setViewName("forward:/");
        registry.addViewController("/**/{spring:\\w+}")
                .setViewName("forward:/");
        registry.addViewController("/{spring:\\w+}/**{spring:?!(\\.js|\\.css)$}")
                .setViewName("forward:/");
    }

}

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 http://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.1.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>XXX</groupId>
    <artifactId>XXX</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>XXX</name>
    <description>Demo project for Spring Boot</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-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-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- For Working with Json Web Tokens (JWT) -->
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
            <version>0.9.0</version>
        </dependency>
        <!-- For Java 8 Date/Time Support -->
        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-jsr310</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
            <version>2.0.1.RELEASE</version>
        </dependency>
    </dependencies>

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

</project>

这是路由的javascript代码:

And here is the javascript code for the routes:

export const routes = (
  <Switch>
    <PrivateRoute exact path="/" component={Routines} />
    <PrivateRoute exact path="/home" component={Routines} />
    <Route exact path="/login" component={Login} />
    <Route exact path="/signup" component={Signup} />
    <PrivateRoute exact path="/password/change" component={ChangePassword} />
    <Route exact path="/password/change/:token" component={ChangePassword} />
    <Route exact path="/password/reset" component={ResetPassword} />
    <Route component={NotFoundPage} />
  </Switch>
);

所有其他路由都可以通过 React 路由或通过在浏览器中插入 URL 来正常工作

All other routes are working fine either by React routing or by inserting the URL in the browser

如何将所有路由转发到 index.html,即使在生产中?

How can I do to forward all routes to index.html, even in production?

推荐答案

问题出在 WebMvcConfig 类中 addViewControllers 方法中的 regexes.

The problem was the regexes at addViewControllers method in WebMvcConfig class.

特别是,\\w 只匹配字母数字,但不匹配连字符.出于一个奇怪的原因,可能是因为浏览器缓存,它在开发环境中工作.

In particular, \\w was only matching alphabets and numbers, but it wasn't matching hyphens. For a strange reason, which may be because of browser caching, it worked in the dev environment.

这是正确的 regex :^[a-zA-Z\d-_] 及以下,正确的 addViewControllers 方法:

Here is the correct regex : ^[a-zA-Z\d-_] and below, the correct addViewControllers method:

@Override
public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/{spring:^[a-zA-Z\\d-_]+}")
            .setViewName("forward:/");
    registry.addViewController("/**/{spring:^[a-zA-Z\\d-_]+}")
            .setViewName("forward:/");
    registry.addViewController("/{spring:^[a-zA-Z\\d-_]+}/**{spring:?!(\\.js|\\.css)$}")
            .setViewName("forward:/");
}

这篇关于Spring Boot 不会将 url 路径转发到 index.html React 包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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