请求的资源上不存在“Access-Control-Allow-Origin”标头. Ionic,AngularJS,Spring Boot 1.3

前端之家收集整理的这篇文章主要介绍了请求的资源上不存在“Access-Control-Allow-Origin”标头. Ionic,AngularJS,Spring Boot 1.3前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用Ionic和 Spring Boot 1.3.直到我升级到1.3才得到这个问题……

显然在更新到Spring Boot 1.3之后. CorsFilter完全被忽略了.所有这些弃用让我疯狂.所以我抬起了新的方式,这就是我得到的.

package app.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("http://192.168.1.66:8101")
                .allowCredentials(false)
                .maxAge(3600)
                .allowedHeaders("Accept","Content-Type","Origin","Authorization","X-Auth-Token")
                .exposedHeaders("X-Auth-Token","Authorization")
                .allowedMethods("POST","GET","DELETE","PUT","OPTIONS");
    }
}

上面的代码在应用程序的启动时被执行.与每次有请求时执行的CorsFilter不同.但是切换到Spring Boot 1.3,我不能再在链接过滤器中得到这个.

再次,代码正在加载,我设置了一个断点,每次调用addCorsMapping,所以进行设置.所以….为什么我仍然会收到此错误

XMLHttpRequest cannot load http://192.168.1.66:8080/login?username=billyjoe&password=happy. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://192.168.1.66:8101' is therefore not allowed access.

编辑
下面是我的旧CorsFilter.自从我更新到Spring Boot 1.3后它就不再有用了

package app.config;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class CorsFilter implements Filter {

    private final Logger log = LoggerFactory.getLogger(CorsFilter.class);

    public CorsFilter() {
        log.info("SimpleCORSFilter init");
    }

    @Override
    public void doFilter(ServletRequest req,ServletResponse res,FilterChain chain) throws IOException,ServletException {

        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;
        String clientOrigin = request.getHeader("origin");
        response.addHeader("Access-Control-Allow-Origin",clientOrigin);
        response.setHeader("Access-Control-Allow-Methods","POST,GET,DELETE,PUT");
        response.setHeader("Access-Control-Allow-Credentials","true");
        response.setHeader("Access-Control-Max-Age","3600");
        response.setHeader("Access-Control-Allow-Headers","Accept,Content-Type,Origin,Authorization,X-Auth-Token");
        response.addHeader("Access-Control-Expose-Headers","X-Auth-Token");

        if (request.getMethod().equals("OPTIONS")) {
            response.setStatus(HttpServletResponse.SC_OK);
        } else {
            chain.doFilter(request,response);
        }
    }

    @Override
    public void init(FilterConfig filterConfig) {
    }

    @Override
    public void destroy() {
    }

}
你可以尝试这样的事情.它对我有用:
@Component
public class SimpleCORSFilter implements Filter {

    public void doFilter(ServletRequest req,ServletException {
        HttpServletResponse response = (HttpServletResponse) res;
        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Access-Control-Allow-Methods",PUT,OPTIONS,PATCH");
        response.setHeader("Access-Control-Max-Age","Origin,X-Requested-With,Accept");
        chain.doFilter(req,res);
    }

    public void init(FilterConfig filterConfig) {}

    public void destroy() {}

}

猜你在找的Angularjs相关文章