首页 > 解决方案 > Spring Boot AuthenticationManagerBean 不起作用

问题描述

在我的项目中,当我试图AuthenticationManager通过覆盖AuthenticationManagerBean()它显示以下错误的方法来定义 bean 时,

The method authenticationManagerBean() of type SecurityConfiguration must override or implement a supertype method

虽然下面给出了我定义的方法,

 @Bean(name = BeanIds.AUTHENTICATION_MANAGER)
 @Override
 public AuthenticationManager authenticationManagerBean() throws Exception {
    return super.authenticationManagerBean();
 }


我的整个SecurityConfiguaration课程如下,

package com.toorjaridwan.introvertreadersauth.Configuration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.BeanIds;
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.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

import com.toorjaridwan.introvertreadersauth.Service.UserDetailService;

@Configuration
@EnableWebSecurity
public class SecurityConfiguration {

    @Autowired
    private UserDetailService userDetailService;

    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailService);
    }

    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable().authorizeRequests().antMatchers("/authenticate").permitAll()
        .anyRequest().authenticated();
    }

       @Bean(name = BeanIds.AUTHENTICATION_MANAGER)
       @Override
       public AuthenticationManager authenticationManagerBean() throws Exception {
           return super.authenticationManagerBean();
       }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return NoOpPasswordEncoder.getInstance();
    }

}


在之前使用 Spring Boot 版本2.1.9的项目中,一切都很顺利,完全没有问题。现在我使用的是 Spring Boot 版本2.2.6。不知道是不是这个原因。

在寻找解决方案时,我遇到了thisthis。我发现事情和我做的一样。

我正在运行 64 位 Ubuntu 16.04 LTS 和 Java 版本 1.8

标签: javaspring-bootspring-mvcauthentication

解决方案


尝试添加

extends WebSecurityConfigurerAdapter

到你的配置类


推荐阅读