我已经将Spring Boot Web应用程序从1.5.10迁移到2.0.0,它与Heroku一起部署并运行在多个域上。对于主域,这是第一个要设置的域,一切都在顺利进行,但其余的静态资源;像Javascript,CSS,图像和图标(Webjars)无法访问。
maindomain.com/js/example.js
工作正常,可以通过浏览器直接访问。浏览器无法访问secondarydomain.com/js/example.js
并且运行应用程序会出现此错误,我想因为而不是.js文件会返回一些短信:
拒绝从''执行脚本,因为它的MIME类型('text / html')不可执行,并且启用了严格的MIME类型检查
静态资源位于:
/resources/static/css
/resources/static/js
/resources/static/images
我已将Spring安全配置设置为WebSecurityConfigurerAdapter的扩展,我已撤消注释@EnableWebSecurity并添加了此代码,目的是确保这些资源可访问,但没有成功:
http
.authorizeRequests()
.requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll()
有一个HandleInterceptor,它处理每个辅助域可访问的目录。主要的一个,可以访问整个应用程序。 在另一个问题中,对同一问题采用不同的方法,有一个HandleInterceptor的摘录。
Spring Boot 2.0.0 & static resources with different domains for the same app
答案 0 :(得分:0)
带有启动功能的Spring Security位于类路径上,默认情况下,自动配置可保护所有端点。
但是,当涉及到复杂的应用程序时,我们需要每个端点使用不同的安全策略。我们还需要配置应保护哪些终结点,哪些类型的用户应能够访问该终结点以及哪些终结点应该是公共的。
WebSecurity
允许我们配置添加Spring Security应该忽略的RequestMatcher实例。
HttpSecurity
允许我们配置应该保护的端点和应该是公共的端点
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring()
.antMatchers("/resources/**");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/css/**", "/js/**", "/image/**"").permitAll()
}
}
希望有帮助。