假设 spring security 和 spring mvc 的工作 hello world 示例。
当我使用wireshark进行跟踪时,我在http请求中看到以下标志
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
Strict-Transport-Security: max-age=31536000 ; includeSubDomains
X-Frame-Options: DENY
Set-Cookie: JSESSIONID=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX; Path=/; Secure; HttpOnly
我想将此添加到我的标题中:
Content-Security-Policy: script-src 'self'
我知道 X-Frame-Options 做了几乎相同的工作,但它仍然让我睡得更好。
现在我想我需要在我的 spring 安全配置的配置功能下执行它但是我不知 Prop 体如何,即我想
.headers().something.something(self)
@Override
protected void configure(HttpSecurity http) throws Exception {
http
// .csrf().disable()
// .headers().disable()
.authorizeRequests()
.antMatchers( "/register",
"/static/**",
"/h2/**",
"/resources/**",
"/resources/static/css/**",
"/resources/static/img/**" ,
"/resources/static/js/**",
"/resources/static/pdf/**"
).permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
}
最佳答案
只需像这样使用 addHeaderWriter 方法:
@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends
WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
// ...
.headers()
.addHeaderWriter(new StaticHeadersWriter("X-Content-Security-Policy","script-src 'self'"))
// ...
}
}
请注意,一旦您指定了应包含的任何 header ,则只会包含这些 header 。
要包含默认标题,您可以执行以下操作:
http
.headers()
.contentTypeOptions()
.xssProtection()
.cacheControl()
.httpStrictTransportSecurity()
.frameOptions()
.addHeaderWriter(new StaticHeadersWriter("X-Content-Security-Policy","script-src 'self'"))
// ...
您可以引用spring security documentation .
关于spring - 内容安全策略 Spring Security,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24057040/