Boa noite pessoal, estou tentando implementar a lógica aprendida no Curso em uma outra aplicação a fim de reforçar os conteudos aprendidos, contudo, estou tendo um erro que não estou sabendo resolver:
Field authenticationManager in edu.corp.api.domain.controllers.AutenticacaoController 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)
- @org.springframework.beans.factory.annotation.Qualifier("authenticationManagerBean")
Action:
Consider defining a bean of type 'org.springframework.security.authentication.AuthenticationManager' in your configuration.
Process finished with exit code 0
package edu.corp.api.utils.infra.security;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
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.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
@Configuration
@EnableWebSecurity
public class securityConfiguration{
@Autowired
private securityFilter securityFilter;
@Autowired
@Qualifier("authenticationManagerBean")
private AuthenticationManager authenticationManagerBean;
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
return http
.csrf(csrf -> csrf.disable())
.sessionManagement(sm -> sm.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.authorizeRequests(authorizeRequests ->
authorizeRequests
.requestMatchers(HttpMethod.POST, "/login").permitAll()
.requestMatchers("/v3/api-docs/**", "/swagger-ui.html", "/swagger-ui/**").authenticated()
.anyRequest().authenticated()
)
.addFilterBefore(securityFilter, UsernamePasswordAuthenticationFilter.class)
.build();
}
}
AutenticacaoController:
package edu.corp.api.domain.controllers;
import edu.corp.api.domain.dtos.autenticacao.LoginDTO;
import edu.corp.api.domain.models.Usuario;
import edu.corp.api.utils.infra.security.TokenJWT;
import edu.corp.api.utils.infra.security.TokenService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class AutenticacaoController {
@Autowired
@Qualifier("authenticationManagerBean")
private AuthenticationManager authenticationManager;
// Classe para disparar o servico de autenticacao
@Autowired
private TokenService tokenService;
@PostMapping
public ResponseEntity efetuarLogin(@RequestBody @Valid LoginDTO login){
try{
// DTO do Spring Security para dados de authenticacao
var authenticationToken = new UsernamePasswordAuthenticationToken(login.email(), login.senha());
var authentication = authenticationManager.authenticate(authenticationToken);
var tokenJWT = tokenService.generateToken((Usuario) authentication.getPrincipal());
return ResponseEntity.ok(new TokenJWT(tokenJWT));
} catch (Exception e){
return ResponseEntity.badRequest().body(e.getMessage());
}
}
}