将 Access-Control-Allow-Origin 添加到 Web 服务

Add Access-Control-Allow-Origin to Web Service

我有一个像这样的网络服务:

@Controller
@RequestMapping("/")
public class ApplicationController {

    @RequestMapping(value="/Changes", method = RequestMethod.GET)
    public String inspect(ModelMap model) {
         model.addAttribute("msg", "example");

         return "index";
    }
}

在link中:"localhost:8081/ChangesPDF/Changes?..."

我正在尝试通过 link 中的 Alfresco 获取此网络服务的响应:"localhost:8080/share"。我现在有不同的端口(当我有相同的端口时,这很好用),所以,使用不同的端口我在 Alfresco 中得到错误:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:8081/ChangesPDF/Changes?... (Reason: Header CORS 'Access-Control-Allow-Origin' missing).

如何添加这个 header?

您应该添加一个过滤器,将 "Access-Control-Allow-Origin" 设置为接受域 "localhost:8081"(* 代表所有)。

很可能您会在这里找到答案cores-filter-not-working

更多说明:

先创建一个过滤器class

public class CorsFilter implements Filter {

    private static final Logger log = Logger.getAnonymousLogger();

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {}

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        HttpServletRequest request = (HttpServletRequest) servletRequest;

        // can be moved to properties
        String[] allowDomain = {"localhost:8080","localhost:8081"};              


        String originHeader = request.getHeader("host");

        for(String domian : allowDomain){

            if(originHeader.endsWith(domian))

            response.setHeader("Access-Control-Allow-Origin", originHeader);
            break;
        }
        filterChain.doFilter(servletRequest, servletResponse);

    }

    @Override
    public void destroy() {}
}

将映射添加到您的 web.xml class

<filter>
    <filter-name>cors</filter-name>
    <filter-class>full name of your filter class here</filter-class>
</filter>

<filter-mapping>
    <filter-name>cors</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

你应该根据你的要求在web.xml配置中正确定义URL模式

如果您使用的是 Spring 4.2+,则可以使用 @CrossOrigin 注释:

@Controller
@RequestMapping("/")
public class ApplicationController {

    @CrossOrigin
    @RequestMapping(value="/Changes", method = RequestMethod.GET)
    public String inspect(ModelMap model) {
         model.addAttribute("msg", "example");

         return "index";
    }
}

否则,您需要在 Spring 应用程序中注册 CORS 过滤器。类似于 this.

我认为您的代码可能需要使用 Spring 的 @CrossOrigin 注释,例如:

import org.springframework.web.bind.annotation.CrossOrigin;

....

@Controller
@RequestMapping("/")
public class ApplicationController {

    @CrossOrigin
    @RequestMapping(value="/Changes", method = RequestMethod.GET)
    public String inspect(ModelMap model) {
        model.addAttribute("msg", "example");

        return "index";
    }
}

这将允许 所有 个来源,这可能会或可能不会满足您的需求。

作为参考,我在this article, and mentioned in the Spring blog here.

中找到了以上描述

希望对您有所帮助!