解决No 'Access-Control-Allow-Origin' header is present on the requested resource.

一般而言是跨域导致

import org.springframework.context.annotation.Configuration;

import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import java.io.IOException;

@Configuration

public class CorsFilterextends OncePerRequestFilter {

@Override

    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)throws ServletException, IOException {

// Access-Control-Allow-Origin: 指定授权访问的域

        response.addHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));  //此优先级高于@CrossOrigin配置,此处应该与前端配置的一致

//        response.addHeader("Access-Control-Allow-Headers", "Origin, x-requested-with, Content-Type, Accept,X-Cookie");

        response.addHeader("Access-Control-Allow-Headers", request.getHeader("Access-Control-Request-Headers"));//

        response.setHeader("Access-Control-Allow-Credentials", "true");

        // Access-Control-Allow-Methods: 授权请求的方法(GET, POST, PUT, DELETE,OPTIONS等)

        response.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE,OPTIONS");

        response.addHeader("Access-Control-Max-Age", "1800");//30 min

        filterChain.doFilter(request, response);

    }

}

你可能感兴趣的:(解决No 'Access-Control-Allow-Origin' header is present on the requested resource.)