いくつかの具体的なURLを除外できますか? 内部 ? 質問する

いくつかの具体的なURLを除外できますか? 内部 ? 質問する

1 つの具体的な URL を除くすべての URL に具体的なフィルターを適用します (つまり、/*を除く/specialpath)。

それを行う可能性はありますか?


サンプルコード:

<filter>
    <filter-name>SomeFilter</filter-name>
    <filter-class>org.somproject.AFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>SomeFilter</filter-name>
    <url-pattern>/*</url-pattern>   <!-- the question is: how to modify this line?  -->
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
</filter-mapping>

ベストアンサー1

標準のサーブレットAPIはこの機能をサポートしていません。このためには、次のような書き換えURLフィルタを使用することをお勧めします。タッキーのもの(これは Apache HTTPD の と非常によく似ています)、またはをリッスンする Filter の メソッドmod_rewriteにチェックを追加します。doFilter()/*

String path = ((HttpServletRequest) request).getRequestURI();
if (path.startsWith("/specialpath/")) {
    chain.doFilter(request, response); // Just continue chain.
} else {
    // Do your business stuff here for all paths other than /specialpath.
}

必要に応じて、無視するパスをinit-paramフィルターの として指定して、フィルター内で制御できるようにすることができますweb.xml。フィルター内では次のようにして取得できます。

private String pathToBeIgnored;

public void init(FilterConfig config) {
    pathToBeIgnored = config.getInitParameter("pathToBeIgnored");
}

フィルターがサードパーティ API の一部であり、変更できない場合は、より具体的な にマップしますurl-pattern。たとえば、サードパーティ フィルターに一致するパスに転送する/otherfilterpath/*新しいフィルターを作成します。/*

String path = ((HttpServletRequest) request).getRequestURI();
if (path.startsWith("/specialpath/")) {
    chain.doFilter(request, response); // Just continue chain.
} else {
    request.getRequestDispatcher("/otherfilterpath" + path).forward(request, response);
}

このフィルターが無限ループで自身を呼び出すのを回避するには、フィルターが のみ でリッスン (ディスパッチ)REQUESTし、サードパーティ フィルターがFORWARDのみ でオンになるようにする必要があります。

参照:

おすすめ記事