Для поля authenticManager LoginController требуется компонент типа AuthenticationManager, который не может быть найден - PullRequest
0 голосов
/ 24 апреля 2020

Получение этой ошибки при попытке настроить мой SecurityConfigurer с помощью Spring Security.

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.

Я переопределил аутентификациюManagerBean в соответствии с другими ответами, однако это не помогло. Любой совет приветствуется. Мой класс 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();
    }
}

LoginController

@RestController
public class LoginController {

    @Autowired
    private AuthenticationManager authenticationManager;

1 Ответ

0 голосов
/ 24 апреля 2020

Глядя на код, кажется, вы пытаетесь автоматически подключить SecurityConfigurer в LoginController. Но в весенней безопасности нет необходимости автоматически подключать класс configurer. Кроме того, как вы упомянули, переопределение authenticationManagerBean предоставит вам только компонент типа AuthenticationManager, а не тип SecurityConfigurer.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...