Spring Security环境下X-Frame-Options默认为DENY
非Spring Security环境下X-Frame-Options的默认大多也是DENY
这种情况下,浏览器拒绝当前页面加载任何Frame页面
X-Frame-Options 有三个值:
DENY 表示该页面不允许在 frame 中展示,即便是在相同域名的页面中嵌套也不允许。
SAMEORIGIN 表示该页面可以在相同域名页面的 frame 中展示。
ALLOW-FROM uri 表示该页面可以在指定来源的 frame 中展示。
支持ALLOW-FROM uri的设置方式:
public class SecurityConfig extends WebSecurityConfigurerAdapter {
// 省略部分代码
@Override
protected void configure(HttpSecurity http) throws Exception {
super.configure(http);
//disable 默认策略。 这一句不能省。
http.headers().frameOptions().disable();
//新增新的策略。
http.headers().addHeaderWriter(new XFrameOptionsHeaderWriter(
new WhiteListedAllowFromStrategy(
Arrays.asList("http://www.baidu.com", "https://www.baidu.com",
"https://www.sougou.com"))));
}
// 省略部分代码
}
支持SAMEORIGIN的设置方式:
public class SecurityConfig extends WebSecurityConfigurerAdapter {
// 省略部分代码
@Override
protected void configure(HttpSecurity http) throws Exception {
super.configure(http);
http.headers().frameOptions().sameOrigin();
}
// 省略部分代码
}
支持完全放开的方式:
public class SecurityConfig extends WebSecurityConfigurerAdapter {
// 省略部分代码
@Override
protected void configure(HttpSecurity http) throws Exception {
super.configure(http);
http.headers().frameOptions().disable();
}
// 省略部分代码
}