Angular and server

Asked

Viewed 61 times

1

I must have done something wrong.

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

Only it doesn’t work, always gives error 401, which can be ?

Someone’s been through this ?

Failed to load http://localhost:8080/des-if-web/admin/pailsRecourse/search: Response to preflight request doesn’t pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested Resource. Origin 'http://localhost:4200' is therefore not allowed access. The Response had HTTP status code 401.

Accessing http://localhost:8080/des-if-web/admin/pailsRecourse/search, director works.

Another discovery:


I think the problem is here:

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

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

Threshing

Navigator

inserir a descrição da imagem aqui

Server

inserir a descrição da imagem aqui

Is not sending the browser

inserir a descrição da imagem aqui

  • No one’s been through this ?

  • No one’s been through this ?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.