简体   繁体   English

ColdFusion REST API 启用 CORS

[英]ColdFusion REST API Enable CORS

I am trying to build a REST API using coldfusion components and I can't figure out how to enable CORS.我正在尝试使用 Coldfusion 组件构建 REST API,但我不知道如何启用 CORS。 I am using IIS 10 and ColdFusion 2016. I can't find anywhere in IIS to configure CORS following instructions on google and I don't see anywhere under CF admin settings to enable CORS, so I figured I would try to enable CORS in my API instead of fiddling with configuration in each environment I deploy this to (qa, uat, prod).我正在使用 IIS 10 和 ColdFusion 2016。我在 IIS 中找不到任何地方可以按照谷歌上的说明配置 CORS,而且我在 CF 管理设置下看不到任何地方来启用 CORS,所以我想我会尝试在我的API 而不是摆弄每个环境中的配置,我将它部署到(qa、uat、prod)。

This is what my application.cfc currently looks like and you can see in the onRequestStart I am attempting to set the headers (two ways I've tried)这是我的 application.cfc 目前的样子,您可以在 onRequestStart 中看到我正在尝试设置标头(我尝试过两种方法)

<cfscript>
    component output="false" {
        this.name = ....


        public boolean function onApplicationStart() {\
            restInitApplication( ... );
            return true;
        }

        public void function onApplicationEnd(ApplicationScope) {
            return;
        }

        public void function onMissingTemplate(targetPage) {
            return;
        }

        public void function onRequestStart(targetPage) {
            cfheader(name="Access-Control-Allow-Origin", value="*");
            // i've also tried ...
            GetPageContext().getResponse().addHeader("Access-Control-Allow-Origin","*");
        }

        public void function onSessionStart() {
            return;
        }

        public void function onSessionEnd(sessionScope, applicationScope) {
            return;
        }
    }
</cfscript>

I would recommend installing the IIS CORS module - reference .我建议安装 IIS CORS 模块 - 参考 Here is a snippet from that reference:这是该参考文献中的一个片段:

Functionality Overview功能概述
The Microsoft IIS CORS Module is an extension that enables web sites to support the CORS(Cross-Origin Resource Sharing) protocol. Microsoft IIS CORS 模块是使网站能够支持 CORS(跨域资源共享)协议的扩展。

The IIS CORS module provides a way for web server administrators and web site authors to make their applications support the CORS protocol. IIS CORS 模块为 Web 服务器管理员和网站作者提供了一种使他们的应用程序支持 CORS 协议的方法。 With this module, developers can move CORS logic out of their applications and rely on the web server.使用此模块,开发人员可以将 CORS 逻辑移出他们的应用程序并依赖于 Web 服务器。 The module's handling of CORS requests is determined by rules defined in the configuration.模块对 CORS 请求的处理由配置中定义的规则决定。 These CORS rules can be easily defined or configured making it simple to delegate all CORS protocol handling to the module.这些 CORS 规则可以轻松定义或配置,从而可以轻松将所有 CORS 协议处理委托给模块。

IIS CORS module is a server-side CORS component IIS CORS 模块是服务器端的 CORS 组件
The CORS protocol governs client/server communication. CORS 协议管理客户端/服务器通信。 Usually, web browsers act as the client-side CORS component, while the IIS server works as the server-side CORS component with the help of the IIS CORS module.通常,Web 浏览器充当客户端 CORS 组件,而 IIS 服务器在 IIS CORS 模块的帮助下充当服务器端 CORS 组件。

A CORS request occurs when a protocol aware client, such as a web browser, makes a request to a domain (origin) that differs from the current domain.当协议感知客户端(例如 Web 浏览器)向与当前域不同的域(源)发出请求时,就会发生 CORS 请求。 This scenario is known as a cross-origin request.这种情况称为跨域请求。 When CORS is not used, cross-origin requests will be blocked by the client.当不使用 CORS 时,跨域请求将被客户端阻止。 When the CORS module is used, IIS will inform clients whether a cross-origin request can be performed based on the IIS configuration.使用CORS模块时,IIS会根据IIS配置通知客户端是否可以进行跨域请求。

Don't try implementing this from ColdFusion, let the web server do what it is designed to do.不要尝试从 ColdFusion 实现这一点,让 Web 服务器做它设计要做的事情。 Once you have the module installed you can create the rules you want within the web.config files for any/all IIS sites.安装模块后,您可以在web.config文件中为任何/所有 IIS 站点创建所需的规则。

Sample config file:示例配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
    <cors enabled="true" failUnlistedOrigins="true">
        <add origin="*" />
        <add origin="https://*.microsoft.com"
             allowCredentials="true"
             maxAge="120"> 
            <allowHeaders allowAllRequestedHeaders="true">
                <add header="header1" />
                <add header="header2" />
            </allowHeaders>
            <allowMethods>
                 <add method="DELETE" />
            </allowMethods>
            <exposeHeaders>
                <add header="header1" />
                <add header="header2" />
            </exposeHeaders>
        </add>
        <add origin="http://*" allowed="false" />
    </cors>
</system.webServer>
</configuration>

You can download the IIS CORS module from here.您可以从这里下载 IIS CORS 模块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM