java - 字段authenticationManager LoginController需要一个类型为AuthenticationManager'的bean,但无法找到

标签 java spring spring-boot spring-security

尝试使用 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/

相关文章:

java - 我们如何从 GraphQL 的 RequestContextHolder 获取 GraphQL 的 HttpServletRequest(DefaultGlobalContext)(使用 graphql-spqr-spring-boot-starter)?

java - 在java中通过搜索和删除旧子字符串来创建子字符串的最快方法是什么?

java - 通配符类型、类型删除和运行时签名 : what happens on <T extends A & B> where A and B inherit a common ancestor?

java - RabbitMQ 批量确认

java - 如何在 Spring boot 中检查嵌套对象来获取对象?

java - @RequestMapping 无法解析为类型 - 仅适用于 @Controller,不适用于 @RestController

java - 单向加密的更好方法

java - Spring 4 Websocket 支持和客户端订阅所有主题

java - Spring Boot - 在应用程序启动期间/之前运行代码的正确方法?

java - Spring - ehcache 无法正常工作