我一直在遵循将Spring Security与AngularJS集成的教程,但是它使用Spring Boot。我可以使教程示例正常工作,但是我需要将我的应用程序作为Tomcat下的常规MVC应用程序运行。
问题是使应用程序路由到初始视图的index.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文件的开头和结尾。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句