Olá, fiz tudo como foi pedido na aula, porém recebo o erro 403 quando disparo o postman
@Configuration
@EnableWebSecurity
public class SecurityConfigurations {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
return http.csrf(csrf -> csrf.ignoringRequestMatchers(toH2Console()).disable()).authorizeHttpRequests(auth -> {
auth.requestMatchers(toH2Console()).permitAll();
auth.requestMatchers("/login").permitAll();
}).sessionManagement(sm -> sm.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.headers(headers -> headers.frameOptions(FrameOptionsConfig::disable)).build();
}
@Bean
public AuthenticationManager authenticationManager(AuthenticationConfiguration configuration) throws Exception {
return configuration.getAuthenticationManager();
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
@RestController
@RequestMapping("/login")
public class AutenticacaoController {
@Autowired
private AuthenticationManager manager;
@PostMapping
public ResponseEntity efetuarLogin(@RequestBody DadosAutenticacao dados) {
var token = new UsernamePasswordAuthenticationToken(dados.login(), dados.senha());
var authentication = manager.authenticate(token);
return ResponseEntity.ok().build();
}
}
@Service
public class AutenticacaoService implements UserDetailsService{
@Autowired
private UsuarioRepository repository;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
return repository.findByLogin(username);
}
}
SELECT * FROM USUARIOS ;
ID LOGIN SENHA
1 ekan@teste.com 123456
2 ekan@teste.com $2a$10$Y50UaMFOxteibQEYLrwuHeehHYfcoafCopUazP12.rqB41bsolF5.
3 ekan@teste.com $2a$12$PunpMrviBEOTdH25mfR6ye5nY7ic/bVTySrJyrx4mTtGVHv1Z27/q
(3 rows, 0 ms)