简体   繁体   English

class org.springframework.security.config.annotation.web.AbstractRequestMatcherRegistry 中的方法 requestMatchers<c> 不能应用于给定类型;</c>

[英]method requestMatchers in class org.springframework.security.config.annotation.web.AbstractRequestMatcherRegistry<C> cannot be applied to given types;

I have a problem with authorize.requestMatchers where when I pass a string it is not being accepted in the compilation I'm new to spring security and I still haven't been able to find a solution for this error below is a print of my class in intellJ: https://uploaddeimagens.com.br/imagens/4N5u8eA我有一个 authorize.requestMatchers 的问题,当我传递一个字符串时,它在编译中没有被接受我是 spring 安全的新手,我仍然无法找到这个错误的解决方案下面是我的打印class in intellJ: https://uploaddeimagens.com.br/imagens/4N5u8eA

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;

@Configuration
@EnableWebSecurity
public class SpringSecurity {

    @Autowired
    private UserDetailsService userDetailsService;

    @Bean
    public static PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http.csrf().disable()
                .authorizeHttpRequests((authorize) ->
                        authorize.requestMatchers("/register/**").permitAll()
                                .requestMatchers("/index").permitAll()
                                .requestMatchers("/users").hasRole("ADMIN")
                ).formLogin(
                        form -> form
                                .loginPage("/login")
                                .loginProcessingUrl("/login")
                                .defaultSuccessUrl("/users")
                                .permitAll()
                ).logout(
                        logout -> logout
                                .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
                                .permitAll()
                );
        return http.build();
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth
                .userDetailsService(userDetailsService)
                .passwordEncoder(passwordEncoder());
    }
}

Pom.xml密码.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>2.6.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.xxxxxxxxx</groupId>
    <artifactId>xxxxxxxxxxxxxxx</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>xxxxxxxxxxxxxx</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
        <squiggly.version>1.3.18</squiggly.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.firebirdsql.jdbc</groupId>
            <artifactId>jaybird-jdk17</artifactId>
            <version>3.0.10</version>
        </dependency>
        <dependency>
            <groupId>net.java.dev.jna</groupId>
            <artifactId>jna</artifactId>
            <version>5.5.0</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hibernate.orm</groupId>
            <artifactId>hibernate-community-dialects</artifactId>
            <version>6.0.0.Alpha9</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</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-thymeleaf</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-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>com.github.bohnman</groupId>
            <artifactId>squiggly-filter-jackson</artifactId>
            <version>1.3.18</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </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>

What URL do you call and how do you call it? URL叫什么,怎么叫?

I think the requestMatcher doesn't match as you call it.我认为requestMatcher与您所说的不匹配。 See the code below.请参阅下面的代码。

If you call localhost:8080/login.html then your code should be like:如果你调用localhost:8080/login.html那么你的代码应该是这样的:

  • antMatcher("/login.html") or antMatcher("/login.html")
  • antMatcher("/login*")

Example:例子:

@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
    http.csrf().disable()
        .authorizeRequests()
            .antMatchers("/admin/**").hasRole("ADMIN")
            .antMatchers("/anonymous*").anonymous()
            .antMatchers("/login*").permitAll()
            .anyRequest().authenticated()
            .and()
}

https://www.baeldung.com/spring-security-login https://www.baeldung.com/spring-security-login

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法自省类 [org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration] - Failed to introspect Class [org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration] 启动 spring 引导时出错 - 无法自省 Class [org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration] - Error starting spring boot - Failed to introspect Class [org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration] 另一个类中的方法不能应用于给定类型 - Method in another class cannot be applied to given types “类中的方法不能应用于给定类型”的问题 - Issue with “Method in Class cannot be applied to given types” Java:接口org.springframework.data.jpa.repository.JpaRepository中的方法saveAllAndFlush<t,id> 不能应用于给定类型</t,id> - Java: method saveAllAndFlush in interface org.springframework.data.jpa.repository.JpaRepository<T,ID> cannot be applied to given types 类中的方法不能应用于给定类型 - method in class cannot be applied to given types 类中的 Java 方法不能应用于给定类型 - Java method in class cannot be applied to given types 类中的方法无法应用于给定类型错误 - method in class cannot be applied to given types error class 中的方法不能应用于给定类型 - method in class cannot be applied to given types Oauth2 创建名为“org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration”的 bean 时出错 - Oauth2 Error creating bean with name 'org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM