使用 RegexpVirtualURIMapping 映射到 Magnolia 中的静态目录

Using RegexpVirtualURIMapping to map to static directories in Magnolia

我想设置 magnolia,以便对目录的所有请求都将重定向到静态资源。例如:如果 URL 匹配 /campaign/(.*) 它将转发到 /static/campaign/ 而无需重定向。

/                        => [no change]
/campaign/               => /static/campaign/index.html
/campaign/styles/all.css => /static/campaign/styles/all.css

在 JCR 中,我将 /modules/pages/virtualURIMapping 设置为:

<?xml version="1.0" encoding="UTF-8"?>
<sv:node sv:name="virtualURIMapping" xmlns:sv="http://www.jcp.org/jcr/sv/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <sv:property sv:name="jcr:primaryType" sv:type="Name">
    <sv:value>mgnl:content</sv:value>
  </sv:property>
  <sv:node sv:name="default">
    <sv:property sv:name="jcr:primaryType" sv:type="Name">
      <sv:value>mgnl:contentNode</sv:value>
    </sv:property>
    <sv:property sv:name="jcr:uuid" sv:type="String">
      <sv:value>c68cde34-eaaf-480d-b1fa-7ea98aa772b0</sv:value>
    </sv:property>
    <sv:property sv:name="class" sv:type="String">
      <sv:value>info.magnolia.cms.beans.config.RegexpVirtualURIMapping</sv:value>
    </sv:property>
    <sv:property sv:name="fromURI" sv:type="String">
      <sv:value>/campaign/([0-9A-Z]*)</sv:value>
    </sv:property>
    <sv:property sv:name="toURI" sv:type="String">
      <sv:value>forward:/static/campaign/</sv:value>
    </sv:property>
  </sv:node>
</sv:node>

我重新启动了服务器,但收到以下异常:

ERROR info.magnolia.rendering.engine.RenderingFilter  - RepositoryException while reading Resource [/static]
javax.jcr.PathNotFoundException: /static

似乎转发到 /static 进入渲染过滤器。您需要通过为 /static/*

添加旁路来排除它

您希望 Magnolia 忽略的任何内容都需要在整个过滤器链(如果您还想忽略身份验证)或至少在过滤器的 cms 个子链中被绕过。
要使 Magnolia 忽略某些 URI,您需要配置旁路,如其他答案中所述。

HTH,
一月

通过创建以下重写过滤器而不是添加到 JCR 来解决它:

web.xml:

<filter>
    <filter-name>campaign</filter-name>
    <filter-class>com.britishgas.contenthub.filters.CampaignRewriteFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>campaign</filter-name>
    <url-pattern>/campaign/*</url-pattern>
</filter-mapping>

Rewrite Filter:

package com.example;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

public class CampaignRewriteFilter implements Filter {

    private RequestDispatcher defaultRequestDispatcher;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        this.defaultRequestDispatcher = filterConfig.getServletContext().getNamedDispatcher("default");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        String uri = req.getRequestURI().replaceFirst("/campaign/", "/static/campaign/");
        RequestDispatcher dispatcher = request.getRequestDispatcher(uri);
        dispatcher.forward(request, response);
    }

    @Override
    public void destroy() {

    }
}