【问题标题】:H2-In memory database console not openingH2-In memory 数据库控制台未打开
【发布时间】:2018-04-23 14:41:16
【问题描述】:

我在 Spring Boot 应用程序中使用 H2 数据库。但无法在http://localhost:8080/console 的浏览器中打开它。我的 pom.xml 如下:

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <version>1.4.192</version>
</dependency>

春季开机配置:

Springboot配置文件

@Configuration
public class WebConfiguration {
    @Bean
    ServletRegistrationBean h2servletRegistration(){
        ServletRegistrationBean registrationBean = new ServletRegistrationBean( new WebServlet());
        registrationBean.addUrlMappings("/console/*");
        return registrationBean;
    }
}

【问题讨论】:

  • 除非你的 pom 是空的,否则我想你忘了附上它 ;)
  • @PierreB.:附上我正在使用的依赖项。

标签: java spring maven spring-boot h2


【解决方案1】:

要使用 H2 控制台,您需要在 .properties 文件中对其进行配置

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

其中/h2console/ 是您要在浏览器上使用的路径,因此您可以将其更改为任何内容。此外,如果您启用了安全性,您可能希望将其添加到允许的路径中

也将此添加到您的HttpSecurity 配置http.headers().frameOptions().disable();

编辑

更改您的安全配置我很确定您的 pom 中可能有 spring security 所以请改用它,如果没有,它应该可以工作

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
public class WebConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity.authorizeRequests().antMatchers("/").permitAll().and()
                .authorizeRequests().antMatchers("/console/**").permitAll();

        httpSecurity.csrf().disable();
        httpSecurity.headers().frameOptions().disable();
    }

}

【讨论】:

  • 我在一个配置文件里配置了
  • 什么配置文件?
  • Springboot 配置文件。
  • 你添加了这个http.headers().frameOptions().disable()
  • 不。我没有。附上我的配置快照。
【解决方案2】:

如果在您的 pom 中包含 spring-boot-starter-security 工件,则默认情况下启用基本身份验证。因此,要访问您的控制台,您可以通过在 application.properties 中添加 security.basic.enabled=false 来禁用基本身份验证,或者在您的配置方法中允许访问,如下所示:

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity.csrf().disable().authorizeRequests().antMatchers("/").permitAll().and().authorizeRequests()
                .antMatchers("/console/**").permitAll();
        httpSecurity.headers().frameOptions().disable();
    }
}

【讨论】:

    【解决方案3】:

    感谢大家的慷慨帮助。应用程序类(Springboot)在一个单独的包中,它没有扫描其他包。另外我修改了我的 Pom.xml 最终帮助我访问控制台。附件是我的新 Pom.xml。

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.spring.app</groupId>
        <artifactId>Demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <name>SpringBootApp</name>
        <description>Generator of statistics </description>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.2.4.RELEASE</version>
            <relativePath /> <!-- lookup parent from repository -->
        </parent>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-security</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
    
    
            <!--WebJars -->
            <dependency>
                <groupId>org.webjars</groupId>
                <artifactId>bootstrap</artifactId>
                <version>3.3.4</version>
            </dependency>
            <dependency>
                <groupId>org.webjars</groupId>
                <artifactId>jquery</artifactId>
                <version>2.1.4</version>
            </dependency>
    
            <dependency>
                <groupId>com.h2database</groupId>
                <artifactId>h2</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
    
    
            <!-- Spring AOP + AspectJ -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aop</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjrt</artifactId>
    
            </dependency>
    
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjweaver</artifactId>
    
            </dependency>
    
    
            <!-- JavaConfig need this library -->
            <dependency>
                <groupId>cglib</groupId>
                <artifactId>cglib</artifactId>
                <version>2.2.2</version>
            </dependency>
    
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-validator</artifactId>
    
            </dependency>
    
    
            <!-- Jackson JSON Mapper -->
            <dependency>
                <groupId>org.codehaus.jackson</groupId>
                <artifactId>jackson-mapper-asl</artifactId>
                <version>1.7.1</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
    
            </dependency>
        </dependencies>
        <build>
    
    
            <plugins>
    
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    
    
    
    </project>
    

    【讨论】:

      【解决方案4】:

      由于 h2 数据库控制台映射到“h2-console”。

      使用这个:

      http.csrf().disable().authorizeRequests()
              .antMatchers("/h2-console/**").permitAll()
              .anyRequest().authenticated();
      
      // disable frame options
      http.headers().frameOptions().disable();
      

      ` 您不需要允许 root 访问: .antMatchers("/") * 不需要 *

      【讨论】:

        【解决方案5】:

        转到 POM 文件并添加依赖项:

               <dependency>
                <groupId>com.h2database</groupId>
                <artifactId>h2</artifactId>
            </dependency>
        

        重建你的项目

        【讨论】:

          【解决方案6】:

          这可能对大家有帮助,上面的所有配置都是正确的

          注意 - 如果您没有使用任何安全性,则不需要添加弹簧安全性

          实际上的问题是 - 当你在 chrome 中打开这个 url 时

          http://localhost:8080/h2 chrome 做到了 --> https://localhost:8080/h2

          要摆脱这个问题 - 下面的参考资料会有所帮助 -

          Google Chrome redirecting localhost to https

          【讨论】:

            【解决方案7】:

            您可能会遇到两种情况,包括以下错误:

            1. localhost 拒绝连接

            • 仔细检查网址。 Chrome 会自动尝试将 http:// 更改为 https://

            • 检查 spring.h2.console.path(控制台可用的路径)以获取您的 URL:

              默认:/h2-console --> 网址:http://localhost:8080/h2-console/

            • 如果您运行 IDE(例如 IntelliJ Idea),确保您的应用正在运行这意味着您的 H2 数据库正在运行!


            • 您面临 404 错误: 在这种情况下,您的 H2 数据库在端口 8080 上正确运行,并且您已经与它建立了连接。

            • 检查 spring.h2.console.path(控制台可用的路径)以获取您的 URL:

              默认:/h2-console --> 网址:http://localhost:8080/h2-console/

            • 启用 H2 控制台

            spring.h2.console.enabled=true
            

            【讨论】:

              【解决方案8】:

              该问题也可能是由于将server.servlet.context-path 添加到属性而引起的。新的 url 将由server.servlet.context-path 加上spring.h2.console.path 组成

              【讨论】:

                猜你喜欢
                • 2022-12-05
                • 2019-01-15
                • 1970-01-01
                • 1970-01-01
                • 2016-06-19
                • 2023-02-08
                • 2017-12-05
                • 1970-01-01
                • 2020-04-12
                相关资源
                最近更新 更多