Whitelabel 错误页面只是在 URL 上输入?

Whitelabel Error Page just type on URL?

Spring-boot 项目与 Angular 4 集成。我可以在输入 url 地址时到达主页 >> localhost:8080.

当输入任何不同的 url 添加参数时,我得到 whitelabel 错误页面,例如:/login,/signup

There was an unexpected error (type=Not Found, status=404).

我可以使用 angular-routing 在网站上访问这些页面(localhost:8080/signuplocalhost:8080/foo、..)。所以问题是只直接点击 url。

所以我该如何解决这个问题,任何检查的想法都会有所帮助。

注意:这些url在服务器端没有授权。

编辑:index.html 添加了路径。

src/main/resources
  static
    assets
    index.html
    bundle
    bundle
    ..

routing.ts

export const routes: Routes = [
  {
    path: '',
    component: HomeComponent,
    pathMatch: 'full'
  },
  {
    path:'signup',
    component: SignupComponent,
    canActivate: [GuestGuard],
    pathMatch:'full'
  },

创建控制器并将每个请求转发到 index.html,如下所示,

@RequestMapping({  "/signup", "/purchase", "/credit"})
public String index() {
    return "forward:/index.html";
}

通过这样做,angular 将选择 url 并导航到相应的页面。另外请根据需要修改请求urls。每当直接从浏览器命中请求时,它会被 spring 引导拾取并且 spring 引导不知道 angular 路由 urls。为了防止这种情况,我们需要将所有请求转发到 index.html.

或者您可以按照@Vikas 的建议使用HashLocationStrategy

我已经直接实现了这个 WebMvcConfigurer,它非常适合我。这是引用 .

import java.io.IOException;

import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.resource.PathResourceResolver;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

      registry.addResourceHandler("/**/*")
        .addResourceLocations("classpath:/static/")
        .resourceChain(true)
        .addResolver(new PathResourceResolver() {
            @Override
            protected Resource getResource(String resourcePath,
                Resource location) throws IOException {
                Resource requestedResource = location.createRelative(resourcePath);
                return requestedResource.exists() && requestedResource.isReadable() ? requestedResource
                : new ClassPathResource("/static/index.html");
            }
        });
    }
}