Refused to display '' in a frame because it set 'X-Frame-Options' to 'sameorigin'
x-frame-options出现的问题夸子系统访问其他子系统的界面(用iframe嵌套,子系统间有一键登录),用iframe嵌套访问springboot项目的中的某个界面时报Refused to display ‘’ in a frame because it set ‘X-Frame-Options’ to ‘sameorigin’.。出现问题的原因这是因不支持iframe嵌入, 这个...
x-frame-options
出现的问题
夸子系统访问其他子系统的界面(用iframe嵌套,子系统间有一键登录),用iframe嵌套访问springboot项目的中的某个界面时报Refused to display ‘’ in a frame because it set ‘X-Frame-Options’ to ‘sameorigin’.。
出现问题的原因
这是因不支持iframe嵌入, 这个主要是因为spring boot默认为了安全, 默认不让网页支持嵌入, 帮助用户对抗点击劫持。
解决办法
X-Frame-Options 有三个值:
DENY
表示该页面不允许在 frame 中展示,即便是在相同域名的页面中嵌套也不允许。
SAMEORIGIN
表示该页面可以在相同域名页面的 frame 中展示。
ALLOW-FROM uri
表示该页面可以在指定来源的 frame 中展示。
spring boot支持EnableWebSecurity 这个anotation来设置不全的安全策略。 具体如下:
import com.alibaba.spring.websecurity.DefaultWebSecurityConfigurer;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.web.header.writers.frameoptions.WhiteListedAllowFromStrategy;
import org.springframework.security.web.header.writers.frameoptions.XFrameOptionsHeaderWriter;
import java.util.Arrays;
@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends DefaultWebSecurityConfigurer {
@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://itaobops.aliexpress.com", "https://cpp.alibaba-inc.com",
"https://pre-cpp.alibaba-inc.com"))));
}
}
上面是支持ALLOW-FROM uri的设置方式。
其他设置方式比较简单。 下面是支持SAMEORIGIN的设置方式:
@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends DefaultWebSecurityConfigurer {
@Override
protected void configure(HttpSecurity http) throws Exception {
super.configure(http);
http.headers().frameOptions().sameOrigin();
}
}
下面是支持完全放开的方式:
@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends DefaultWebSecurityConfigurer {
@Override
protected void configure(HttpSecurity http) throws Exception {
super.configure(http);
http.headers().frameOptions().disable();
}
}
如果是在相同域名下的,http.headers().frameOptions().sameOrigin();这个设置可以解决。但是我出现的问题是跨子系统间的访问,不在相同域名下,所以我直接设置支持完全开发才解决问题。(暂时先这样用了)
更多推荐
所有评论(0)