Предупреждение безопасности Spring: как исправить конфигурацию AuthenticationProvider и UserDetailsService?JAVA

Программисты JAVA общаются здесь
Anonymous
Предупреждение безопасности Spring: как исправить конфигурацию AuthenticationProvider и UserDetailsService?

Сообщение Anonymous »

Я получаю предупреждение при запуске приложения Spring Boot с конфигурацией Spring Security. Предупреждающее сообщение:

Код: Выделить всё

WARN [  restartedMain] r$InitializeUserDetailsManagerConfigurer : Global AuthenticationManager configured with an AuthenticationProvider bean. UserDetailsService beans will not be used for username/password login. Consider removing the AuthenticationProvider bean. Alternatively, consider using the UserDetailsService in a manually instantiated DaoAuthenticationProvider.
Я настроил аутентификацию на основе JWT в своем приложении Spring Boot. Вот соответствующие части моей конфигурации:
SecurityFilterChain:

Код: Выделить всё

@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.csrf(AbstractHttpConfigurer::disable)
.authorizeHttpRequests(authorize ->
authorize
.requestMatchers("/assets/**", "/css/**", "/images/**", "/js/**").permitAll()
.requestMatchers("/", "/about", "/contact").permitAll()
.requestMatchers("/auth/**").permitAll()
.anyRequest().authenticated()
)
.sessionManagement(session -> session.sessionCreationPolicy(STATELESS))
.authenticationProvider(authenticationProvider)
.addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);

return http.build();
}
Соответствующая часть ApplicationBeanConfiguration:

Код: Выделить всё

@Configuration
public class ApplicationBeanConfiguration {

private final UserRepository userRepository;

public ApplicationBeanConfiguration(UserRepository userRepository) {
this.userRepository = userRepository;
}

@Bean
public UserDetailsService userDetailsService() {
return username -> userRepository.findByUsername(username)
.orElseThrow(() -> new UsernameNotFoundException("User not found"));
}

@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}

@Bean
public AuthenticationManager authenticationManager(AuthenticationConfiguration config) throws Exception {
return config.getAuthenticationManager();
}

@Bean
public AuthenticationProvider authenticationProvider() {
DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();

authProvider.setUserDetailsService(userDetailsService());
authProvider.setPasswordEncoder(passwordEncoder());

return authProvider;
}
}
Предупреждение сохраняется независимо от того, включаю или исключаю компонент AuthenticationProvider. Приложение запускается с предупреждением, указывающим на то, что конфигурация AuthenticationProvider может конфликтовать с UserDetailsService.
Предупреждение предполагает, что моя установка может быть настроена неправильно. В частности, это указывает на то, что компонент AuthenticationProvider переопределяет UserDetailsService, что может привести к проблемам с аутентификацией по имени пользователя и паролю.
Как мне правильно настроить Spring Security, чтобы избежать этого предупреждения?

Подробнее здесь: https://stackoverflow.com/questions/787 ... ailsservic

Вернуться в «JAVA»