无法自动装配。找不到任何类型为'AuthenticationProvider'的bean。

huangapple go评论84阅读模式
英文:

Could not autowire. No beans of 'AuthenticationProvider' type found

问题

无法在我的SecurityConfig类中自动装配AuthenticationProvider Spring框架实例这很奇怪因为我已经包含了必要的导入而且UserDetailsService另一个Spring框架类可以无问题地进行自动装配以下是我的代码

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.security.authentication.AuthenticationProvider;
    import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
    import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
    import org.springframework.security.config.annotation.web.builders.HttpSecurity;
    import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
    import org.springframework.security.core.userdetails.UserDetailsService;
    import org.springframework.security.web.authentication.RememberMeServices;
    import org.springframework.security.web.authentication.rememberme.TokenBasedRememberMeServices;

    @Configuration
    @EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)
    class SecurityConfig : WebSecurityConfigurerAdapter() {
        companion object {
            private const val REMEMBER_ME_KEY = "basdrwerdadev$#@safasfewrahl";
        }
    
        @Autowired
        private lateinit var authenticationProvider: AuthenticationProvider;
        
        @Autowired
        private lateinit var userDetailsService: UserDetailsService;
        ...
英文:

I cant autowire an AuthenticationProvider Spring framework instance in my SecurityConfig class. This is strange as I have included the necessary import and the UserDetailsService (another Spring Framework class) can be autowired without any issues. This is my code:

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.security.authentication.AuthenticationProvider
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity
import org.springframework.security.config.annotation.web.builders.HttpSecurity
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter
import org.springframework.security.core.userdetails.UserDetailsService
import org.springframework.security.web.authentication.RememberMeServices
import org.springframework.security.web.authentication.rememberme.TokenBasedRememberMeServices

@Configuration
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)
class SecurityConfig : WebSecurityConfigurerAdapter() {
    companion object {
        private const val REMEMBER_ME_KEY = "basdrwerdadev$#@safasfewrahl"
    }

    @Autowired private lateinit var authenticationProvider: AuthenticationProvider
    @Autowired private lateinit var userDetailsService: UserDetailsService
    ...

Maybe I am missing a dependency? I have added the Spring security dependency

答案1

得分: 2

首先要检查的两件事是:
i) 您的自定义身份验证提供程序是否已标注为服务/组件(即Spring是否可以看到并管理它?)
ii) 它是否扩展了Spring的AuthenticationManager接口?

英文:

First 2 things I would check are:<br>
i) is your custom authentication provider annotated as a Service/Component (i.e. can spring see and manage it?) <br>
ii) Does it extend spring's AuthenticationManager interface?

huangapple
  • 本文由 发表于 2020年10月1日 17:57:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/64153034.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定