в моем приложении Spring Boot я хочу добавить второй поставщик аутентификации, так что пользователи аутентифицируют любого
- , так что пользователи являются аутентификацией
Код: Выделить всё
DaoAuthenticationProvider Код: Выделить всё
TwoFactorAuthenticationProvider
Код: Выделить всё
@Configuration
@EnableWebSecurity
@EnableMethodSecurity
public class SecurityConfiguration {
@Bean
public AuthenticationManager authenticationManager(UserDetailsService userDetailsService,
PasswordEncoder passwordEncoder,
HttpSecurity http) throws Exception {
var singleFactorProvider = new DaoAuthenticationProvider(passwordEncoder);
singleFactorProvider.setUserDetailsService(userDetailsService);
var twoFactorProvider = new TwoFactorAuthenticationProvider(passwordEncoder, userDetailsService);
return http.getSharedObject(AuthenticationManagerBuilder.class)
.authenticationProvider(singleFactorProvider)
.authenticationProvider(twoFactorProvider)
.build();
}
@Bean
public SecurityFilterChain configure(AuthenticationManager authenticationManager,
HttpSecurity http) throws Exception {
// The real class has additional configuration, but I've omitted it as it's
// not relevant to the question
return http.authenticationManager(authenticationManager).build()
}
}
< /code>
Когда я пытаюсь получить ссылку на этот диспетчер аутентификации, я получаю реализацию по умолчанию. Я не могу в зависимости от инъекции аутентификации manager - инъекция аутентификацииконфигурации и вызов аутентификации Managemanager
- getObject ()
Код: Выделить всё
class MyClass implements BeanFactoryAware {
private BeanFactory beanFactory;
public Authentication authenticate(Authentication authRequest) {
// this returns my custom AuthenticationManager
var authenticationManager = beanFactory.getBean(AuthenticationManager.class);
return authenticationManager.authenticate(authRequest);
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = beanFactory;
}
}
Подробнее здесь: https://stackoverflow.com/questions/795 ... urity-boot