Java 扩展WebSecurity配置适配器未添加CSP标头
在我的Spring MVC(5.0.3)应用程序中,我添加了一个类Java 扩展WebSecurity配置适配器未添加CSP标头,java,spring-mvc,spring-security,Java,Spring Mvc,Spring Security,在我的Spring MVC(5.0.3)应用程序中,我添加了一个类SecurityConfig,它扩展了WebSecurityConfigureAdapter,如下所示: @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http)
SecurityConfig
,它扩展了WebSecurityConfigureAdapter
,如下所示:
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter
{
@Override
protected void configure(HttpSecurity http) throws Exception
{
http.headers().contentSecurityPolicy("script-src 'self'");
}
}
我的期望是浏览之后http://localhost:2020/webapp/ 我将在浏览器的开发者控制台(网络选项卡)中看到CSP标题。但是,响应头中没有添加任何与CSP相关的内容
我已经通过在第http.headers().contentSecurityPolicy(“scriptsrc'self')行应用断点进行了检查代码>并且它在上下文加载期间命中一次
为什么CSP头没有被添加到响应中有什么问题
更新:我甚至尝试了http.headers()
,据文档称,此调用不带其他方法,将添加默认的安全头。但在我的情况下,没有添加任何默认安全标头。
根据要求,我上传了最小的、可复制的示例@Google Drive,要下载,必须提供源URL,以便用户代理阻止从脚本src指令中声明的源以外的其他源加载脚本的尝试
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter
{
@Override
protected void configure(HttpSecurity http) throws Exception
{
http.headers().contentSecurityPolicy("script-src 'self' https://trustedscripts.example.com");
}
}
根据,必须提供源URL,以便用户代理将阻止尝试从脚本src指令中声明的源以外的其他源加载脚本
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter
{
@Override
protected void configure(HttpSecurity http) throws Exception
{
http.headers().contentSecurityPolicy("script-src 'self' https://trustedscripts.example.com");
}
}
查看mvce,您似乎没有注册spring安全过滤器链。将下面的内容添加到web.xml中,应该都能正常工作
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
springSecurityFilterChain
org.springframework.web.filter.DelegatingFilterProxy
springSecurityFilterChain
/*
查看了mvce,发现您没有注册spring安全过滤器链。将下面的内容添加到web.xml中,应该都能正常工作
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
springSecurityFilterChain
org.springframework.web.filter.DelegatingFilterProxy
springSecurityFilterChain
/*
您需要使用以下格式将contentSecurityPolicy添加到api调用中。您可以在这里进一步了解spring版本(5.0.3)
Spring(5.5.X)Security默认情况下不添加内容安全策略,因为没有应用程序的上下文,不可能知道合理的默认值。web应用程序作者必须声明要强制执行和/或监视受保护资源的安全策略。参考14.2.8。这里
您需要使用以下格式将contentSecurityPolicy添加到api调用中。您可以在这里进一步了解spring版本(5.0.3)
Spring(5.5.X)Security默认情况下不添加内容安全策略,因为没有应用程序的上下文,不可能知道合理的默认值。web应用程序作者必须声明要强制执行和/或监视受保护资源的安全策略。参考14.2.8。这里
对不起,我无法复制您的问题。您可以提供一个例子吗?@s7vr请检查有关MVCE的更新。您好,您是否在前端使用Vue.js或Angular这样的单页应用程序框架,并且在前端运行他们提供的服务器进行开发,我刚刚在我的一个项目中尝试了Vue.js,如果a通过代理服务器执行请求,我不会得到内容安全策略,但是如果我直接调用spring boot启动的tomcat服务器,配置就在那里,即使我尝试与postman一起在后端直接调用端点,我也会得到内容安全策略抱歉,我无法重现您的问题。您可以提供一个例子吗?@s7vr请检查有关MVCE的更新。您好,您是否在前端使用Vue.js或Angular这样的单页应用程序框架,并且在前端运行他们提供的服务器进行开发,我刚刚在我的一个项目中尝试了Vue.js,如果a通过代理服务器执行请求,我不会得到内容安全策略,但是如果我直接调用spring boot启动的tomcat服务器,配置就在那里,即使我尝试使用postman在后端直接调用端点,我也会得到内容安全策略,但我不想为脚本添加任何其他源,除了“self”,那么为什么我应该指定url。而且我在文档中没有发现任何内容说“你必须提到可信的url,只指定“self”将不起作用”当然不起作用,文档将向你展示一个示例应该做什么,而不是不应该做什么。在这种情况下,即使是你的“self”“,他们正在指定源代码,我只是指出这一点。我还使用http.headers()
进行了检查,根据文档,此调用仅headers()
,没有其他方法,它将添加默认的安全头。但是在我的例子中,没有一个默认的安全头被添加。检查mvce的问题。正如您所说,我已经检查了更多供参考的内容,我添加了这一行http.headers().contentSecurityPolicy(“脚本src'self'https://trustedscripts.example.com");代码>但是没有成功。但是我不想为脚本添加除“self”之外的任何其他源代码,那么为什么我应该指定url。而且我在文档中也没有发现任何内容说“你必须提到可信的URL,只指定“self”将不起作用”当然不会,文档将向你展示你应该用一个例子做什么,而不是你不应该做什么。在这种情况下,即使使用您的“self”,他们也在指定源代码,我只是指出这一点。我还使用http.headers()
进行了检查,根据文档,此调用仅headers()
,没有其他方法,它将添加默认的安全头。但是在我的例子中,没有一个默认的安全头被添加。检查mvce的问题。正如您所说,我已经检查了更多供参考的内容,我添加了这一行http.headers().contentSecurityPolicy(“脚本src'self'https://trustedscripts.example.com");代码>但没有成功。在mvce中,在将其添加到web.xml中后,一切都正常工作,但在实际项目中,添加此I ge之后