在用spring boot项目时,整合了springSecurity框架,结果iframe中界面无法显示出来,按F12查看问题,结果显示访问的界面有in a frame because it set 'X-Frame-Options' to 'deny'提示,
解决办法如下:
在我的SecurityConfig类的configure方法中,增加头设置http.headers().frameOptions().sameOrigin();,如下所示:
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
/**
* 根路径 “/” 允许全部访问请求
* 路径 “/users/**” 只允许角色是 USER 的访问, hasRole() 方法也可以用多参数方法 hasAnyRole()
* 登录路径设置为 /login
* 登录失败跳转到 /login-error
* 注销路径 /login
*/
http.authorizeRequests()
.antMatchers("/").permitAll()
//.antMatchers("/users/**").hasRole("USER")
.and()
.formLogin().loginPage("/login.jsp").failureUrl("/login.jsp")
.and()
.logout().logoutUrl("/项目名/User/logout");
// 关闭CSRF跨域
http.csrf().disable();
//浏览器 in a frame because it set 'X-Frame-Options' to 'deny'
/*原因:
项目里面使用了springSecurity
spring Security下,X-Frame-Options默认为DENY,非spring Security环境下,X-Frame-Options的默认大多也是DENY,这种情况下,浏览器拒绝当前页面加载任何Frame页面,设置含义如下:
• DENY:浏览器拒绝当前页面加载任何Frame页面
• SAMEORIGIN:frame页面的地址只能为同源域名下的页面
• ALLOW-FROM:origin为允许frame加载的页面地址。*/
//解决X-Frame-Options问题
http.headers().frameOptions().sameOrigin();
}
}