spring - 需要类型为 'org.springframework.security.authentication.AuthenticationManager' 的 bean,但无法找到。来自 Spring 安全的消息

标签 spring spring-boot spring-mvc spring-security

我正在尝试使用 Spring Boot 为 Spring Security 实现一个示例演示,以检查身份验证。我正在尝试为 Spring Security 实现基本锻炼并收到以下消息,

Description:

Parameter 0 of constructor in com.spacestudy.service.CustomAuthenticationProvider required a bean of type 'org.springframework.security.authentication.AuthenticationManager' that could not be found.

Action:

Consider defining a bean of type 'org.springframework.security.web.AuthenticationEntryPoint' in your configuration.

我的安全配置类SecurityConfig.java,

@EnableWebSecurity
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
private AuthenticationEntryPoint authEntryPoint;

 @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
        .csrf().disable()
        .authorizeRequests()
        .anyRequest().authenticated()
        .and()
        .addFilter(new ApplicationContextHeaderFilter((ApplicationContext) authenticationManager()));
    }
}

我的 BasicAuthenticationFilter 实现如下所示,

@Component
public class  CustomAuthenticationProvider  extends  BasicAuthenticationFilter    { 

public CustomAuthenticationProvider(AuthenticationManager authenticationManager) {
    super(authenticationManager);
    // TODO Auto-generated constructor stub
}
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws IOException, ServletException {
    UsernamePasswordAuthenticationToken authentication = getAuthentication(request);
    SecurityContextHolder.getContext().setAuthentication(authentication);
    filterChain.doFilter(request, response);
}
private UsernamePasswordAuthenticationToken getAuthentication(HttpServletRequest request) {
   String bearerToken = request.getHeader("accessToken");
String username = "test";
String password = "test";
    if (username != null && !username.isEmpty()) {
        return new UsernamePasswordAuthenticationToken(username, null, null);
      }
    return null;
    }
 }

如何解决这个问题?

最佳答案

您可以尝试一下,将其放入配置安全文件中

 @Bean
    public AuthenticationManager authenticationManager(AuthenticationConfiguration authConfig) throws Exception {
        return authConfig.getAuthenticationManager();
    }

关于spring - 需要类型为 'org.springframework.security.authentication.AuthenticationManager' 的 bean,但无法找到。来自 Spring 安全的消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58671062/

相关文章:

java - Spring Boot 和 mongodb 中的连接池

spring - 为什么我在 em.persist(...) 上得到 "No transactional EntityManager available"?

java - MVC 中的推与拉模型

spring - LocalDate反序列化&序列化错误-jackson.databind.exc.InvalidDefinitionException : Cannot construct instance of java. time.LocalDate

eclipse - Spring框架入门

java - 如何在 Spring 3.1 中从多个参数生成自定义键以进行缓存抽象?

Java Spring 安全 : 401 Unauthorized for token OAuth2 end point

java - Jackson 解析中对象值为 null

java - 用 Hibernate/Guice/Wicket 替换 Hibernate/Spring/Spring MVC 堆栈

java - Spring Kafka 客户端 SSL 设置