cli*_*ell 3 h2 gradle spring-boot
Spring Boot指南说我可以使用H2控制台,但它对我不起作用.
http:// localhost:8080/h2 / Whitelabel错误页面此应用程序没有/ error的显式映射,因此您将此视为回退.Wed Oct 26 12:31:46 BST 2016出现意外错误(type = Not Found,status = 404).没有可用的消息
我创建了一个application.properties文件如下
spring.h2.console.enabled=true
spring.h2.console.path=/h2
Run Code Online (Sandbox Code Playgroud)
我的项目就是以此为基础的
默认路径/h2-console也不起作用.
我找到另一个答案,通过添加到Application.java:
@Bean
public ServletRegistrationBean h2servletRegistration() {
ServletRegistrationBean registration = new ServletRegistrationBean(new WebServlet());
registration.addUrlMappings("/h2/*");
return registration;
}
Run Code Online (Sandbox Code Playgroud)
我application.properties文件中的所有内容都会被忽略.我试过添加:
spring.datasource.url=jdbc:h2:file:~/portal;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driverClassName=org.h2.Driver
Run Code Online (Sandbox Code Playgroud)
但是数据库仍然只在内存中创建.
小智 9
导致此问题的另一个可能原因是您是否使用spring security。在这种情况下,您可能需要向您定义的 h2-console URL 添加特定权限。例如,对于默认的 h2-console 配置(没有属性),您将在WebSecurityConfigurerAdapterspring.h2.console.path扩展类中添加以下内容:
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/h2-console/**").permitAll()
.anyRequest().authenticated();
http.headers().frameOptions().sameOrigin();
}
Run Code Online (Sandbox Code Playgroud)
另请注意末尾的那一行 - http.headers().frameOptions().sameOrigin();。它需要防止登录控制台时出现 Whitelable 页面错误。此处也对此进行了描述。
检查是否在application.properties中设置了基本路径。
例如,如果您有一个设置
server.contextPath=/api
Run Code Online (Sandbox Code Playgroud)
您在下面访问h2控制台
http:// localhost:8080 / api / h2-console
很明显,但是那是给我的
a在它必须看起来像/之前丢失了:spring.h2.console.path
spring.h2.console.path=/h2
Run Code Online (Sandbox Code Playgroud)
spring.h2.console.path /h2-console当您指出不再可用时
问候
我在构建微服务项目时尝试了 h2-console,并遇到了同样的问题“Whitelabel 错误页面”。我目前尚未在项目中添加任何安全性,但我发现通过从 application.properties 中删除spring.h2.console.enabled= true并添加依赖项dev-tools可以解决我的问题。对我来说,不添加开发工具也会导致问题,因此也尝试添加此依赖项。当然,您将添加 h2、执行器、Web 依赖项。
| 归档时间: |
|
| 查看次数: |
9863 次 |
| 最近记录: |