Java 弹簧靴2.0.0&;同一应用程序具有不同域的静态资源
我已经将一个web应用程序从Spring Boot 1.5.10迁移到2.0.0,它通过不同的域提供内容服务,并与Heroku一起部署。主域工作正常,但对于其他域,Javascript、CSS、图像和图标等静态元素不工作,浏览器会出现以下错误: 拒绝从“”执行脚本,因为其MIME类型('text/html')不可执行,并且启用了严格的MIME类型检查 静态资源位于:Java 弹簧靴2.0.0&;同一应用程序具有不同域的静态资源,java,spring-boot,spring-security,Java,Spring Boot,Spring Security,我已经将一个web应用程序从Spring Boot 1.5.10迁移到2.0.0,它通过不同的域提供内容服务,并与Heroku一起部署。主域工作正常,但对于其他域,Javascript、CSS、图像和图标等静态元素不工作,浏览器会出现以下错误: 拒绝从“”执行脚本,因为其MIME类型('text/html')不可执行,并且启用了严格的MIME类型检查 静态资源位于: /resources/static/css /resources/static/js /resources/static/imag
/resources/static/css
/resources/static/js
/resources/static/images
所有域都使用Heroku提供的Let's Encrypt SSL证书进行保护。所有域都被重定向,包括主域,并使用一个CNAME将其重定向到Heroku提供的地址
主域可以访问所有内容,次域只能访问目录中的内容。例如:
maindomain.com/1/
工作正常,但secondarydomain.com/1/
不工作
使次要域与主要域不同的是,通过实现HandlerInterceptor
,我们可以控制它们只能从其目录访问内容。这是来自预处理实现的代码:
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
serverName = request.getServerName();
if("POST".equals(request.getMethod())){
return true;
}
//We check if the request comes from one of the main domains
if(!checkRentalWebsURL(RwConstants.SERVER_NAMES, serverName)){
if(idweb == null){
Web web = webRepository.getWebByDomain(serverName);
if(web != null){
idweb = web.getIdweb();
} else {
response.sendRedirect(RwConstants.RW_URL);
}
}
String URI = request.getRequestURI();
String rootURI = "/" + idweb + "/";
if(URI.equals("/") || !URI.startsWith(rootURI)){
URI = rootURI;
RequestDispatcher requestDispatcher = request.getRequestDispatcher(request.getContextPath() + URI);
requestDispatcher.forward(request, response);
}
}
return true;
}
我已尝试在WebSecurityConfigurerAdapter的扩展中设置此代码来解决此问题
:
http
.authorizeRequests()
.requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll()
解决方案是在实现
webmvcconfiguer
时,在拦截器注册中排除不同静态资源的路径。这是代码的摘录:
public void addInterceptors(InterceptorRegistry registry) {
...
registry.addInterceptor(rootDomainInterceptor())
.excludePathPatterns("/js/**", "/css/**", "/images/**", "/webjars/**");
}
rootDomainInterceptor()
是HandleInterceptor
的一个实现,我用它来处理访问应用程序的不同域。我最近也遇到了类似的问题。我刚刚添加了整个资源目录
public void addInterceptors(InterceptorRegistry registry) {
...
registry.addInterceptor(rootDomainInterceptor())
.excludePathPatterns("/recources/**");
}
你找到解决方案了吗?@vashishth Iguan2 at给了我这个解决方案,我还没有测试:我发现迁移到2.0.1后,所有静态资源请求都会转到我的拦截器。在拦截器注册中排除静态资源路径之后,它对我有效。不确定这是否能帮到你。