首页 > 解决方案 > Spring Boot 安全性上的 NoSuchMethodError

问题描述

我正在关注本教程:https ://www.baeldung.com/spring-boot-security-autoconfiguration 添加简单身份验证。到我的 Spring Boot 项目。当我尝试运行我的应用程序时,我总是收到此错误:

工厂方法“springSecurityFilterChain”抛出异常;嵌套异常是 java.lang.NoSuchMethodError: org.springframework.util.Assert.isTrue(ZLjava/util/function/Supplier;)V

IDK 为什么我得到这个或者这与版本问题有关。你们对此有什么想法吗?

标签: springspring-bootspring-security

解决方案


您可以将您的问题与此答案联系起来

pom.xml

    <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>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-test</artifactId>
        <scope>test</scope>
    </dependency>

安全配置类

@Configuration
@EnableWebSecurity
public class Config extends WebSecurityConfigurerAdapter{

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.inMemoryAuthentication()
            .withUser("username")
            .password("password")
            .roles("ADMIN","USER");
 }

@Override
protected void configure(HttpSecurity http) throws Exception {

    http.cors().and()
            .csrf().disable()
            .authorizeRequests()
            .antMatchers("/greetings").hasRole("ADMIN")
            .and()
            .httpBasic()
            .and()
            .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
 }
}

一个简单的restController

@GetMapping("/greetings")
public String greetings(){
    return "Hello World!";
}

这是使用内存身份验证的基本安全性。您可以将 pom.xml 和 configuration.class 关联起来,在大多数情况下 spring-boot 会默认解析版本。


推荐阅读