java - 尝试测试与 h2 数据库的连接或连接 whitelabel 错误后
问题描述
运行 Spring boot 项目并转到 localhost:8080/h2-console 并尝试连接到我的数据库后,当我单击 h2-console 屏幕上的任何按钮时,我得到 whitelabel 错误。我在内存数据库中使用。我正在尝试使用经典的用户名:sa 和密码:(空白)连接到数据库,
即使单击保存后,我也会得到 whitelabel 错误
应用程序属性:
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.enabled=true
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.m2-2.0</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>test</name>
<description>test project for mc2</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
配置方法 WebSecurityConfig 类:
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/", "/home", "/h2-console/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
http.headers().frameOptions().sameOrigin();
}
解决方案
经过研究,我发现spring security csrf阻止了h2-console,所以需要继续将下一个代码添加到WebSecurityConfig类
http
.headers().frameOptions().sameOrigin();
http
.csrf().disable();
http
.headers().frameOptions().disable();
推荐阅读
- apache-spark - 在 Dataproc 中向 Spark 作业提交作业参数
- prometheus-alertmanager - 如何在警报管理器路由匹配中写入非条件?
- nearprotocol - NEAR 协议网络的发布周期是多少?
- postgresql - Postgres RDS pg_recvlogical 复制角色
- r - 如何制作支持带引号和不带引号的参数的 tidyverse 函数?
- json - 如何使用空键并使用默认值快速读取 json?
- javascript - 带有嵌套函数和循环的 ES6 闭包的奇怪输出,为什么输出与 Var 不同而与 Let 不同?
- c# - 在 Html.BeginForm 中使用局部视图将空模型发送到控制器操作方法
- html - 网页材料设计 - 如何进行自动完成输入
- sql - Oracle SQL - 模型子句中的可变列数