Spring jHipster中具有角度4的布线
在JHipster的帮助下,我刚刚在Angular 4开始了一个新项目 开箱即用,Jhipster使用的是Spring jHipster中具有角度4的布线,spring,angular,spring-mvc,angular-ui-router,jhipster,Spring,Angular,Spring Mvc,Angular Ui Router,Jhipster,在JHipster的帮助下,我刚刚在Angular 4开始了一个新项目 开箱即用,Jhipster使用的是HashLocationStrategy,因此我的URL看起来是这样的: localhost:8080/#/url 现在,对于我的项目,我想使用Html5 pushstate,并添加如下前缀: localhost:8080/myproject/url 为此,我做了以下修改: application.yml 在这里,我只是添加了一行“上下文路径:/myproject” server:
HashLocationStrategy
,因此我的URL看起来是这样的:
localhost:8080/#/url
现在,对于我的项目,我想使用Html5 pushstate,并添加如下前缀:
localhost:8080/myproject/url
为此,我做了以下修改:
application.yml 在这里,我只是添加了一行“上下文路径:/myproject”
server:
session:
cookie:
http-only: true
-> context-path: /myproject
*.module.ts 在每个包含以下内容的.module.ts文件中
RouterModule.forRoot(accountState,{useHash:true})
,我删除了{useHash:true}
-部分
注意:jHipster项目中的每个RouterModule默认使用.forRoot
这已经产生了巨大的影响,因为我现在可以通过
localhost:8080/myproject/
打开我的页面并在页面中导航
为了从带有书签的链接(如localhost:8080/myproject/url
)成功打开页面,我创建了以下Java文件,遵循最初用于AngularJS的思想
Routes.java
package my.company.mc.config;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class Routes {
@RequestMapping(value = "/{[path:[^.]*}")
public String redirect(){
return "forward:/";
}
}
(...)
protected void configure(HttpSecurity http) throws Exception {
http
.csrf()
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
.and()
.addFilterBefore(corsFilter, CsrfFilter.class)
.exceptionHandling()
.authenticationEntryPoint(http401UnauthorizedEntryPoint())
-> .and()
-> .addFilterBefore(new AngularRouteFilter(), FilterSecurityInterceptor.class)
-> .exceptionHandling()
-> .authenticationEntryPoint(http401UnauthorizedEntryPoint())
.and()
.rememberMe()
.rememberMeServices(rememberMeServices)
.rememberMeParameter("remember-me")
.key(jHipsterProperties.getSecurity().getRememberMe().getKey())
(...)
从现在开始,大部分事情似乎都很顺利,直到今天我注意到,我无法直接访问像
localhost:8080/myproject/entity/{id}
每当我试图直接访问它时,我都会从我的SpringWebServer获得一个404
我怀疑这与我的Routes.Java文件有关,实际上它没有通过index.html正确地路由每个直接页面访问
我已经尝试了一些方法,但总是无法真正解决我的问题,而且因为我对Angular 4和Spring仍然很新鲜,所以我开始厌倦在谷歌上搜索可能的解决方案,这最终不起作用,因为从一开始,这个项目就有太多的差异
我希望这里有人能帮助我,如果你遗漏了任何信息,请告诉我,我会很乐意遵守
1。编辑: 我实现了如Gaël所示的过滤器类,并将其添加到我的SecurityConfiguration.java中,如下所示:
SecurityConfigurations.java
package my.company.mc.config;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class Routes {
@RequestMapping(value = "/{[path:[^.]*}")
public String redirect(){
return "forward:/";
}
}
(...)
protected void configure(HttpSecurity http) throws Exception {
http
.csrf()
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
.and()
.addFilterBefore(corsFilter, CsrfFilter.class)
.exceptionHandling()
.authenticationEntryPoint(http401UnauthorizedEntryPoint())
-> .and()
-> .addFilterBefore(new AngularRouteFilter(), FilterSecurityInterceptor.class)
-> .exceptionHandling()
-> .authenticationEntryPoint(http401UnauthorizedEntryPoint())
.and()
.rememberMe()
.rememberMeServices(rememberMeServices)
.rememberMeParameter("remember-me")
.key(jHipsterProperties.getSecurity().getRememberMe().getKey())
(...)
此外,我现在删除了上下文路径,因为在进行任何更改之前,我首先想实现一个“香草”版本
遗憾的是,我不能让它工作。我尝试将过滤器放置在过滤器链的不同位置。我还尝试删除了我的Routes.java,因为我不确定它到底是如何与新过滤器交互的
最后,我认为可能是我对Angular Routing如何与Spring后端交互(反之亦然)缺乏理解,这妨碍了我找到合适的解决方案。尝试调整我的评论中的filter类,它不是为上下文路径创建的,但您可以调整它,不过,可能还有更多的案例需要测试。@GaëlMarziou我曾尝试实现此解决方案,但无法使其工作。我编辑了我的帖子以反映我的过期。似乎仅仅在后端更改内容是不够的。或者我的缺乏经验可能会让我退缩/我已经很久没有尝试过了,我可能已经在WebConfigurer.java中重新注册了过滤器?你检查过你的过滤器是在调试器下调用的吗?@GaëlMarziou尝试在WebConfigure中注册过滤器,但这只会导致网站索引不再加载,尽管我可能做错了<代码>@Bean public FilterRegistrationBean angularRouterFilter(){FilterRegistrationBean registrationBean=new FilterRegistrationBean();Filter angularRouterFilter=new angularRouterFilter();beanFactory.autowireBean(angularRouterFilter);registrationBean.setFilter(angularRouterFilter);registrationBean.addUrlPatterns(“/*”);return registrationBean;}即使未在WebConfigure中注册过滤器,也会调用它。所以我认为这不是问题所在。谢谢你的快速帮助,很高兴看到JHipster团队在StackOverflow上如此活跃!尝试在我的注释中调整筛选器类它不是为上下文路径创建的,但您可以调整它,但可能还有更多的情况需要测试。@GaëlMarziou我尝试实现此解决方案,但无法使其工作。我编辑了我的帖子以反映我的过期。似乎仅仅在后端更改内容是不够的。或者我的缺乏经验可能会让我退缩/我已经很久没有尝试过了,我可能已经在WebConfigurer.java中重新注册了过滤器?你检查过你的过滤器是在调试器下调用的吗?@GaëlMarziou尝试在WebConfigure中注册过滤器,但这只会导致网站索引不再加载,尽管我可能做错了<代码>@Bean public FilterRegistrationBean angularRouterFilter(){FilterRegistrationBean registrationBean=new FilterRegistrationBean();Filter angularRouterFilter=new angularRouterFilter();beanFactory.autowireBean(angularRouterFilter);registrationBean.setFilter(angularRouterFilter);registrationBean.addUrlPatterns(“/*”);return registrationBean;}即使未在WebConfigure中注册过滤器,也会调用它。所以我认为这不是问题所在。谢谢你的快速帮助,很高兴看到JHipster团队在StackOverflow上如此活跃!