简体   繁体   English

字段 authenticationManager LoginController 需要一个无法找到的 typeAuthenticationManager' bean

[英]Field authenticationManager LoginController required a bean of typeAuthenticationManager' that could not be found

Getting this error when trying to set up my SecurityConfigurer with spring security.尝试使用 spring 安全性设置我的 SecurityConfigurer 时出现此错误。

APPLICATION FAILED TO START

Description:

Field authenticationManager in com.springvuegradle.Controller.LoginController required a bean of type 'org.springframework.security.authentication.AuthenticationManager' that could not be found.

The injection point has the following annotations:
        - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'org.springframework.security.authentication.AuthenticationManager' in your configuration.

I Have overrided the authenticationManagerBean as suggested by other answers, however this has not helped.我已经按照其他答案的建议覆盖了 authenticationManagerBean ,但这并没有帮助。 Any advice welcome.欢迎任何建议。 My SecurityConfigurer class is below.我的 SecurityConfigurer class 在下面。

package com.springvuegradle.Security;

import com.springvuegradle.Services.MyUserDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.authentication.AuthenticationManager;
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.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

public class SecurityConfigurer extends WebSecurityConfigurerAdapter {

    @Autowired
    private MyUserDetailsService myUserDetailsService;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(myUserDetailsService);
    }

    /***
     * Requires authentication for all endpoints except the /authenticate endpoint.
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/authenticate").permitAll().anyRequest().authenticated();
    }

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

    @Bean
    public PasswordEncoder passwordEncoder(){
        //TODO change this to actually hash the password
        return NoOpPasswordEncoder.getInstance();
    }
}

LoginController登录控制器

@RestController
public class LoginController {

    @Autowired
    private AuthenticationManager authenticationManager;

Looking at the code, it seems you are trying to autowire SecurityConfigurer in LoginController.查看代码,您似乎正在尝试在 LoginController 中自动装配 SecurityConfigurer。 But in spring security, there is no need to autowire the configurer class.但是在 spring 安全性中,不需要自动装配配置器 class。 Also, as mentioned by you, overriding the authenticationManagerBean will only provide you a bean of AuthenticationManager type, not the SecurityConfigurer type.此外,正如您所提到的,覆盖 authenticationManagerBean 只会为您提供 AuthenticationManager 类型的 bean,而不是 SecurityConfigurer 类型。

I had similar issue,I discovered that I forgot to add @Configuration @EnableWebSecurity to my SecurityConfig file.我有类似的问题,我发现我忘记将@Configuration @EnableWebSecurity 添加到我的SecurityConfig 文件中。

 @Configuration @EnableWebSecurity
 public class SecurityConfigurer extends WebSecurityConfigurerAdapter {}

This should fix your issue.It will autowire the AuthenticationManager bean这应该可以解决您的问题。它将自动装配 AuthenticationManager bean

Also this NoOpPasswordEncoder.getInstance();还有这个 NoOpPasswordEncoder.getInstance(); is deprecated in spring 2.5.5 use this instead在 spring 2.5.5 中已弃用,请改用此

   //deprecated in spring boot 2.5.5
   @Bean
   public PasswordEncoder passwordEncoder(){
    return NoOpPasswordEncoder.getInstance();
   }
   //Use this below
   @Bean
  PasswordEncoder getPasswordEncoder() {
     return new BCryptPasswordEncoder();
  }

暂无
暂无

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

相关问题 *** 中的字段 authenticationManager 需要一个找不到的 'org.springframework.security.authentication.AuthenticationManager' 类型的 bean - Field authenticationManager in *** required a bean of type 'org.springframework.security.authentication.AuthenticationManager' that could not be found 字段需要在springboot中找不到的bean - Field required a bean which could not be found in springboot 字段......在......需要一个类型的bean......无法找到错误 - Field ... in ... required a bean of type ... that could not be found Error 字段ClientService需要在Spring中找不到的bean - Field ClientService required a bean that could not be found in Spring (…)中的字段memberRepo需要一个找不到类型的bean - Field memberRepo in (…) required a bean of type that could not be found 字段需要一个类型为…的Bean。 - Field required a bean of type … that could not be found 字段需要一个无法找到的类型的 bean - Field required a bean of type that could not be found service.SecurityServiceImpl 中的字段 authenticationManager 需要类型为“org.springframework.security.authentication.AuthenticationManager”的 bean - Field authenticationManager in service.SecurityServiceImpl required a bean of type 'org.springframework.security.authentication.AuthenticationManager' Openfeign + Spring cloud:字段需要一个无法找到的类型的bean - Openfeign + Spring cloud : Field required a bean of type that could not be found Controller中的字段需要找不到名为'entityManagerFactory'的bean - Field in Controller required a bean named 'entityManagerFactory' that could not be found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM