1.问题解释说明:

X-Frame-Options HTTP 响应头是用来给浏览器 指示允许一个页面 可否在 <frame>, <iframe>, <embed> 或者 <object> 中展现的标记。站点可以通过确保网站没有被嵌入到别人的站点里面,从而避免被攻击。

X-Frame-Options有三个可能的值:

X-Frame-Options: deny

X-Frame-Options: sameorigin

X-Frame-Options: allow-from url

换一句话说,如果设置为deny,不光在别人的网站 frame 嵌入时会无法加载,在同域名页面中同样会无法加载。另一方面,如果设置为sameorigin,那么页面就可以在同域名页面的 frame 中嵌套。

deny

表示该页面不允许在 frame 中展示,即便是在相同域名的页面中嵌套也不允许。

sameorigin

表示该页面可以在相同域名页面的 frame 中展示。

allow-from uri

表示该页面可以在指定来源的 frame 中展示。

2.开发中遇到springBoot springSecurty导致x-frame-option出现deny问题的,项目如下类定义方法即可解决:

package cn.xxx.config.common;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.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer.AuthorizedUrl;@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {public WebSecurityConfig() {}protected void configure(HttpSecurity http) throws Exception {((HttpSecurity)((AuthorizedUrl)((HttpSecurity)http.csrf().disable()).authorizeRequests().anyRequest()).permitAll().and()).logout().permitAll();}
}
package cn.xxx.config.security;import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;import cn.xxx.config.common.WebSecurityConfig;@Configuration
@Order(1)
public class SecurityConfig extends WebSecurityConfig {@Overrideprotected void configure(HttpSecurity http) throws Exception {//http.headers().frameOptions().disable(); // 任何跨域http.headers().frameOptions().sameOrigin(); // 同源跨域http.csrf().disable().authorizeRequests().anyRequest().permitAll().and().logout().permitAll();http.cors().configurationSource(corsConfigurationSource());}//配置跨域访问资源private CorsConfigurationSource corsConfigurationSource() {CorsConfigurationSource source = new UrlBasedCorsConfigurationSource();CorsConfiguration corsConfiguration = new CorsConfiguration();corsConfiguration.addAllowedOrigin("*"); // 同源配置,*表示任何请求都视为同源,若需指定ip和端口可以改为如“localhost:8080”,多个以“,”分隔;corsConfiguration.addAllowedHeader("*");// header,允许哪些header,本案中使用的是token,此处可将*替换为token;corsConfiguration.addAllowedMethod("*"); // 允许的请求方法,PSOT、GET等((UrlBasedCorsConfigurationSource) source).registerCorsConfiguration("/**", corsConfiguration); // 配置允许跨域访问的urlreturn source;}
}

3.若出现因为前端请求的不同ip和端口跨域问题导致的不能访问,项目如下类定义方法可解决:

package cn.xxx.config.cors;import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;@Configuration
public class CorsConfig extends WebMvcConfigurerAdapter {@Overridepublic void addCorsMappings(CorsRegistry registry) {// TODO Auto-generated method stub// super.addCorsMappings(registry);registry.addMapping("/**").allowedOrigins("*").allowCredentials(true).allowedMethods("GET", "POST", "DELETE", "PUT").maxAge(3600);}@Overridepublic void configureMessageConverters(List<HttpMessageConverter<?>> converters) {MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();//设置日期格式ObjectMapper objectMapper = new ObjectMapper();SimpleDateFormat smt = new SimpleDateFormat("yyyy-MM-dd");objectMapper.setDateFormat(smt);mappingJackson2HttpMessageConverter.setObjectMapper(objectMapper);//设置中文编码格式List<MediaType> list = new ArrayList<MediaType>();list.add(MediaType.APPLICATION_JSON_UTF8);mappingJackson2HttpMessageConverter.setSupportedMediaTypes(list);converters.add(mappingJackson2HttpMessageConverter);super.configureMessageConverters(converters);}}

springBoot springSecurty导致的x-frame-options值为deny问题及跨域问题处理方法相关推荐

  1. 我在学习springboot和vue前后台连接时碰到的问题记录!(跨域问题)

