Dockerized Alfresco CORS 过滤器

Dockerized Alfresco CORS filter

我有一个 dockerized Alfresco 6.2 社区版本,暂时清理,并 运行 在我的 AWS 服务器中正确。我可以使用它登录共享。

现在我想添加我的自定义应用程序,写在 Vue.js 中,它也是 dockerized,并调用 Alfresco REST APIs 来开发我的自定义。 当我调用任何 APIs 时,我总是收到 CORS 错误。

我尝试了很多次,改变了 Alfresco 配置,Tomcat 服务器配置,使用 jar 等等,但没有任何改变。

我得到的错误总是Access to XMLHttpRequest at 'http://my-alfresco-url/alfresco/api/-default-/public/authentication/versions/1/tickets' from origin 'http://localhost:8080' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

这是我简化的 API 调用(只是为了简单起见,我在 Vue.js 应用程序中使用的获取方法是相同的)

    <!DOCTYPE html>
    <html>

    <head>
        <meta charset='utf-8'>
        <meta http-equiv='X-UA-Compatible' content='IE=edge'>
        <title>Page Title</title>
        <meta name='viewport' content='width=device-width, initial-scale=1'>
    </head>

    <body>

    <script>
        var myHeaders = new Headers();
        myHeaders.append("Content-Type", "application/json");
        myHeaders.append("Access-Control-Allow-Origin", "*");
        myHeaders.append("Access-Control-Allow-Methods", "GET, POST, PATCH, PUT, DELETE, OPTIONS");
        myHeaders.append("Access-Control-Allow-Headers","Origin, Content-Type, X-Auth-Token, Access-Control-Allow-Methods, Access-Control-Allow-Origin");

        var raw = JSON.stringify({
            "userId": "user",
            "password": "password"
        });

        var requestOptions = {
            method: 'POST',
            headers: myHeaders,
            body: raw,
            redirect: 'follow',
        };

        fetch("http://my-alfresco-url/alfresco/api/-default-/public/authentication/versions/1/tickets", requestOptions)
            .then(response => response.json())
            .then(result => console.log(result))
            .catch(error => console.log('error', error));
    </script>

    </body>

    </html>

这是我的露天场所-global.properties:

    cors.enabled=true
    cors.allowed.origins=*
    cors.allowed.methods=GET,POST,HEAD,OPTIONS,PUT
    cors.allowed.headers=Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers
    cors.exposed.headers=Access-Control-Allow-Origin,Access-Control-Allow-Credentials
    cors.support.credentials=false
    cors.preflight.maxage=1800

我尝试更改这些参数,但没有用。

我试过更改 tomcat/conf/web.xml 或 tomcat/webapps/alfresco/WEB-INF/web.xml 添加此部分

    <!-- CORS Filter Begin -->
    <filter>
        <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</param-value>
        </init-param>
        <init-param>
            <param-name>cors.allowed.headers</param-name>
            <param-value>Content-Type,X-Requested-With,accept,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>1800</param-value>
        </init-param>
    </filter><!-- CORS Filter End -->


    <!-- CORS Filter Mappings Begin -->
    <filter-mapping>
        <filter-name>CorsFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!-- CORS Filter Mappings End -->

但如果我按所示使用它,那么整个 Alfresco 都会损坏,并出现类似

的错误
    provacors-ass_1       | 2021-04-19 12:07:10.007 ERROR (org.alfresco.solr.AlfrescoCoreAdminHandler@29a5f4e7_Worker-24) [   ] o.a.s.t.AbstractTracker Tracking failed for AclTracker - archive
    provacors-ass_1       | org.alfresco.error.AlfrescoRuntimeException: 03190007 api/solr/aclchangesets return status:404
    provacors-ass_1       |         at org.alfresco.solr.client.SOLRAPIClient.getAclChangeSets(SOLRAPIClient.java:169)
    provacors-ass_1       |         at org.alfresco.solr.tracker.AclTracker.checkRepoAndIndexConsistency(AclTracker.java:338)
    provacors-ass_1       |         at org.alfresco.solr.tracker.AclTracker.trackRepository(AclTracker.java:303)
    provacors-ass_1       |         at org.alfresco.solr.tracker.AclTracker.doTrack(AclTracker.java:95)
    provacors-ass_1       |         at org.alfresco.solr.tracker.AbstractTracker.track(AbstractTracker.java:215)
    provacors-ass_1       |         at org.alfresco.solr.tracker.TrackerJob.execute(TrackerJob.java:47)
    provacors-ass_1       |         at org.quartz.core.JobRunShell.run(JobRunShell.java:216)
    provacors-ass_1       |         at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:563)

