gpt4 book ai didi

spring-boot - Spring + H2DB-Web-控制台 : "This method cannot decide whether these patterns are Spring MVC patterns or not."

转载 作者:行者123 更新时间:2023-12-03 07:51:26 24 4
gpt4 key购买 nike

我的最终目标是使用h2db-web-console在我的应用程序中,作为本地开发环境的一部分。

我收到错误:

Caused by: java.lang.IllegalArgumentException: This method cannot decide whether these patterns are Spring MVC patterns or not. If this endpoint is a Spring MVC endpoint, please use requestMatchers(MvcRequestMatcher); otherwise, please use requestMatchers(AntPathRequestMatcher).

This is because there is more than one mappable servlet in your servlet context: {org.h2.server.web.JakartaWebServlet=[/my-h2-console/*], org.springframework.web.servlet.DispatcherServlet=[/]}.

For each MvcRequestMatcher, call MvcRequestMatcher#setServletPath to indicate the servlet path.

虽然这听起来很有描述性,但它让我发疯,因为看起来我为 JakartaWebServlet=[/my-h2-console/*] 使用哪条路径并不重要,因为 DispatcherServlet=[ /] 简单地匹配所有内容。以“/”开头...这就是一切。

...please use requestMatchers(MvcRequestMatcher); otherwise, please use requestMatchers(AntPathRequestMatcher)...

嗯,这些在 Spring 3.x.x 中已被弃用 patchnotes所以我尝试使用 RequestMatcher(这应该自动与“授权”一起使用)。

安全配置

    @Bean
@Order(GENERAL_SECURITY_CONFIGURATION_ORDER)
fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
http {
...
}
securityMatcher("/**")
headers { frameOptions { disable() } }
csrf { ignoringRequestMatchers("/my-h2-console/**") }

authorizeRequests {
authorize("/my-h2-console/**", permitAll)
authorize("/ping", permitAll)
authorize("/**", denyAll)
}
}
return http.build()
}

pom.xml

        <dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope> <!-- Should be "test", revert later -->
</dependency>

application.yml

spring:
h2:
console:
enabled: true
path: /my-h2-console
settings:
trace: false
web-allow-others: false
datasource:
url: jdbc:h2:mem:testdb
driverClassName: org.h2.Driver
username: sa
password:
jpa:
database-platform: org.hibernate.dialect.H2Dialect
sql:
init:
mode: embedded
schema-locations: classpath:sql/schema.testdb.local.sql

请记住,我对 spring-boot 还很陌生,所以请保持温柔。欢迎提供有关此主题的任何信息。 :)

我尝试过:

  • 如上所述更改 servlet 的路径/名称
  • 更改了我的 H2DB 的范围
  • 在我的安全配置中尝试了不同的配置

=> 仅关闭/打开

spring:
h2:
console:
enabled: true

似乎会带来任何改变。

最佳答案

此配置帮助我解决了 H2 问题:

@Configuration
public class SecurityConfig {

// My enpdoints start from /v1 so this pattern is ok for me
private static final String API_URL_PATTERN = "/v1/**";

@Bean
public SecurityFilterChain getSecurityFilterChain(HttpSecurity http,
HandlerMappingIntrospector introspector) throws Exception {
MvcRequestMatcher.Builder mvcMatcherBuilder = new MvcRequestMatcher.Builder(introspector);

http.csrf(csrfConfigurer ->
csrfConfigurer.ignoringRequestMatchers(mvcMatcherBuilder.pattern(API_URL_PATTERN),
PathRequest.toH2Console()));

http.headers(headersConfigurer ->
headersConfigurer.frameOptions(HeadersConfigurer.FrameOptionsConfig::sameOrigin));

http.authorizeHttpRequests(auth ->
auth
.requestMatchers(mvcMatcherBuilder.pattern(API_URL_PATTERN)).permitAll()
//This line is optional in .authenticated() case as .anyRequest().authenticated()
//would be applied for H2 path anyway
.requestMatchers(PathRequest.toH2Console()).authenticated()
.anyRequest().authenticated()
);

http.formLogin(Customizer.withDefaults());
http.httpBasic(Customizer.withDefaults());

return http.build();
}
}

更多 info .

更新了 H2 控制台的代码,以便在浏览器中正常工作。

此外,还可以使用 PathRequest.toH2Console() 代替 AntPathRequestMatcher.antMatcher("/h2-console/**")

关于spring-boot - Spring + H2DB-Web-控制台 : "This method cannot decide whether these patterns are Spring MVC patterns or not.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77024398/

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