英文:
Failed to configure a DataSource in Spring Boot with H2 in-memory database
问题
我正在开发一个使用H2作为内嵌内存数据库的Spring Boot应用程序。当我尝试访问H2控制台时遇到了问题。每当我导航到http://localhost:8080/h2-console时,它会提示我输入用户名和密码。
控制台输出:
2023-08-03T19:41:47.901+03:00 INFO 5176 --- [ main] c.e.a.AlbacamiereaApplication : 使用Java 17.0.5启动AlbacamiereaApplication,PID为5176(C:\albacamierea\target\classes中的Robert启动)
2023-08-03T19:41:47.916+03:00 INFO 5176 --- [ main] c.e.a.AlbacamiereaApplication : 未设置活动配置文件,回退到默认配置文件:“default”
2023-08-03T19:41:50.102+03:00 INFO 5176 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : 以默认模式引导Spring Data JPA存储库。
2023-08-03T19:41:50.167+03:00 INFO 5176 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : 在38毫秒内完成了Spring Data存储库扫描。找到0个JPA存储库接口。
2023-08-03T19:41:51.805+03:00 INFO 5176 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat使用端口(们)初始化:8080(http)
...
(中间省略)
...
2023-08-03T19:41:58.582+03:00 WARN 5176 --- [ main] .s.s.UserDetailsServiceAutoConfiguration :
使用生成的安全密码:dab22a6d-fca6-4e5d-9796-8f4d30c2c9b5
我尝试过使用空白的用户名和密码,以及默认的"SA"用户名和无密码访问H2控制台,但仍然无法正常工作。
英文:
I am developing a Spring Boot application with H2 as an embedded in-memory database. I'm facing an issue while trying to access the H2 console. Whenever I navigate to http://localhost:8080/h2-console, it prompts me for a username and password.
The console output:
2023-08-03T19:41:47.901+03:00 INFO 5176 --- [ main] c.e.a.AlbacamiereaApplication : Starting AlbacamiereaApplication using Java 17.0.5 with PID 5176 (C:\albacamierea\target\classes started by Robert in C:\albacamierea)
2023-08-03T19:41:47.916+03:00 INFO 5176 --- [ main] c.e.a.AlbacamiereaApplication : No active profile set, falling back to 1 default profile: "default"
2023-08-03T19:41:50.102+03:00 INFO 5176 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2023-08-03T19:41:50.167+03:00 INFO 5176 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 38 ms. Found 0 JPA repository interfaces.
2023-08-03T19:41:51.805+03:00 INFO 5176 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2023-08-03T19:41:51.809+03:00 INFO 5176 --- [ main] o.a.catalina.core.AprLifecycleListener : Loaded Apache Tomcat Native library [2.0.3] using APR version [1.7.2].
2023-08-03T19:41:51.851+03:00 INFO 5176 --- [ main] o.a.catalina.core.AprLifecycleListener : OpenSSL successfully initialized [OpenSSL 3.0.8 7 Feb 2023]
2023-08-03T19:41:51.891+03:00 INFO 5176 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2023-08-03T19:41:51.892+03:00 INFO 5176 --- [ main] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.11]
2023-08-03T19:41:52.247+03:00 INFO 5176 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2023-08-03T19:41:52.248+03:00 INFO 5176 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 4149 ms
2023-08-03T19:41:52.757+03:00 INFO 5176 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2023-08-03T19:41:53.629+03:00 INFO 5176 --- [ main] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Added connection conn0: url=jdbc:h2:mem:d48ab7e7-383a-4245-846e-d273ef5237bd user=SA
2023-08-03T19:41:53.637+03:00 INFO 5176 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2023-08-03T19:41:53.884+03:00 INFO 5176 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2023-08-03T19:41:54.170+03:00 INFO 5176 --- [ main] org.hibernate.Version : HHH000412: Hibernate ORM core version 6.2.6.Final
2023-08-03T19:41:54.178+03:00 INFO 5176 --- [ main] org.hibernate.cfg.Environment : HHH000406: Using bytecode reflection optimizer
2023-08-03T19:41:54.854+03:00 INFO 5176 --- [ main] o.h.b.i.BytecodeProviderInitiator : HHH000021: Bytecode provider name : bytebuddy
2023-08-03T19:41:55.528+03:00 INFO 5176 --- [ main] o.s.o.j.p.SpringPersistenceUnitInfo : No LoadTimeWeaver setup: ignoring JPA class transformer
2023-08-03T19:41:56.089+03:00 INFO 5176 --- [ main] o.h.b.i.BytecodeProviderInitiator : HHH000021: Bytecode provider name : bytebuddy
2023-08-03T19:41:57.195+03:00 INFO 5176 --- [ main] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2023-08-03T19:41:57.221+03:00 INFO 5176 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2023-08-03T19:41:57.358+03:00 WARN 5176 --- [ main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2023-08-03T19:41:58.582+03:00 WARN 5176 --- [ main] .s.s.UserDetailsServiceAutoConfiguration :
Using generated security password: dab22a6d-fca6-4e5d-9796-8f4d30c2c9b5
I have tried accessing the H2 console with a blank username and password, as well as with the default "SA" username and no password, but it still doesn't work.
答案1
得分: 3
看起来您已启用了Spring Security,但尚未明确允许访问H2端点的路径。
针对仅H2的可能SecurityFilterChain设置(应通过@Order
注解放置在其他FilterChains之前):
@Bean
@Order(5)
// @Profile("development")
public SecurityFilterChain h2FilterChain(
HttpSecurity http,
@Value("${spring.h2.console.path:/h2-console}") String h2ConsolePath) throws Exception {
return http
.securityMatchers(matchers -> matchers
/* https://github.com/spring-projects/spring-security/issues/12546#issuecomment-1382891839 */
.requestMatchers(AntPathRequestMatcher.antMatcher(h2ConsolePath + "/**")))
.authorizeHttpRequests(authorize -> authorize.anyRequest().permitAll())
/* H2控制台运行在一个框架中。因此,我们需要将'X-Frame-Options'从'deny'设置为'sameOrigin'。 */
.headers(header -> header.frameOptions(HeadersConfigurer.FrameOptionsConfig::sameOrigin))
/* 否则登录到H2将无法正常工作。 */
.csrf(CsrfConfigurer::disable)
.build();
}
确保正确设置属性中的h2-console值。
英文:
It seems that you have Spring Security enabled but not explicitly allowed the path to your H2 endpoint.
A possible SecurityFilterChain setup for H2 only (should be positioned in front of the other FilterChains via @Order
annotation):
@Bean
@Order(5)
// @Profile("development")
public SecurityFilterChain h2FilterChain(
HttpSecurity http,
@Value("${spring.h2.console.path:/h2-console}") String h2ConsolePath) throws Exception {
return http
.securityMatchers(matchers -> matchers
/* https://github.com/spring-projects/spring-security/issues/12546#issuecomment-1382891839 */
.requestMatchers(AntPathRequestMatcher.antMatcher(h2ConsolePath + "/**")))
.authorizeHttpRequests(authorize -> authorize.anyRequest().permitAll())
/* The H2 console runs in a frameset. Thus, we need to set 'X-Frame-Options' from 'deny' to 'sameOrigin'. */
.headers(header -> header.frameOptions(HeadersConfigurer.FrameOptionsConfig::sameOrigin))
/* Otherwise login into H2 won't work. */
.csrf(CsrfConfigurer::disable)
.build()
}
Make sure to set the h2-console value in the properties correctly.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论