requestMatchers giving Error in spring security how to solve this error?

201 Views Asked by At

I'm trying to learn spring security for my spring boot project. As I cannot use WebSecurityConfigurerAdapter due to its depreciation ,im using SecurityFilterChain. Here is My working code.

ServiceConfig:

package com.spring.security.demo;

import static org.springframework.security.config.Customizer.withDefaults;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.web.SecurityFilterChain;

 @Configuration
 @EnableWebSecurity
 public class SecurityConfig {

    @Configuration
    public class SecurityConfiguration {

        @Bean
       SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
            http
                    .cors(cors -> cors.disable())
                    .csrf(csrf -> csrf.disable())
                    .formLogin(login -> login.disable())
                    .authorizeHttpRequests((authz) -> authz
                           .anyRequest().authenticated()
                    )
                    .httpBasic(withDefaults());
        return http.build();
        }

    }
}

and this is my Controller

package com.spring.security.demo.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HomeController {

    @GetMapping("/home")
    public String Home() {
        return "Welcome to Home Page !";
        
    }
}

but when i change the code securityFilterChain code to

@Configuration
    public class SecurityConfiguration {

        @Bean
       SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
            http
                    .cors(cors -> cors.disable())
                    .csrf(csrf -> csrf.disable())
                    .formLogin(login -> login.disable())
                    .authorizeHttpRequests((authz) -> authz
                                    .requestMatchers("/home").permitAll()
                                    .anyRequest().authenticated()
                    )
                    .httpBasic(withDefaults());
            return http.build();
        }
    } 
This is my 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.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.spring.security.demo</groupId>
    <artifactId>Spring-security-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Spring-security-demo</name>
    <description>Spring Boot security</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper -->
<dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>tomcat-jasper</artifactId>
    <version>10.1.15</version>
</dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <image>
                        <builder>paketobuildpacks/builder-jammy-base:latest</builder>
                    </image>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

its giving me

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'filterChain' defined in class path resource [com/spring/security/demo/SecurityConfig$SecurityConfiguration.class]: Failed to instantiate [org.springframework.security.web.SecurityFilterChain]: 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).
For each MvcRequestMatcher, call MvcRequestMatcher#setServletPath to indicate the servlet path. 

This error in console.

And im using spring Boot 3.1.5

if anyone have spare time please teach me !

0

There are 0 best solutions below