Facing Exception in spring security filterchain-如果此端点是Spring MVC端点,请使用requestMatchers(MvcRequestMatcher)

kx7yvsdv  于 2023-10-20  发布在  Spring
关注(0)|答案(1)|浏览(159)

我正在做一个spring Boot 个人项目,我在spring security filterchain class中遇到了异常。我尝试了各种解决方案,但不幸的是没有任何效果。

SECURITY CONFIG

@Configuration
@EnableWebSecurity
public class SecurityConfig {
    
    
    
    //INMemory userDetails DB  ,PasswordEncoder is a interface ,BCryptPasswordEncoder is one of the implementation
    @Bean
    public InMemoryUserDetailsManager detailsManager(PasswordEncoder p) {
        UserDetails user=User.withUsername("springer1")
                            .password(p.encode("secret"))
                            .roles("admin")
                            .build();
        UserDetails admin=User.withUsername("springer2")
                              .password(p.encode("secret"))
                              .roles("user")
                              .build();;
        return new InMemoryUserDetailsManager(user,admin);
    }
    
    @Bean
    BCryptPasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
    

    
    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
            http
                     // If you are only creating a service that is used by non-browser clients, you will likely want to disable CSRF protection.
                    .authorizeHttpRequests( (authorizeHttpRequests)->
                        authorizeHttpRequests
                            .requestMatchers("/users").hasRole("admin") 
                            .requestMatchers("/users","/users/**").hasAnyRole("admin","user")
                            .requestMatchers("/users/**").hasRole("admin")
                            .requestMatchers("/","/h2-console/**").permitAll()
                            .requestMatchers("/").permitAll() );
                    
             
            return http.build();
                    
    }

}
POM.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.1.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>userApp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>userApp</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-hateoas</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
            
        </dependency>
        
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-core</artifactId>
            <version>6.1.2</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-rest-hal-explorer</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
            
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springdoc/springdoc-openapi-ui 
        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-ui</artifactId>
            <version>1.7.0</version>
        </dependency> -->
         <dependency>
              <groupId>org.springdoc</groupId>
              <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
              <version>2.2.0</version>
         </dependency>
         <!-- https://mvnrepository.com/artifact/junit/junit -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <scope>test</scope>
            </dependency>


    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

例外:
org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为“org.springframework.security.config.annotation.web.configuration. WebSecurityConfiguration”的bean时出错:通过方法“setFilterChains”参数0表示的不满足的依赖项:创建在类路径资源[com/userApp/Security/SecurityConfig.class]中定义的名为“filterChain”的Bean时出错:无法示例化[org.springframework.security.web.SecurityFilterChain]:工厂方法'filterChain'抛出异常,消息为:这个方法不能决定这些模式是否是Spring MVC模式。如果此端点是Spring MVC端点,请使用requestMatchers(MvcRequestMatcher);否则,请使用requestMatchers(AntPathRequestMatcher)。
原因:java.lang.IllegalArgumentException:这个方法不能决定这些模式是否是Spring MVC模式。如果此端点是Spring MVC端点,请使用requestMatchers(MvcRequestMatcher);否则,请使用requestMatchers(AntPathRequestMatcher)。
我是springboot的新手,有谁能帮助我理解,为了避免这个异常,我应该改变什么?我哪里出错了?
我尝试按照stackoverflow中给出的建议将spring安全依赖升级到6.1.1到6.1.2。但是它不起作用,而且我也没有在我的项目中使用任何额外的servlet。

vngu2lb8

vngu2lb81#

没有确切的解决方案,但当我将Sping Boot 版本更改为3.1.1时,它可以工作

相关问题