[英]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.