gpt4 book ai didi

spring - 使用 AuthenticationManagerBuilder 添加自定义提供程序时如何获取 AuthenticationManager?

转载 作者:行者123 更新时间:2023-12-04 03:00:48 28 4
gpt4 key购买 nike

我正在使用 Spring Boot 2.0(利用 Spring Security 5.0)。我正在尝试向 WebSecurityConfigurerAdapter 子类中的 AuthenticationManager 添加自定义 AuthenticationProvider 。如果我覆盖 configure(AuthenticationManagerBuilder)方法来提供我的新提供程序,然后我不知道如何将 AuthenticationManager 作为 bean 检索。

前任:

public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception
{
auth.authenticationProvider(customAuthenticationProvider);
}
}

哪里 customAuthenticationProvider工具 AuthenticationProvider .

Spring docs ,似乎说明两者不兼容:

5.8.4 AuthenticationProvider You can define custom authentication by exposing a custom AuthenticationProvider as a bean. For example, the following will customize authentication assuming that SpringAuthenticationProvider implements AuthenticationProvider:

[Note] This is only used if the AuthenticationManagerBuilder has not been populated



事实上,如果我尝试使用以下方法检索 AuthenticationManager bean:
@Bean
public AuthenticationManager authenticationManager() throws Exception {
return super.authenticationManagerBean();
}

然后是 configure()方法甚至从未被调用。

那么如何将我自己的自定义提供程序添加到提供程序的默认列表中,并且仍然能够检索 AuthenticationManager?

最佳答案

您可以覆盖 WebSecurityConfigurerAdapter.authenticationManagerBean()方法并用 @Bean 注释它注解

 @Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}

并且没有 Spring Boot 5.0,最新版本是 Spring Boot 2.0。我相信你在谈论 Spring Security 5.0。

关于spring - 使用 AuthenticationManagerBuilder 添加自定义提供程序时如何获取 AuthenticationManager?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49410977/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com