为我的插件添加CORS支持

时间:2015-01-21 09:27:48

标签: cors opendaylight

我有一个使用AD sal开发的插件 该插件暴露了许多其他API 这些api是从部署在另一个域上的Web应用程序访问的 因此,我的访问权限是跨域的 目前我正在使用jsonp进行此类访问 我想要做的是在我的opendaylight氢气上启用CORS支持 从我设法发现。我需要将我的apis添加到cors-config.xml 但那没用 我也尝试在插件web xml中定义过滤器,但是再次没有成功,是否有人为了让这个工作变得有效?

1 个答案:

答案 0 :(得分:0)

经过长时间的搜索,找到了答案。 如果你想在氢释放上启用cors支持: 在ODL Web容器(Tomcat)上启用CORS: 1.将以下jar复制到ODL中的插件目录     org.opendaylight.controller.filter阀-1.4.2-SNAPSHOT.jar donload链接:                 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的过滤器定义。在这里,您可以像定义restconf api一样定义路径并添加CORS过滤器。
  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>