首页 > 解决方案 > 重新加载在 docker 中运行的 Angular 应用程序会出现 500 内部服务器错误

问题描述

我正在开发一个 Angular 应用程序,BE 在 Java/Spring-Boot 中。不知何故,我能够在除 /management/** 之外的每个视图中执行重新加载或直接在浏览器中直接复制地址。

www.site-name.com/inscription(工作) www.site-name.com/management/utilisateurs(不工作)

这些是我在 Angular 中的路由配置

const routes: Routes = [
    {
        path: '',
        redirectTo: '/home',
        pathMatch: 'full'
    },
    {
        path: 'home',
        component: IntroPageComponent,
        canActivate: [HomeGuard]
    },
    {
        path: '',
        loadChildren: () => import('../user-auth/').then(module => module.UserAuthModule),
    },
    {
        path: 'inscription',
        loadChildren: () => import('../inscription').then(module => module.InscriptionModule),
        canActivateChild: [AuthenticatedGuard, InscriptionGuard]
    },
    {
        path: 'management',
        loadChildren: () => import('../management').then(module => module.ManagementModule),
        canActivateChild: [AuthenticatedGuard, ManagementGuard]
    },
    {
        path: '**',
        redirectTo: '/home'
    }
];

然后管理模块有自己的路由配置

const routes: Routes = [
  {
    path: 'utilisateurs',
    component: UsersComponent,
    resolve: { man : ManagementPreload }
  },
  "more routes here"
];

提供 index.html 的 Spring 控制器

 @GetMapping({
            "/",
            "/home",
            "/incription", 
            "/management/**",
            ......
    })
    public String index() {
        return "index";
    }

和 dockerfile

FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG JAR_FILE
ARG APP_NAME
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar", "--file:/app/config/application-siteinscription.properties", "-XX:+UseG1GC"]

标签: angulardockerspring-mvcspring-security

解决方案


我设法弄清楚问题是什么并实施了一个我不确定它是否是最合适的解决方案,但我会在这里为其他有同样问题的人发布它,也许可以就如何实施更好的解决方案获得一些反馈.

值得一提的是,角度路由配置没有任何问题。

该问题与 SpringMVC ViewResolver(s) 有关

根据我的配置

 @GetMapping({
            "/",
            "/home",
            "/incription", 
            "/management/**",
            ......
    })
    public String index() {
        return "index";
    }

当 Spring ViewResolver 看到类似的路由时

/management/**

它将尝试查找视图索引并根据配置返回它,例如

/path/to/resource/management/index.html

我所需要的只是无论路线总是返回

/path/to/resource/index.html

我设法使应用程序按照我的需要工作而没有太多开销的方法就是简单地实现控制器,如

@GetMapping({
        "/",
        "/home",
        "/auth",
        "/inscription",
        ...
})
public String index() {
    return "index";
}

@GetMapping("/management/**")
public ModelAndView forwardToHome() {
    return new ModelAndView("forward:/home", new ModelMap());
}

推荐阅读