我的问题是如何隐藏URL中显示的zul扩展名?

出于安全原因,我们不想展示应用程序中使用的技术。

网址:

http://localhost:8080/warname/index.zul


我希望该URL看起来像这样:

http://localhost:8080/warname/index


要么

http://localhost:8080/warname/


表示index.zul或.zul需要从url隐藏。

ZK Version: 8.0.0
Server: wildfly-8.2.1.Final
jdk: jdk1.8.0_51
Eclipse Version: Neon.2 Release (4.6.2)


web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_ID" version="2.4">
  <display-name>test4</display-name>
  <listener>
    <description>
        Used to cleanup when a session is destroyed</description>
    <display-name>ZK Session cleaner</display-name>
    <listener-class>org.zkoss.zk.ui.http.HttpSessionListener</listener-class>
  </listener>
  <servlet>
    <description>
        The ZK loader for ZUML pages</description>
    <servlet-name>zkLoader</servlet-name>
    <servlet-class>org.zkoss.zk.ui.http.DHtmlLayoutServlet</servlet-class>
    <init-param>
      <param-name>update-uri</param-name>
      <param-value>/zkau</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet>
    <description>
        The asynchronous update engine for ZK</description>
    <servlet-name>auEngine</servlet-name>
    <servlet-class>org.zkoss.zk.au.http.DHtmlUpdateServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>zkLoader</servlet-name>
    <url-pattern>*.zul</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>zkLoader</servlet-name>
    <url-pattern>*.zhtml</url-pattern>
  </servlet-mapping>
   <servlet-mapping>
    <servlet-name>auEngine</servlet-name>
    <url-pattern>/zkau/*</url-pattern>
  </servlet-mapping>

 <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
    <welcome-file>index.zul</welcome-file>
  </welcome-file-list>
</web-app>

最佳答案

我只能遵循Bilbo Baggins的建议:


  更改网址格式


有很多方法可以做到这一点,这里仅是几种...


在web.xml中指定一个欢迎文件列表(此处为example


如果URL中未提供文件名,则将显示任何文件夹的index.zul。


使用HTTP服务器将Apache httpd与mod_rewrite结合使用
如果要将内容保留在应用程序服务器中,则Servlet Spec提供了一种RequestDispatcher.forward()方法,可以在servlet过滤器或servlet中使用该方法。


与此question/answer类似,您可以对zul文件执行相同的操作。

package your.package;

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

public class NoZulForwardingFilter implements Filter {
    private static final String ZUL_VIEW_ROOT_PATH = "/pages";
    private static final String ZUL_VIEW_SUFFIX = ".zul";

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {}

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        HttpServletRequest httpServletRequest = ((HttpServletRequest) request);
        String servletPath = httpServletRequest.getServletPath();
        //Only process the paths starting with /pages, so as other requests get unprocessed.
        //You can register the filter itself for /pages/* only, too
        if (servletPath.startsWith(ZUL_VIEW_ROOT_PATH)
                && !servletPath.contains(ZUL_VIEW_SUFFIX)) {
            request.getRequestDispatcher(servletPath.concat(ZUL_VIEW_SUFFIX))
                    .forward(request, response);
        } else {
            chain.doFilter(httpServletRequest, response);
        }
    }

    @Override
    public void destroy() { }
}


并在您的web.xml中进行配置

<filter>
    <filter-name>nozulforwarding</filter-name>
    <filter-class>your.package.NoZulForwardingFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>nozulforwarding</filter-name>
    <url-pattern>/pages/*</url-pattern>
</filter-mapping>



最后,如果您不希望ZK应用程序中包含任何zul文件,则可以使用名为Richlets的纯Java方法。


注意:这些技术都不会掩盖您的应用程序是用ZK编写的,因为它总是下载包含ZK客户端API的特定JavaScript文件。这将使任何人都知道使用ZK,即使隐藏了URL的一部分。

10-08 08:23