【问题标题】:H2 database console spring boot Load denied by X-Frame-OptionsH2 数据库控制台 Spring Boot 加载被 X-Frame-Options 拒绝
【发布时间】:2014-11-30 22:33:51
【问题描述】:

我正在为开发人员构建一个具有 Spring 4 启动安全性和其他功能的骨架项目。 在尝试登录数据库控制台并管理我的数据库时使用 H2 我收到以下错误。页面是空白的,firebug konsole 中有 4 个错误:

 Load denied by X-Frame-Options: http://localhost:8080/console

有链接

/header.jsp?jsessionid=f71207a702c9177e57208414721bbe93 does not permit framing.
/query.jsp?jsessionid=f71207a702c9177e57208414721bbe93 does not permit framing.
/help.jsp?jsessionid=f71207a702c9177e57208414721bbe93 does not permit framing.
/tables.do?jsessionid=f71207a702c9177e57208414721bbe93 does not permit framing.
  1. 我可以从控制台级别测试连接 - 没问题。
  2. DB 工作正常,import.sql 工作正常,我可以在 spring 启动时创建用户实体。

我正在使用的配置来自(它适用于带有 xml 配置的 spring 3.2)

spring boot default H2 jdbc connection (and H2 console)

使用: spring-boot-starter-parent 1.1.4.发布

【问题讨论】:

  • 添加 .and().headers() .addHeaderWriter(new XFrameOptionsHeaderWriter( new WhiteListedAllowFromStrategy(Arrays.asList("localhost:8080","http://localhost")))) 白页和信息刷新到页面以获取源代码。

标签: h2 spring-boot spring-4


【解决方案1】:

也可以通过以下方式简化@chrosciu 的答案:

@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http.headers().frameOptions().disable();
  }
}

【讨论】:

  • 或者,更安全的是headers().frameOptions().sameOrigin()
【解决方案2】:

这对我有用:

@EnableWebSecurity
@Configuration
class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.headers().addHeaderWriter(
            new XFrameOptionsHeaderWriter(
                new WhiteListedAllowFromStrategy(Arrays.asList("localhost"))));
    }
}

当然,白名单的内容应该调整,以防应用程序在不同于本地主机的地方运行。

【讨论】:

  • 更好的解决方案:http.headers().addHeaderWriter(new XFrameOptionsHeaderWriter(XFrameOptionsMode.SAMEORIGIN)); 这不需要在安全配置中明确状态主机名
  • 更好:.headers().frameOptions().sameOrigin()
【解决方案3】:

将下面的代码添加到 Application.java 中,现在它可以工作了,默认在端口 8082 上,从 spring 应用程序开始。它没有达到目的,但出于开发目的,一切正常。

@Bean
org.h2.tools.Server h2Server() {
    Server server = new Server();
    try {
        server.runTool("-tcp");
        server.runTool("-tcpAllowOthers");
    } catch (Exception e) {
        e.printStackTrace();
    }
    return server;

}

【讨论】:

  • 很好,但是在启动时会在浏览器中自动启动 h2,有没有办法避免它?
  • 我花了 3 个小时试图通过 Spring Security 使其工作,但不能。这成功了!
猜你喜欢
  • 1970-01-01
  • 2013-02-01
  • 1970-01-01
  • 2014-09-06
  • 2015-11-09
  • 2015-06-04
  • 2023-02-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多