H2-Console未在浏览器中显示

TAB*_*TAB 14 h2 spring-boot

我正在使用SpringBoot api并使用具有以下属性设置的H2数据库.

spring.h2.console.enabled=true
spring.datasource.name=test
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.datasource.initialization-mode = embedded
spring.datasource.url=jdbc:h2:mem:test
spring.jpa.hibernate.ddl-auto = update
Run Code Online (Sandbox Code Playgroud)

当我想使用浏览器通过' http:// localhost:8082/h2-console ' 查看H2数据库控制台时,在浏览器中打开一个带有连接和测试连接按钮的屏幕.当我单击"测试连接"时,它会返回成功,但是当单击"连接"按钮时,会出现localhost拒绝连接的错误.

这是该错误的屏幕

Ali*_*ien 29

根据 这篇博客文章,如果项目中有依赖项,则需要在该configure方法中添加一行,否则在登录H2控制台后会看到一个空页:SecurityConfigspring-boot-starter-security

http.headers().frameOptions().disable();
Run Code Online (Sandbox Code Playgroud)

我添加了该行,它解决了问题.

可替代地,下面的行,可以使用(如所提到的在这里):

http.headers().frameOptions().sameOrigin();
Run Code Online (Sandbox Code Playgroud)

  • 看到这个答案,我的第一个想法是在application.properties中添加一行,这是错误的。然后终于通过以下方式让 h2-console 工作:/sf/ask/2811614081/在 (2认同)

小智 20

默认情况下,Spring Security 会禁用iframe内的渲染,因为允许将网页添加到框架中可能会产生安全问题,例如Clickjacking。由于 H2 控制台在框架内运行,因此在启用 Spring 安全性时,必须显式禁用框架选项,以便使 H2 控制台正常工作。

http.headers().frameOptions().disable();
Run Code Online (Sandbox Code Playgroud)

一般来说,X-Frame-Options有两个可能的指令,即 DENY 或 SAMEORIGIN,因此以下配置也可用于受限但安全的访问。

headers().frameOptions().sameOrigin();
Run Code Online (Sandbox Code Playgroud)

这允许页面显示在与页面本身同源的框架中

  • 您的答案是最完整、最安全的。谢谢! (2认同)

Odw*_*ori 8

在您的 spring 安全文件中添加这两行,您就可以开始了。

    http.csrf().disable();
    http.headers().frameOptions().disable();
Run Code Online (Sandbox Code Playgroud)