相关疑难解决方法(0)

Spring Boot的CORS问题

我有一个在端口8443上运行的Spring Boot应用程序,在端口8080上有一个基于angular2的前端.我需要我的前端向我的Spring服务器发出请求,但是我左右收到CORS错误.我已将@CrossOrigin注释添加到我的RestController方法中,并且已将CORSFilter添加到我的项目中,并将其映射到web.xml,但在Firefox 46.0a2 上,我仍然在控制台上收到此错误:

跨源请求已阻止:同源策略禁止在https:// localhost:8443/allEquips读取远程资源.(原因:缺少CORS标题'Access-Control-Allow-Origin').

我的控制器的相关部分:

@CrossOrigin
@RequestMapping("/allequips")
List<String> allequips(Model model) {
    List<String> codes = equipmentRepository.findAllEquipments();
    return codes;
}
Run Code Online (Sandbox Code Playgroud)

CORSFilter:

public class CORSFilter implements Filter{
     public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
            HttpServletResponse response = (HttpServletResponse) res;
            response.setHeader("Access-Control-Allow-Origin", "*");
            response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
            response.setHeader("Access-Control-Max-Age", "3600");
            response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
            chain.doFilter(req, res);
        }
        public void init(FilterConfig filterConfig) {}
        public void destroy() {}
}
Run Code Online (Sandbox Code Playgroud)

映射web.xml:

  <filter>
  <filter-name>cors</filter-name> …
Run Code Online (Sandbox Code Playgroud)

firefox spring cors spring-boot angular

5
推荐指数
2
解决办法
4805
查看次数

标签 统计

angular ×1

cors ×1

firefox ×1

spring ×1

spring-boot ×1