Java 您应该在哪里配置内容安全策略?
我有一个angular应用程序,它根据设置与Tomcat上的REST API或Jetty上的REST API进行通信。angular应用程序本身与war托管在同一个tomcat/jetty上 Tomcat安装程序前面可能有一个Apache(取决于客户端) 应用程序需要使用base64图像(通过css加载),但是现在,如果它托管在服务器上,我会得到以下错误:Java 您应该在哪里配置内容安全策略?,java,angularjs,spring,tomcat,content-security-policy,Java,Angularjs,Spring,Tomcat,Content Security Policy,我有一个angular应用程序,它根据设置与Tomcat上的REST API或Jetty上的REST API进行通信。angular应用程序本身与war托管在同一个tomcat/jetty上 Tomcat安装程序前面可能有一个Apache(取决于客户端) 应用程序需要使用base64图像(通过css加载),但是现在,如果它托管在服务器上,我会得到以下错误: Refused to load the image 'data:image/png;base64,...' because it viola
Refused to load the image 'data:image/png;base64,...' because it violates the following Content Security Policy directive: "default-src https:". Note that 'img-src' was not explicitly set, so 'default-src' is used as a fallback.
所以我所做的:
在index.html中,我设置了:
<meta http-equiv="Content-Security-Policy"
content="default-src https: http:; script-src 'self' 'unsafe-inline' 'unsafe-eval' https: http:; style-src http: https: 'unsafe-inline'; img-src 'self' data: https:; connect-src http: https: ws:;">
httpServletResponse.setHeader("Content-Security-Policy",
"default-src https: http:; script-src 'self' 'unsafe-inline' 'unsafe-eval' https: http:; style-src http: https: 'unsafe-inline'; img-src 'self' data: https:; connect-src http: https: ws:;");
但当然,这没有任何效果,因为我没有为html/js/css调用API
据我所知,这并不意味着要在Tomcat上配置。
您通常在哪里配置内容安全策略/内容安全策略头
我需要一个解决方案,它不需要在安装文件的服务器上进行手动配置
提前谢谢 Spring Security允许用户轻松地插入安全头,以帮助保护其应用程序。以下是内容安全策略的示例。需要注意的是,SpringSecurity默认情况下不添加内容安全策略。web应用程序作者必须声明要强制执行和/或监视受保护资源的安全策略。您可以使用Java配置启用CSP标头,如下所示:
@EnableWebSecurity
public class WebSecurityConfig extends
WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
// ...
.headers()
.contentSecurityPolicy("script-src 'self' https://trustedscripts.example.com; object-src https://trustedplugins.example.com; report-uri /csp-report-endpoint/");
}
}
问题是,angular应用程序不在spring上下文中。它是一个带有grunt的WAR构建,只包含js/css/html文件。REST-API还可以…您是如何解决的?我知道这是一篇老文章,但我遇到了类似的问题,所以我想检查一下。我们的tomcat前面有一个nginx或apache服务器(不记得了),它指向tomcat或静态文件。我们将cors配置添加到该服务器。还需要元标记。