gpt4 book ai didi

spring - Spring Boot/h2-console在Spring Security 1.5.2中抛出403

转载 作者:行者123 更新时间:2023-12-04 04:46:03 27 4
gpt4 key购买 nike

我们最近从Spring Boot 1.4.1升级到了1.5.2。 1.5.2的功能之一是,如果Spring Security是该软件包的一部分,那么它将受到基本身份验证的保护。即使经过基本身份验证,我也无法访问/h2-console。禁止抛出403。
application.yml:

spring:
datasource:
driver-class-name: org.h2.Driver
url: jdbc:h2:file:../app-db/app_db;AUTO_SERVER=TRUE
username: sa
password: sa
initialize: false
jpa:
hibernate:
ddl-auto: validate
show-sql: true
database-platform: org.hibernate.dialect.H2Dialect
h2:
console:
enabled: true
settings:
web-allow-others: true
allowed:
resources: /h2-console/**

我什至明确允许 /h2-console/**
 httpSecurity.authorizeRequests()
.antMatchers(allowedResources)
.permitAll()

尝试访问 localhost:8080/h2-console时,我一直收到403。
我尝试了许多设置以及放置:
management.security.enabled=true
security.basic.enabled=true

但是我无法访问h2-console。

最佳答案

Spring安全阻止H2数据库的/ h2-console(或您在application.yaml中配置的路径)路径。

要访问H2控制台,只需将以下代码添加到WebSecurityConfigurerAdapter中。

@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

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

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

不要在生产环境中使用此配置。 =)

关于spring - Spring Boot/h2-console在Spring Security 1.5.2中抛出403,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43794721/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com