假设一个spring安全和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安全配置的配置函数下做这件事,但是我不知道具体是怎么做的,即我假设是.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();
}发布于 2014-06-08 08:53:52
只需像这样使用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'"))
// ...
}
}请注意,一旦您指定了应该包含的任何标头,那么将只包含这些标头。
要包含默认标头,您可以执行以下操作:
http
.headers()
.contentTypeOptions()
.xssProtection()
.cacheControl()
.httpStrictTransportSecurity()
.frameOptions()
.addHeaderWriter(new StaticHeadersWriter("X-Content-Security-Policy","script-src 'self'"))
// ...您可以参考spring security documentation。
发布于 2017-09-22 01:00:28
虽然使用StaticHeadersWriter的方法可以工作,但在Spring Security的最新版本中,可以使用特殊的方法:
headers()
.contentSecurityPolicy("script-src 'self'");发布于 2018-05-23 15:30:16
如Spring安全文档中所述:https://docs.spring.io/spring-security/site/docs/current/reference/html/headers.html
@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/")
.reportOnly();
}
}https://stackoverflow.com/questions/24057040
复制相似问题