Spring Security Spring + H2DB-Web-Console:“此方法无法决定这些模式是否为Spring MVC模式,”

mftmpeh8  于 2023-10-20  发布在  Spring
关注(0)|答案(3)|浏览(125)

我的最终目标是在我的应用程序中使用h2db-web-console,作为我的本地开发环境的一部分。
我收到错误消息:
原因:java.lang.IllegalArgumentException:这个方法不能决定这些模式是否是Spring MVC模式。如果此端点是Spring MVC端点,请使用requestMatchers(MvcRequestMatcher);否则,请使用requestMatchers(AntPathRequestMatcher)。
这是因为在servlet上下文中有多个可Map的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(这应该会自动与'authorize'一起使用)。

安全配置

@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启动一般,所以请温柔。关于这个主题的任何类型的信息都非常感谢。:)
我试过:

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

=>仅关闭/打开

spring:
  h2:
    console:
      enabled: true

似乎带来了任何改变。

gmxoilav

gmxoilav1#

这个配置帮助我解决了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();
    }
}

更多信息.
更新了H2控制台的代码,以便在浏览器中正常工作。
也可以使用PathRequest.toH2Console()代替AntPathRequestMatcher.antMatcher("/h2-console/**")

cidc1ykv

cidc1ykv2#

试试antMatcher怎么样?我不知道Kotlin,但我希望它能帮助

authorizeRequests {
            authorize(antMatcher("/my-h2-console/**"), permitAll)
            //...
        }
lf5gs5x2

lf5gs5x23#

@Denis Korolev这个解决了,谢谢!

@Bean
    fun securityFilterChainTestEnv(http: HttpSecurity): SecurityFilterChain {
        http {
                ...
            }

            headers { frameOptions { disable() } }
            csrf { ignoringRequestMatchers(AntPathRequestMatcher("/h2-console/**")) }

            http.authorizeHttpRequests { httpSecurity ->
                val mvcIntrospector = HandlerMappingIntrospector()
                httpSecurity
"/ping-test")).permitAll()
                    .requestMatchers(AntPathRequestMatcher("/h2-console/**")).permitAll()
                    .anyRequest().denyAll()
            }
        }
        return http.build()
    }

相关问题