英文:
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?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论