Fala pessoal,
Sou novato no java, estou com um projeto a fim de praticar/treinar e ao tentar fazer um método de autenticação com SpringBoot e JWT não estou conseguindo, atualmente ele nem deixa o projeto subir, da o erro abaixo:
APPLICATION FAILED TO START
***************************
Description:
Field authenticationManager in com.luan.config.SecurityConfig required a bean of type 'org.springframework.security.authentication.AuthenticationManager' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'org.springframework.security.authentication.AuthenticationManager' in your configuration.
estou utilizando a versão 6.0.1 do Spring Security e a versão 0.9.1 do JWT conforme pom.xml abaixo:
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
A classe que eu suspeito estar com erro é essa abaixo:
@EnableWebSecurity
@Configuration
public class SecurityConfig {
@Autowired
private JWTUtil jwtUtil;
@Autowired
private AuthenticationManager authenticationManager;
@Autowired
private UserDetailsService userDetailsService;
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.addFilter(new JWTAuthenticationFilter(authenticationManager, jwtUtil));
http.httpBasic().and().authorizeHttpRequests()
.requestMatchers(HttpMethod.POST, "/postsblog/**").permitAll()
.requestMatchers(HttpMethod.GET).permitAll()
.requestMatchers(HttpMethod.DELETE).hasRole("ADMIN")
.requestMatchers(HttpMethod.PUT).permitAll()
.anyRequest().authenticated()
.and().csrf().disable()
.cors().disable();
return http.build();
}
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedOrigins("*").allowedHeaders("*").allowedMethods("*").exposedHeaders("*");
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean(name = "authenticationManager")
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
}
}
Mais especificamente, acredito que seja algum problema no ultimo método, o AuthenticationManagerBuilder, já tentei resolver de algumas formas e não consegui, pesquisando na internet só encontrei conteudo com Spring Security antigo extendendo o WebSecurityConfigAdapter.
Se alguém puder ajudar, agradeço desde já.