我已经关注了这个问题但没有任何结果:

我已经阅读了这个论坛但没有成功:

我也尝试过在 Vue 中使用 webpack 配置,但我不太确定我做对了。没用。

我错过了什么? 如何使用 Dockerized Alfresco 在另一个域中使用自定义 javascript 调用?

谢谢。

经过多次尝试,我终于成功了。这是我的做法。

首先,alfresco-global.properties cors 选项似乎无用且无效。至少对于 ACS。如果有人可以向我解释他们是如何工作的,我将不胜感激。

我需要将两个库添加到我的 Alfresco 6.2 tomcat/webapps/WEB-INF/lib 文件夹,cors-filter v2.5java- 属性-utils v. 1.9.1。它们必须完全是那些版本(不是较新的 2.10 和 1.16),否则它们将无法使用下一个 web.xml 配置。

我将它们添加到我的 acs/platform pom.xml

<dependency>
    <groupId>com.thetransactioncompany</groupId>
    <artifactId>cors-filter</artifactId>
    <version>2.5</version>
</dependency>
<dependency>
    <groupId>com.thetransactioncompany</groupId>
    <artifactId>java-property-utils</artifactId>
    <version>1.9.1</version>
</dependency>

这是他们的 web.xml 配置,放宽 CORS 过滤器:


   <!-- CORS Filter Begin -->
   <filter>
      <filter-name>CORS</filter-name>
      <filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
      <init-param>
         <param-name>cors.allowGenericHttpRequests</param-name>
         <param-value>true</param-value>
      </init-param>
      <init-param>
         <param-name>cors.allowOrigin</param-name>
         <param-value>*</param-value>
      </init-param>
      <init-param>
         <param-name>cors.allowSubdomains</param-name>
         <param-value>true</param-value>
      </init-param>
      <init-param>
         <param-name>cors.supportedMethods</param-name>
         <param-value>GET, HEAD, POST, PUT, DELETE, OPTIONS</param-value>
      </init-param>
      <init-param>
         <param-name>cors.supportedHeaders</param-name>
         <param-value>origin, authorization, x-file-size, x-file-name, content-type, accept, x-file-type</param-value>
      </init-param>
      <init-param>
         <param-name>cors.supportsCredentials</param-name>
         <param-value>true</param-value>
      </init-param>
      <init-param>
         <param-name>cors.maxAge</param-name>
         <param-value>3600</param-value>
      </init-param>
   </filter>
   <!-- CORS Filter End -->

   <!-- CORS Filter Mappings Begin -->
   <filter-mapping>
      <filter-name>CORS</filter-name>
      <url-pattern>/api/*</url-pattern>
      <url-pattern>/service/*</url-pattern>
      <url-pattern>/s/*</url-pattern>
      <url-pattern>/cmisbrowser/*</url-pattern>
   </filter-mapping>
   <!-- CORS Filter Mappings End -->

记下 <filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class> 值。

为了添加这些值,我自定义了我的 acs-platform-docker 项目并在 Dockerfile 说明中添加了下一行:

# TO ENABLE CORS
COPY web.xml ${TOMCAT_DIR}/webapps/alfresco/WEB-INF/web.xml

很明显,修改后的web.xml文件放在acs-platform-docker资源文件夹中,构建镜像时需要包含它。

如果有其他方法可以解决这个问题,请告诉我。