Estou utilizando os conhecimentos adquiridos no curso para programar uma aplicação Full Stack e estou enfrentando a seguinte dificuldade: Ao tentar efetuar o login de um usuário devidamente salvo no banco, retorna o token, mas informa que o usuário é nulo. Inseri 'System.out.println' em 'token', 'login' e 'user' para facilitar o rastreamento. Segue a mensagem de erro:
. Meu arquivo SecurityConfigurations: `package com.minascafe.api.infra.security;
import org.springframework.beans.factory.annotation.Autowired; 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.configuration.AuthenticationConfiguration; 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.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.web.SecurityFilterChain; import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
@Configuration @EnableWebSecurity public class SecurityConfigurations {
@Autowired
SecurityFilter securityFilter;
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Exception {
return httpSecurity
.csrf(csrf -> csrf.disable())
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.cors()
.and()
.authorizeHttpRequests(authorize -> authorize
.requestMatchers(HttpMethod.POST, "/auth/login").permitAll()
.requestMatchers(HttpMethod.OPTIONS, "/auth/login/**").permitAll()
.requestMatchers(HttpMethod.POST, "/auth/register").hasRole("ADMIN")
.requestMatchers(HttpMethod.OPTIONS, "/auth/register").permitAll()
.requestMatchers(HttpMethod.OPTIONS, "/cafecoco/**").permitAll()
.requestMatchers(HttpMethod.OPTIONS, "/cafemaquina/**").permitAll()
.requestMatchers(HttpMethod.OPTIONS, "/cafebeneficiado/**").permitAll()
.requestMatchers(HttpMethod.OPTIONS, "/produtor/**").permitAll()
.requestMatchers(HttpMethod.POST, "/cafecoco/**").hasRole("ADMIN")
.requestMatchers(HttpMethod.POST, "/cafemaquina/**").hasRole("ADMIN")
.requestMatchers(HttpMethod.POST, "/cafebeneficiado/**").hasRole("ADMIN")
.requestMatchers(HttpMethod.POST, "/produtor/**").hasRole("ADMIN")
.requestMatchers(HttpMethod.GET, "/cafecoco/**").authenticated()
.requestMatchers(HttpMethod.GET, "/cafemaquina/**").authenticated()
.requestMatchers(HttpMethod.GET, "/cafebeneficiado/**").authenticated()
.requestMatchers(HttpMethod.GET, "/produtor/**").authenticated()
.requestMatchers(HttpMethod.PUT, "/cafecoco/**").hasRole("ADMIN")
.requestMatchers(HttpMethod.PUT, "/cafemaquina/**").hasRole("ADMIN")
.requestMatchers(HttpMethod.PUT, "/cafebeneficiado/**").hasRole("ADMIN")
.requestMatchers(HttpMethod.PUT, "/produtor/**").hasRole("ADMIN")
.requestMatchers(HttpMethod.DELETE, "/cafecoco/**").hasRole("ADMIN")
.requestMatchers(HttpMethod.DELETE, "/cafemaquina/**").hasRole("ADMIN")
.requestMatchers(HttpMethod.DELETE, "/cafebeneficiado/**").hasRole("ADMIN")
.requestMatchers(HttpMethod.DELETE, "/produtor/**").hasRole("ADMIN")
.anyRequest().authenticated()
)
.addFilterBefore(securityFilter, UsernamePasswordAuthenticationFilter.class)
.build();
}
@Bean
public AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration)
throws Exception {
return authenticationConfiguration.getAuthenticationManager();
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}`.