为我的插件添加 CORS 支持

Adding CORS support for my Plugin

我有一个使用 AD sal 开发的插件。
该插件公开了一些剩余的API。
这些 api 是从部署在另一个域上的 Web 应用程序访问的。
因此我的访问是跨域的。
目前我正在使用 jsonp 进行这种访问。
我想做的是在我的 opendaylight hydrogen 上启用 CORS 支持。
从我设法发现的。我需要将我的 api 添加到 cors-config.xml.
但这没有用。
我也尝试在插件网络中定义过滤器 xml 但同样没有成功 有人设法让它工作吗?

经过长时间的搜索,找到了答案。 如果你想在 Hydrogen 版本上启用 cors 支持: 在 ODL Web 容器 (Tomcat) 上启用 CORS: 1.将以下jar复制到ODL中的插件目录 org.opendaylight.controller.filter-阀门-1.4.2-SNAPSHOT.jar 下载 link: https://nexus.opendaylight.org/service/local/repositories/opendaylight.snapshot/content/org/opendaylight/controller/filter-valve/1.4.2-SNAPSHOT/filter-valve-1.4.2-20141001.225558-591.jar

  1. 转到 ./configuration/tomcat-server.xml
  2. 在文件中添加以下标记行:

    <Engine name="Catalina" defaultHost="localhost">
        <Host name="localhost" appBase="" unpackWARs="false" autoDeploy="false" deployOnStartup="false" createDirs="false">
        <Realm className="org.opendaylight.controller.security.ControllerCustomRealm" />
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
                        prefix="web_access_log_" suffix=".txt" resolveHosts="false"
                        rotatable="true" fileDateFormat="yyyy-MM"
                        pattern="%{yyyy-MM-dd HH:mm:ss.SSS z}t - [%a] - %r"/>
    
        <!--add this line!-->
        <Valve className="org.opendaylight.controller.filtervalve.cors.FilterValve" configurationFile="configuration/cors-config.xml"/>
    
      </Host>
    </Engine>
    
  3. 在 ODL 下的配置目录中创建 cors-config.xml 文件。该文件包含 tomcat 的过滤器定义。在这里你可以定义你的路径并向其添加 CORS 过滤器,就像对 restconf api.
  4. 所做的那样
<Host>
  <!-- Filters are allowed here, only serving as a template -->
  <filter-template>
    <filter-name>CorsFilter</filter-name>
    <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
    <init-param>
      <param-name>cors.allowed.origins</param-name>
      <param-value>*</param-value>
    </init-param>
    <init-param>
      <param-name>cors.allowed.methods</param-name>
      <param-value>GET,POST,HEAD,OPTIONS,PUT,DELETE</param-value>
    </init-param>
    <init-param>
      <param-name>cors.allowed.headers</param-name>
      <param-value>Content-Type,X-Requested-With,accept,authorization,
        origin,Origin,Access-Control-Request-Method,Access-Control-Request-Headers
      </param-value>
    </init-param>
    <init-param>
      <param-name>cors.exposed.headers</param-name>
      <param-value>Access-Control-Allow-Origin,Access-Control-Allow-Credentials</param-value>
    </init-param>
    <init-param>
      <param-name>cors.support.credentials</param-name>
      <param-value>true</param-value>
    </init-param>
    <init-param>
      <param-name>cors.preflight.maxage</param-name>
      <param-value>10</param-value>
    </init-param>
  </filter-template>

  <Context path="/restconf">
    <filter>
      <filter-name>CorsFilter</filter-name>
      <!-- init params can be added/overriden if template is used> -->
    </filter>
    <!-- references to templates without <filter> declaration are not allowed -->
    <filter-mapping>
      <filter-name>CorsFilter</filter-name>
      <url-pattern>/*</url-pattern>
    </filter-mapping>
  </Context>




</Host>