Spring boot:无法登录h2控制台

kas*_*ase 1 h2 spring-boot

我是 h2 DB 的新手,我搜索过这个问题,但没有找到解决方案。我想尝试用spring boot搭建一个tcp服务器模式,让别人用spring boot或者python连接。这是我的属性如下:

db2.datasource.driverClassName=org.h2.Driver
db2.datasource.url=jdbc:h2:mem:testdb
db2.datasource.username=sa
db2.datasource.password=
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
spring.jpa.hibernate.ddl-auto=update
Run Code Online (Sandbox Code Playgroud)

我可以连接到 localhost:8080/h2-console,但无法登录,它返回

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Wed Sep 09 15:08:13 CST 2020
There was an unexpected error (type=Forbidden, status=403).
Forbidden
Run Code Online (Sandbox Code Playgroud)

我正在使用 spring security,但我的方法配置在开发模式下为空,我不知道应该修复哪里...

我的 pom.xml:

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <version>1.4.200</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

和 TCP 服务器配置:

 @Bean(initMethod = "start", destroyMethod = "stop")
    public Server inMemoryH2DatabaseaServer() throws SQLException {
        return Server.createTcpServer(
                "-tcp", "-tcpAllowOthers", "-tcpPort", "9092");
    }

    @PostConstruct
    private void initDb() {
        JdbcTemplate jdbcTemplate=new JdbcTemplate(dataSource);
        String sqlStatements[] = {
                "drop table employees if exists",
                "create table employees(id serial,first_name varchar(255),last_name varchar(255))",
                "insert into employees(first_name, last_name) values('Eugen','Paraschiv')",
                "insert into employees(first_name, last_name) values('Scott','Tiger')"
        };

        Arrays.asList(sqlStatements).forEach(sql -> {
            jdbcTemplate.execute(sql);
        });

    }
Run Code Online (Sandbox Code Playgroud)

有人知道如何解决这个问题吗?

谢谢

kas*_*ase 6

好吧,我发现问题了。这是因为我使用的是 spring security,并且自动启用 CSRF 保护,所以我扩展WebSecurityConfigurerAdapter并覆盖configure

@Override
protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable();
        http.headers().frameOptions().disable();
    }
Run Code Online (Sandbox Code Playgroud)

并且它运行成功。