# Spring 配置的 H2 控制台 frameOptions 导致无法访问

**URL:** <https://www.isharkfly.com/t/spring-h2-frameoptions/607>\
**Category:** 程序语言\
**Tags:** spring, java, h2\
**Created:** [October 22, 2020, 6:00pm UTC](https://www.isharkfly.com/t/spring-h2-frameoptions/607 "2020-10-22T18:00:01Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![honeymoose](https://www.isharkfly.com/user_avatar/www.isharkfly.com/honeymoose/32/20796_2.png) [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Post date:** [October 22, 2020, 6:00pm UTC](https://www.isharkfly.com/t/spring-h2-frameoptions/607/1 "2020-10-22T18:00:02Z")

</div>

Spring H2 控制台访问登录以后的内容无法显示。

访问的时候会看到下面的情况。

![spring-h2-console-01](https://www.isharkfly.com/images/transparent.png)

## 解决办法

这个主要是 Spring 安全设置的问题。

在 Spring 中的 frameOptions 配置为 iframe 的安全配置。

X-Frame-Options 头主要是为了防止站点被别人劫持，所以 iframe 将会在 Spring Security 中默认是拒绝设置的。以防止点击劫持攻击。

要修改这个配置，你可以在 Spring 安全配置中进行下面的配置：

```auto
httpSecurity.headers().frameOptions().disable();

```

上面的选项是完全允许 iframe 。

或者你也可以配置下面：

```auto
httpSecurity.headers().frameOptions().sameOrigin();

```

X-Frame-Options 有三个值：

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

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

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

![spring-h2-console-02](https://www.isharkfly.com/images/transparent.png)

因为我们的控制台配置的是 localhost， 因此我们可以使用 sameOrigin()，因此产生的效果是一样。
