estou tentando conectar a tela de login feito em react CRA quando eu envio a requisicao está apresentando erro de cors ao trazer a resposta`` Filtro
package br.com.IBMsystem.IBMsystem.Security;
import br.com.IBMsystem.IBMsystem.Repository.UsuarioRepository;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.web.filter.OncePerRequestFilter;
import java.io.IOException;
@Configuration
public class SecurityFilter extends OncePerRequestFilter {
@Autowired
private UsuarioRepository usuarioRepository;
@Autowired
private GerandoToken gerandoToken;
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
var token = pegarToken(request);
System.out.println("token"+token);
System.out.println(request);
if(token!=null){
var auth = gerandoToken.convertendoConde(token);
System.out.println(auth);
var result = usuarioRepository.findByEmail(auth);
var aut = new UsernamePasswordAuthenticationToken(result, null, result.getAuthorities());
SecurityContextHolder.getContext().setAuthentication(aut);
}
System.out.println("erro");
filterChain.doFilter(request, response);
}
private String pegarToken(HttpServletRequest request) {
var authoraze = request.getHeader("Authorization");
if(authoraze!=null){
return authoraze.replace("Bearer","").trim();
}
return null;
}
}
classe de consfiguracao do spring security
classe de consfiguracao do spring security
package br.com.IBMsystem.IBMsystem.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;
@EnableWebSecurity
@Configuration
public class SecurityConfigurations {
@Autowired
private SecurityFilter securityFilter;
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http.csrf(e->e.disable())
.sessionManagement(e->e.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.authorizeHttpRequests(e->e.requestMatchers(HttpMethod.POST,"/login").permitAll()
.anyRequest().authenticated()
)
.addFilterBefore(securityFilter,UsernamePasswordAuthenticationFilter.class);
return http.build();
}
@Bean
public AuthenticationManager authenticationManager(AuthenticationConfiguration configuration) throws Exception {
return configuration.getAuthenticationManager();
}
@Bean
public PasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}
}
class de configuracao de cors
@Configuration
public class CorsConfigurations implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("http://localhost:3000")
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS", "HEAD", "TRACE", "CONNECT");
}
}
alguem pode me da essa força ,versao do spring boot3.1.5