Ошибка создания бина с именем authenticationManagerBean - PullRequest
0 голосов
/ 14 сентября 2018

Я получаю сообщение об ошибке при создании компонента с именем authenticationManagerBean: запрашиваемый компонент находится в процессе создания: существует неразрешимая циклическая ссылка?

Что я делаю плохо?

@EnableResourceServer
@Configuration
public class ResourceServerConfig extends WebSecurityConfigurerAdapter {

    private final AuthenticationManager authenticationManager;

    @Autowired
    public ResourceServerConfig(AuthenticationManager authenticationManager) {
        this.authenticationManager = authenticationManager;
    }

    @Override
    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.requestMatchers()
                .antMatchers( "/login","/oauth/authorize")
                .and()
                .authorizeRequests()
                .anyRequest()
                .authenticated()
                .and()
                .formLogin()
                .permitAll();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.parentAuthenticationManager(authenticationManager)
                .inMemoryAuthentication()
                .withUser("Admin")
                .password("admin")
                .roles("ADMIN");
    }
}

1 Ответ

0 голосов
/ 14 сентября 2018

Если у вас нет родителя AuthenticationManager, оно должно быть:

@EnableResourceServer
@Configuration
public class ResourceServerConfig extends WebSecurityConfigurerAdapter {

    @Override
    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.requestMatchers()
                .antMatchers( "/login","/oauth/authorize")
                .and()
                .authorizeRequests()
                .anyRequest()
                .authenticated()
                .and()
                .formLogin()
                .permitAll();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
                .withUser("Admin")
                .password("admin")
                .roles("ADMIN");
    }
}

И если вы это сделаете, то вам придется либо переименовать родительский , либо назвать это child Bean что-то еще:

@EnableResourceServer
@Configuration
public class ResourceServerConfig extends WebSecurityConfigurerAdapter {

    // Option 1) rename the parent somewhere else, like so....
    private final AuthenticationManager parentAuthenticationManager;

    @Autowired
    public ResourceServerConfig(AuthenticationManager parentAuthenticationManager) {
        this.parentAuthenticationManager = parentAuthenticationManager;
    }

    @Override
    // Option 2) rename the child AuthenticationManager here
    @Bean("childAuthenticationManager")
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.requestMatchers()
                .antMatchers( "/login","/oauth/authorize")
                .and()
                .authorizeRequests()
                .anyRequest()
                .authenticated()
                .and()
                .formLogin()
                .permitAll();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.parentAuthenticationManager(authenticationManager)
                .inMemoryAuthentication()
                .withUser("Admin")
                .password("admin")
                .roles("ADMIN");
    }
}
...