Spring + H2DB-Web-Console:“此方法无法确定这些模式是否是 Spring MVC 模式。”

问题描述 投票:0回答:1

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

我收到错误:

原因:java.lang.IllegalArgumentException:该方法无法判断这些模式是否是Spring MVC模式。如果此端点是 Spring MVC 端点,请使用 requestMatchers(MvcRequestMatcher);否则,请使用 requestMatchers(AntPathRequestMatcher)。

这是因为您的 servlet 上下文中有多个可映射 servlet:{org.h2.server.web.JakartaWebServlet=[/my-h2-console/*], org.springframework.web.servlet.DispatcherServlet=[/ ]}.

对于每个MvcRequestMatcher,调用MvcRequestMatcher#setServletPath来指示servlet路径。

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

...请使用 requestMatchers(MvcRequestMatcher);否则,请使用 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

似乎会带来任何改变。

spring-boot spring-mvc spring-security h2 web-console
1个回答
0
投票

尝试一下 antMatcher 怎么样? 我不懂 kotlin 但我希望它有帮助

 authorizeRequests {
            authorize(antMatcher("/my-h2-console/**"), permitAll)
            //...
        }
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.