2015-01-21 63 views
0

我有一个使用AD sal开发的插件。
该插件公开了一些其余的API。
这些API是从部署在另一个域上的Web应用程序访问的。
因为这个我的访问是跨域。
目前我使用jsonp进行这种访问。
我想要做的是在我的opendaylight氢上启用CORS支持。
从我设法发现。我需要将我的apis添加到cors-config.xml。
但没有奏效。
我也尝试在插件web xml中定义过滤器,但再次没有任何成功的任何人maged得到这个工作?为我的插件添加CORS支持

回答

0

经过漫长的搜索后找到了答案。 如果您希望启用氢释放支持cors: 启用ODL Web容器(Tomcat)上的CORS: 1.将以下jar添加到ODL的插件目录中 org.opendaylight.controller.filter-valve -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

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

    <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> 
    
  • 在ODL下的配置目录中创建cors-config.xml文件。该文件包含tomcat的过滤器定义。在这里你可以像定义restconf api一样定义你的路径并添加CORS过滤器。
  • <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>