zoukankan      html  css  js  c++  java
  • JS跨域访问CORS配置

    两个方法

    一、配置Filter,每个请求都拦截,设置Http Header

    Web.xml

    <filter>
        <filter-name>cros</filter-name>
        <filter-class>cn.ifengkou.test.filter.CORSFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>cros</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    Java

    public class CORSFilter extends OncePerRequestFilter {
        @Override
        protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
            response.addHeader("Access-Control-Allow-Origin", "*");
            response.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
            response.addHeader("Access-Control-Allow-Headers", "Content-Type");
            response.addHeader("Access-Control-Max-Age", "1800");//30 min
            filterChain.doFilter(request, response);
        }
    }

    二、使用Spring4.2版本以上,使用CrossOrigin注解

    @CrossOrigin(origins = "http://domain2.com", maxAge = 3600)
    @RestController
    @RequestMapping("/account")
    public class AccountController {
    
        @RequestMapping("/{id}")
        public Account retrieve(@PathVariable Long id) {
            // ...
        }
    
        @RequestMapping(method = RequestMethod.DELETE, path = "/{id}")
        public void remove(@PathVariable Long id) {
            // ...
        }
    }
    

      

  • 相关阅读:
    RPA-UiPath视频教程2
    skywalking链路监控
    RPA-UiPath视频教程1
    Redis基础与性能调优
    tomcat调优
    DDos、CC攻击与防御
    UiPath官方视频Level2
    UiPath直播课程
    UiPath手把手教程
    【0】python核心编程,第二章
  • 原文地址:https://www.cnblogs.com/kevin19900306/p/6065576.html
Copyright © 2011-2022 走看看