Void Linux下H2 Console无法访问,Windows环境运行正常
问题分析与解决方案
从日志来看,核心问题是主机名解析失败:java.net.UnknownHostException: SlepiLepi,这导致H2控制台的Servlet初始化失败,最终触发Spring Security的403错误(请求转发到/error路径,而该路径需要认证)。以下是针对性的解决步骤:
1. 修复Void Linux的主机名映射问题
Void Linux默认不会自动将本地主机名映射到回环地址,需要手动修改/etc/hosts文件:
- 打开终端,编辑
/etc/hosts:sudo nano /etc/hosts - 添加以下两行(将
SlepiLepi替换为你的实际主机名):127.0.0.1 SlepiLepi ::1 SlepiLepi - 保存退出后,重启Spring Boot应用。
2. 确认Spring Security配置的完整性
你的配置已经包含必要设置,可做以下优化确保生效:
- 确保
AntPathRequestMatcher路径匹配正确(因设置了server.servlet.context-path=/api,/h2-console/**会匹配/api/h2-console/**,逻辑正确)。 - 可将
AntPathRequestMatcher替换为更简洁的lambda写法(可选):.requestMatchers("/h2-console/**").permitAll() - 再次确认
csrf().disable()和headers().frameOptions().disable()已正确配置,这两项是H2控制台正常访问的必要条件。
3. 验证H2控制台的访问路径
确保使用正确的URL访问:
- 当
context-path=/api时,访问地址应为:http://localhost:8080/api/h2-console - 若关闭
context-path,则访问:http://localhost:8080/h2-console
4. 额外排查(若上述步骤无效)
- 强制JVM使用IPv4解析,避免IPv6问题,启动应用时添加参数:
java -jar your-app.jar -Djava.net.preferIPv4Stack=true - 在
application.properties中明确指定H2控制台路径:spring.h2.console.path=/h2-console
内容的提问来源于stack exchange,提问作者SlepiMis
相关产品推荐
相关产品推荐

