java 为什么 Spring Boot 中的 H2 控制台登录后显示黑屏?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40165915/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 04:58:48  来源:igfitidea点击:

Why does the H2 console in Spring Boot show a blank screen after logging in?

javaspring-booth2

提问by pacoverflow

I'm using Spring Boot 1.4.1 with the H2 database. I have enabled the H2 console as described in the reference guideby adding the following lines to my application.properties file:

我在 H2 数据库中使用 Spring Boot 1.4.1。我按照参考指南中的描述启用了 H2 控制台,方法是将以下几行添加到我的 application.properties 文件中:

spring.h2.console.enabled=true
spring.h2.console.path=/h2

When I go to the H2 console in Chrome 53 for Windows, I can see the login page and clicking the "Test Connection" button results in "Test successful":

当我转到 Windows 版 Chrome 53 中的 H2 控制台时,我可以看到登录页面并单击“测试连接”按钮会导致“测试成功”:

enter image description here

在此处输入图片说明

But when I click on the "Connect" button, the screen turns completely blank. When I view the source, I see "Sorry, Lynx not supported yet" (see the full source). The same thing happens in Firefox.

但是当我点击“连接”按钮时,屏幕变成完全空白。当我查看源代码时,我看到“抱歉,尚不支持 Lynx”(请参阅完整的源代码)。同样的事情发生在 Firefox 中。

Why is that happening? I believe I am using the correct JDBC URL, as 4 different people posted on this questionthat you should use jdbc:h2:mem:testdb.

为什么会这样?我相信我使用的是正确的 JDBC URL,因为 4 个不同的人在这个问题上发布了你应该使用的jdbc:h2:mem:testdb.

回答by pacoverflow

According to a blog post, a line needs to be added to the configuremethod of the SecurityConfigclass if you have the spring-boot-starter-securitydependency in your project, otherwise you will see an empty page after logging into the H2 console:

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

http.headers().frameOptions().disable();

I added that line and it solved the problem.

我添加了该行并解决了问题。

Alternatively, the following line can be used (as mentioned here):

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

http.headers().frameOptions().sameOrigin();

回答by Martin Larizzate

I can resolve the same problem using the following code in my SecurityConfig class

我可以在我的 SecurityConfig 类中使用以下代码解决同样的问题

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

I don't know what this line does, maybe someone with more experience can explain it.

我不知道这条线是做什么的,也许有经验的人可以解释一下。

回答by Antariksh

Add this to your application.properties

将此添加到您的 application.properties

 security.headers.frame=false