Spring Boot 从3.0.8到3.0.9的Sping Boot 迁移后,SecurityFilterChain无法示例化

bvjveswy  于 2023-08-04  发布在  Spring
关注(0)|答案(2)|浏览(143)

在我的spring Boot 项目中,我有以下SecurityFilterChain的定义:

public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
  http

    // public routes
    .authorizeHttpRequests()
    .requestMatchers("/", "/favicon.ico", "/v3/api-docs*")
    .permitAll()
    .and()

    // enable security for the log-view
    .authorizeHttpRequests()
    .requestMatchers("/log")
    .hasAnyRole(ROLE_LOGVIEWER)
    .and()

    // enable security for the health check
    .authorizeHttpRequests()
    .requestMatchers("/manage/health")
    .hasAnyRole(ROLE_HEALTH)
    .and()

    // enable basic-auth and ROLE_USER for all other routes
    .authorizeHttpRequests()
    .anyRequest()
    .hasAnyRole(ROLE_USER)
    .and()
    .httpBasic();

  return http.build();
}

字符串
它在几个模型测试中进行了测试,并在生产环境中按预期运行。
但是在从spring-boot 3.0.8迁移到3.0.9之后,我得到了以下错误:

Factory method 'filterChain' threw exception with message: 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).


我试图使用一个更具体的请求匹配器,但没有成功。
有什么提示吗?

ui7jx7zq

ui7jx7zq1#

我在Spring Security 6.1.2的一个非常基本的配置中遇到了同样的异常。我解决了它:

@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
    return http.
            authorizeHttpRequests(requests -> {
                    requests.requestMatchers(new AntPathRequestMatcher("/"), new AntPathRequestMatcher("/style.css")).permitAll();
                    requests.requestMatchers(new AntPathRequestMatcher("/secure/**")).hasAuthority("MyAuthority");
            }).

字符串
我不确定这是否是正确的方法,但它似乎确实有效。在Spring Security 6中,AbstractRequestMatcherRegistry不再有.antMatchers()方法。同样,不确定这是否是最好的方法,也不完全确定为什么我想要一个蚂蚁匹配器和一个MVC端点匹配器。

ogq8wdun

ogq8wdun2#

user 2959589的答案告诉我正确的方法,谢谢!

http
    // public routes
    .authorizeHttpRequests()
    .requestMatchers(AntPathRequestMatcher.antMatcher("/"))
    .permitAll()
    .requestMatchers(AntPathRequestMatcher.antMatcher("/favicon.ico"))
    .permitAll()
    .requestMatchers(AntPathRequestMatcher.antMatcher("/v3/api-docs*"))
    .permitAll()
    .and()

    // enable security for the log-view
    .authorizeHttpRequests()
    .requestMatchers(AntPathRequestMatcher.antMatcher("/log"))
    .hasAnyRole(ROLE_LOGVIEWER)
    .and()

    // enable security for the health check
    .authorizeHttpRequests()
    .requestMatchers(AntPathRequestMatcher.antMatcher("/manage/health"))
    .hasAnyRole(ROLE_HEALTH)
    .and()

    // enable basic-auth and ROLE_USER for all other routes
    .authorizeHttpRequests()
    .anyRequest()
    .hasAnyRole(ROLE_USER)
    .and()
    .httpBasic();

字符串

相关问题