    解决跨域是遇到的问题及记录: 我在另一个电脑上这样写后台解决跨域问题完全没问题,但是换台电脑就出现这种问题了,后来前辈告诉我,用.allowedOriginPatterns("") ...

  2. 【docker】elasticsearch-head无法连接elasticsearch的原因和解决,集群健康值:未连接,ElasticSearch——跨域访问的问题...

    环境 ==================== 虚拟机启动 centos 7  ip:192.168.92.130 elasticsearch 5.6.9   port:9200  9201 elas ...

  3. SpringBoot—CORS跨域问题详解和解决方案

    关注微信公众号:CodingTechWork,一起学习进步. 引言   在前后端开发过程中,遇到过一种错误,类似于报错: Access to XMLHttpRequest at 'http://127 ...

  4. 记一次SpringBoot解决CROS跨域问题(CROS)

    记一次SpringBoot解决CROS跨域问题(CROS) 使用注解@CrossOrigin(局部跨域 后端创建注入切面 @Target({ElementType.TYPE, ElementType. ...

  5. springboot+Vue项目使用axios实现跨域(CROS)

    springboot+Vue项目使用axios实现(CROS)跨域 一.项目背景 二.跨域资源共享 CORS 详解 三.axios跨域实现 3.1 安装qs模块 3.2 axios使用 四.sprin ...

  6. springboot 跨域配置cors

    撸了今年阿里.头条和美团的面试,我有一个重要发现.......>>> 1 跨域的理解 跨域是指:浏览器A从服务器B获取的静态资源,包括Html.Css.Js,然后在Js中通过Ajax ...

  7. 页面跨域与iframe通信(Blocked a frame with origin)

    项目中有个需求是在前后端分离的情况下,前台页面将后台页面加载在预留的iframe中:但是遇到了iframe和主窗口双滚动条的情况,由此引申出来了问题: 只保留单个滚动条,那么就要让iframe的高度自 ...

  8. Iframe和Frame中实现cookie跨域的方法(转载)

    在Iframe和Frame中默认是不支持Cookie跨域的,但通过设置P3P协议相关的响应头可以解决这一问题. 关于p3p协议: P3P: Platform for Privacy Preferenc ...

  9. SpringBoot解决跨域(CROS)问题

    SpringBoot解决跨域问题(CROS) 问题: 前端请求后端出现下图类似问题: Access to fetch at 'http://localhost:8081/user/page?pageN ...

最新文章

  1. 使用JNA,让java调用原生代码
  2. C#基础解析之Ⅱ【运算符和条件结构】
  3. [深度学习] 自然语言处理 --- 1.Attention
  4. ASP.NET Core Filter与IOC的羁绊
  5. 160 - 18 Brad Soblesky.1
  6. listary什么意思_listary使用心得
  7. java 课程设计数据库_人事管理系统(java数据库课程设计)+SQL数据库
  8. 如何在博客中插入数学公式
  9. nginx热升级实现
  10. mysql主从简明配置
  11. 使用开源软件 Mantis 实施缺陷跟踪的成功实践
  12. Nginx常见问题(优化)
  13. Python代码混淆工具,Python源代码保密、加密、混淆
  14. 系统集成项目管理之项目合同管理
  15. 数字html显示e的格式化,表格中数字显示是E+数字 怎么正常显示
  16. HTML5 + Canvas + 广度优先搜索(BFS) 编写lol连连看
  17. 元宵大师的Python股票量化分析工具QTYX-版本更新说明
  18. CF868F 分治优化dp
  19. BlockingQueue 使用
  20. Mybatis传递单个参数

热门文章

  1. 【Python数据分析—NumPy】7.NumPy专用函数
  2. 蓝桥ROS机器人之极简贪吃蛇
  3. windows/linux下svn修改URL地址
  4. Fdog系列(四):使用Qt框架模仿QQ实现登录界面,界面篇。
  5. 股市和期市是两种截然不同的市场机制 ——兼与徐小明先生商榷
  6. 网络是怎样连接的学习笔记(四)
  7. 理解梯度下降算法中的动量
  8. WPF UI开发教程LinkButton链接按钮
  9. 版权登记版权登记有何作用?
  10. linux虚拟网卡ping不通网关,解决虚拟机或物理机ping不通网关故障的方法与思路...