Angular and server

1

I must have done something wrong, because it gave the error again.

I created this class:

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);
    }
}

In the spring configuration

  

@Bean public FilterRegistrationBean corsFilterRegistration () {         FilterRegistrationBean registrationBean = new   FilterRegistrationBean (                 new CrossOriginFilter ()); registrationBean.setName ("CORS Filter"); registrationBean.addUrlPatterns ("/ *");         registrationBean.setOrder (1); return registrationBean; }

It just does not work, it always gives 401 error, what can it be?

Has anyone gone through this?

Failed to load link : Response to preflight request does not pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' link ' is therefore not allowed access. The response had HTTP status 401.

Accessing link , director works.

One more breakthrough:

I think the problem is here:

String requestHeader = request.getHeader ("Authorization");

If you fill in the requestHeader variable with a valid token, it works.

Debugging

Browser

Server

Youarenotsendingyourbrowser

    
asked by anonymous 02.04.2018 / 15:53

0 answers