我一直在遵循将Spring Security与AngularJS集成的教程,但它使用Spring Boot。我可以让教程示例正常工作,但我需要我的应用程序在 Tomcat 下作为常规 MVC 应用程序运行。
问题是让应用程序路由到初始视图的索引.html页。我在控制器中拥有的唯一映射是我想从 Angular 接收的 REST 调用,但我似乎无法让应用程序转到索引页。Spring Boot 会自动执行此操作,但我将在 Tomcat 下将其作为 Web 应用程序运行。尝试直接访问那里会导致"找不到映射"错误。
我正在使用 Java 配置,并具有本教程中所述的 antMatchers 等。
以下是我的配置类中的一些条目来实现这一点。
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/index.html", "/home.html", "/login.html", "/").permitAll()
.antMatchers("/css/**").permitAll()
.antMatchers("/js/**").permitAll()
.antMatchers(HttpMethod.POST, "/user").permitAll().anyRequest()
.authenticated().and()
.csrf()
.csrfTokenRepository(csrfTokenRepository()).and()
.addFilterAfter(csrfHeaderFilter(), CsrfFilter.class);
if ("true".equals(System.getProperty("httpsOnly"))) {
LOGGER.info("launching the application in HTTPS-only mode");
http.requiresChannel().anyRequest().requiresSecure();
}
}
@Configuration
@EnableWebMvc
@ComponentScan("com.mygroupnotifier.controller")
public class ServletContextConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/css/**").addResourceLocations("/resources/static/css/");
registry.addResourceHandler("/js/**").addResourceLocations("/resources/static/js/");
registry.addResourceHandler("/*.html").addResourceLocations("/resources/static/");
}
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("forward:/index.html");
}
}
像往常一样,最困难的部分是在类和html文件上获取前导和结尾/。