English 中文(简体)
排除子公司的非主食
原标题:Filter mapping url-pattern that excludes subdirectories

是否有任何办法使过滤器不包含分方向?

例如。

我有根基中的C.xhtml档案,我还有一小数点,称为“测试”,其卷宗范围相同。 是否有人可以把过滤器绘制成背景材料,而不是“测试”目录中的文档?

最佳回答

<代码>ur-pattern在配对方面确实有限制性。 它只允许准确、事先确定或准确匹配。 不进行中校/校对配对。 e.g./*xhtml 您打算做的工作。

如果您希望在<代码>/测试中排除XVD 仅以倍数计算,您的最好方式是Filter倾听url-pattern>> ∗.xhtml,该编码基本上在doFilter(方法>上做了以下工作:

// First cast ServletRequest to HttpServletRequest.
HttpServletRequest hsr = (HttpServletRequest) request;

// Check if requested resource is not in /test folder.
if (!hsr.getServletPath().startsWith("/test/")) {
    // Not in /test folder. Do your thing here.
}

。 请求书的部分内容基本上从背景道路上返回。

如有必要,请将“<代码>/test”作为“<init-param>”;的过滤器,以便你能够控制“密码”内而不是“过滤器代码”内的价值。

问题回答

解决办法是,你基本上需要自己的“过滤器”类别,并将“URLs”界定为“过滤器的内分数”。

package test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginValidationFilter implements Filter {

    private String loginPage = "";
    private List<String> excludedURLs;

    public void init(FilterConfig filterConfig) throws ServletException {
        this.loginPage = filterConfig.getInitParameter("loginPage");

        String[] excluded = filterConfig.getInitParameter("excludedURLs").split(";");
        excludedURLs = new ArrayList<String>();
        for (int i = 0; i < excluded.length; i++) {
            excludedURLs.add(excluded[i]);
        }
    }

    public void destroy() {
        this.loginPage = "";
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException,
            ServletException {

        HttpServletRequest httpRequest = request instanceof HttpServletRequest ? (HttpServletRequest) request : null;
        HttpServletResponse httpResponse = response instanceof HttpServletResponse ? (HttpServletResponse) response
                : null;

        if (httpRequest == null || httpResponse == null) {
            filterChain.doFilter(request, response);
            return;
        }

        boolean isExcludedURL = false;
        for (int i = 0; i < excludedURLs.size(); i++) {
            if (httpRequest.getRequestURL().indexOf(excludedURLs.get(i)) > -1) {
                isExcludedURL = true;
                break;
            }
        }

        if (isExcludedURL) {
            filterChain.doFilter(request, response);
        } else {
            if (UserUtil.validateUserLogin(httpRequest)) {
                filterChain.doFilter(request, response);
            } else {
                httpResponse.sendRedirect(httpRequest.getContextPath() + loginPage);
            }
        }
    }
}

并界定过滤器、绘图和在网上排除URLs。

<filter>
    <filter-name>LoginValidationFilter</filter-name>
    <filter-class>test.LoginValidationFilter</filter-class>
    <init-param>
        <param-name>loginPage</param-name>
        <param-value>/login.jsp</param-value>
    </init-param>
    <init-param>
        <param-name>excludedURLs</param-name>
        <param-value>/admin/;/assets/;/content/;/css/;/js/;/login.jsp;/login.cmd;/logout.cmd;forgot_password.jsp;pageName=</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>LoginValidationFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

利用标准服务器/过滤器的现有材料,没有直接办法排除该次子,因为<代码>*.ext风格测绘包括次子。 您可以通过宣布另一个专门处理<代码>/测试/*.xhtml的过滤器来开展这项工作,并相应地进行这项工作。

您能够增加你在环境中根基的每个档案的彩色价值,因此,过滤器将适用于这些档案,但不适用于测试夹中的档案(实际上它只适用于这些具体档案)。 以下是我的工作。

<url-pattern>/index.faces</url-pattern>




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签