Spring Boot 不允许加载iframe问题解决

时间:2022-09-29 15:39:45


 spring Security下,X-Frame-Options默认为DENY,非spring Security环境下,X-Frame-Options的默认大多也是DENY,这种情况下,浏览器拒绝当前页面加载任何Frame页面,设置含义如下:

    DENY:浏览器拒绝当前页面加载任何Frame页面
    SAMEORIGIN:frame页面的地址只能为同源域名下的页面
    ALLOW-FROM:origin为允许frame加载的页面地址。


在spring boot项目中出现不能加载iframe

页面报一个"Refused to display 'http://......' in a frame because it set 'X-Frame-Options' to 'DENY'. "错误

解决方式:

因spring Boot采取的java config,在配置spring security的位置添加:

@Override
protected void
configure(HttpSecurity http) throws Exception {
http.headers().frameOptions().disable();
http
.csrf().disable();
http
.authorizeRequests()
.anyRequest().authenticated();

http.formLogin()
.defaultSuccessUrl("/platform/index",true)
.loginPage("/login")
.permitAll()
.and()
.logout()
.logoutUrl("/logout");

http.addFilterBefore(wiselyFilterSecurityInterceptor(),FilterSecurityInterceptor.class);


}