尝试使用 spring security 设置我的 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.
我已经按照其他答案的建议覆盖了authenticationManagerBean,但这没有帮助。欢迎任何建议。我的 SecurityConfigurer 类如下。
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();
}
}
登录 Controller
@RestController
public class LoginController {
@Autowired
private AuthenticationManager authenticationManager;
最佳答案
查看代码,您似乎正在尝试在 LoginController 中 Autowiring SecurityConfigurer。但在 Spring Security 中,不需要 Autowiring 配置器类。另外,正如您所提到的,覆盖authenticationManagerBean只会为您提供AuthenticationManager类型的bean,而不是SecurityConfigurer类型。
关于java - 字段authenticationManager LoginController需要一个类型为AuthenticationManager'的bean,但无法找到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61398681/