Vou abrir o mesmo tópico que este https://cursos.alura.com.br/forum/topico-angular-e-servidor-57845.
Devo ter feito algo de errado, pois voltou a dar o erro.
Criei esta classe:
package br.com.netsoft.configuracao.auth;
import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;
@Component
public class CrossOriginFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(HttpServletRequest request,
HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
final String origin = "http://localhost:4200";
response.addHeader("Access-Control-Allow-Origin", origin);
response.setHeader("Access-Control-Allow-Methods",
"POST, PUT, GET, DELETE, OPTIONS");
response.setHeader("Access-Control-Allow-Credentials", "true");
response.setHeader(
"Access-Control-Allow-Headers",
"Authorization, Origin, X-Requested-With, Content-Type, Accept,"
+ "x-gwt-module-base, x-gwt-permutation, clientid, longpush");
filterChain.doFilter(request, response);
}
}
Na configuração do spring
@Bean
public FilterRegistrationBean corsFilterRegistration() {
FilterRegistrationBean registrationBean = new FilterRegistrationBean(
new CrossOriginFilter());
registrationBean.setName("CORS Filter");
registrationBean.addUrlPatterns("/*");
registrationBean.setOrder(1);
return registrationBean;
}
Só que não funciona, sempre dá erro 401, o que pode ser ?