Bom dia, estou a umas 3 semanas em busca da solução de como fazer o Security Configuration rodar, estou utilizando um FilterChain mas toda hora que resolvo um erro me vem outros 10 diferentes, alguém poderia me ajudar por favor, recorri a varias paginas e ate as IA'S O erro é basicamente a Instancia do AuthenticationManager se eu configuro ele ou crio um Bean ele da erro quando passado para o filtro personalizado que roda antes do UsernamePasswordAuthenticationFilter Description:
Parameter 1 of method filterChain in br.com.claudiano.forum.config.SecurityConfiguration required a bean of type 'org.springframework.security.authentication.AuthenticationManager' that could not be found.
Action: Consider defining a bean of type 'org.springframework.security.authentication.AuthenticationManager' in your configuration.
Process finished with exit code 0 se eu inicio ele no construtor da class SecurityConfiguration ele da erro, poderiam me ajudar por favor package br.com.claudiano.forum.config
import br.com.claudiano.forum.security.JWTAuthenticationFilter import br.com.claudiano.forum.security.JWTLoginFilter import br.com.claudiano.forum.service.UsuarioService import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration import org.springframework.http.HttpMethod 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.EnableWebSecurity import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder import org.springframework.security.web.SecurityFilterChain import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter import org.springframework.web.filter.OncePerRequestFilter
@Configuration @EnableWebSecurity class SecurityConfiguration( private val usuarioService: UsuarioService, private val jwtUtil: JWTUtil, private val authManager: AuthenticationManager ) {
@Bean
fun filterChain(http: HttpSecurity, authManager: AuthenticationManager): SecurityFilterChain {
http.authorizeHttpRequests {it
.requestMatchers(HttpMethod.POST,"/topicos").permitAll()
?.anyRequest()
?.authenticated()
}
.addFilterBefore(JWTLoginFilter(authManager = authManager, jwtUtil = jwtUtil), UsernamePasswordAuthenticationFilter().javaClass)
.addFilterBefore(JWTAuthenticationFilter(jwtUtil = jwtUtil), OncePerRequestFilter::class.java)
return http.build()
}
// @Bean // fun authManager(http: HttpSecurity): AuthenticationManager { // val authBuilder = http.getSharedObject(AuthenticationManagerBuilder::class.java) // authBuilder.userDetailsService(usuarioService).passwordEncoder(bCryptPasswordEncoder()) // return authBuilder.build() // }
fun configure(http: HttpSecurity): AuthenticationManager {
val authBuilder = http.getSharedObject(AuthenticationManagerBuilder::class.java)
authBuilder.userDetailsService(usuarioService).passwordEncoder(bCryptPasswordEncoder())
return authBuilder.build()
}
@Bean
fun bCryptPasswordEncoder(): BCryptPasswordEncoder? {
return BCryptPasswordEncoder()
}
}