Java Field authenticationManager LoginController需要一个类型为authenticationManager';那是找不到的
尝试使用spring security设置我的SecurityConfigure时出现此错误Java Field authenticationManager LoginController需要一个类型为authenticationManager';那是找不到的,java,spring,spring-boot,spring-security,Java,Spring,Spring Boot,Spring Security,尝试使用spring security设置我的SecurityConfigure时出现此错误 APPLICATION FAILED TO START Description: Field authenticationManager in com.springvuegradle.Controller.LoginController required a bean of type 'org.springframework.security.authentication.Authenticatio
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,但是这没有帮助。欢迎任何意见。下面是我的SecurityConfigure类
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();
}
}
登录控制器
@RestController
public class LoginController {
@Autowired
private AuthenticationManager authenticationManager;
查看代码,似乎您正在尝试在LoginController中自动连接SecurityConfigure。但是在SpringSecurity中,不需要自动连接configurer类。另外,正如您所提到的,重写authenticationManagerBean只会为您提供AuthenticationManager类型的bean,而不是SecurityConfigurer类型的bean。您好,谢谢您的回复。我相信我只是想在我的LoginController中自动连接一个AuthenticationManager bean。我已将相关代码添加到我的原始帖子中。您似乎忘记在SecurityConfigure类上添加@EnableWebSecurity注释。即使添加了配置和@EnableWebSecurity注释,错误仍然存在。您认为这可能是项目配置的问题吗?应用@EnableWebSecurity注释后,情况不应该如此。我尝试了同样的代码,它对我很有效。它将能够找到AuthenticationManager bean,并且完全可以自动连接。