2011-12-20 48 views
29

特定のタスクを実行するためのフィルタを作成していますが、特定のURLパターンをフィルタに設定できません。私は予想していたとしてweb.xmlのURLパターンで正規表現を使用できますか?

<web.xml> 
    <filter> 
    <filter-name>myFilter</filter-name> 
    <filter-class>test.MyFilter</filter-class> 
    </filter> 

    <filter-mapping> 
    <filter-name>myFilter</filter-name> 
    <url-pattern>/org/test/*/keys/*</url-pattern> 
    </filter-mapping> 
</web-app> 

私のurl-pattern [ /org/test/ * /keys/ * ]が動作していない次のように私のフィルタのマッピングがあります。

私は次のようにブラウザからURLを呼び出しています:フィルタパターン上のURLが一致している必要がありますのためにそう

http://localhost:8080/myapp/org/test/SuperAdminReport/keys/superAdminReport.jsp 
http://localhost:8080/myapp/org/test/Adminreport/keys/adminReport.jsp 
http://localhost:8080/myapp/org/test/OtherReport/keys/otherReport.jsp 

。どうすればこれを達成できますか?

+3

私の知る限り、特定のフォルダの内容( 'path/to/my/folder/*')または特定の拡張子( '* .do')のみをurlパターンで使用することができます。 – bdares

+0

[この質問](http://stackoverflow.com/q/26732/922954)の回答を参照してください。 – aishwarya

答えて

6

有効なURLパターンではないため、あなたのURLは機能しません。あなたが選んだことはありませんマッピングの間にワイルドカードを適用しようとする場合は、のみstartまたはendでワイルドカードを適用することができ、サーブレットマッピングのために、以下の回答

web.xml

URL spec

0

を参照することができます。

3

それは動作しません、正規表現はそこに使用することはできません。あなたのアプリケーションのために、このURLを使用してみてください:

http://localhost:8080/myapp/org/test/keys/SuperAdminReport/superAdminReport.jsp 
http://localhost:8080/myapp/org/test/keys/Adminreport/adminReport.jsp 
http://localhost:8080/myapp/org/test/keys/OtherReport/otherReport.jsp 

そして、あなたのweb.xmlのこの設定:

<filter-mapping> 
    <filter-name>myFilter</filter-name> 
    <url-pattern>/org/test/keys/*</url-pattern> 
</filter-mapping> 
35

いいえ、あなたがそこに正規表現を使用することはできません。次のようにJavaサーブレット仕様V2.4(セクションsrv.11.1)によると、URL-pathは解釈されます。

  • 文字列「/」文字で始まるで終わります「/ *」接尾辞はパスマッピングに使用されます。
  • '*。'接頭辞で始まる文字列が拡張マッピングとして使用されます。
  • '/'文字のみを含む文字列は、アプリケーションの「デフォルト」サーブレットを示します。この場合、サーブレットパスは、 リクエストURIからコンテキストパスを引いたもので、パス情報はnullです。
  • 他のすべての文字列は完全一致にのみ使用されます。

ませ正規表現は許可されません。複雑なワイルドカードでもありません。

0

url-patternはワイルドカード文字で始めたり終わったりするだけなので、regexの真のパワーは使えません。

しかし、以前のプロジェクトでは、すべてのリクエストをインターセプトする単純なデフォルトフィルタを作成し、フィルタに正規表現が含まれているため、さらなるロジックを適用するかどうかが決まりました。このアプローチでは、パフォーマンスの低下はほとんどないことがわかりました。

以下は、正規表現パターンをフィルタ属性に移動することで強化できる簡単な例です。

ウェブ内のフィルタ設定。XML:他の人が指摘したように、正規表現のマッチングをフィルタリングする方法はありません

public class SampleFilter extends OncePerRequestFilter { 

    @Override 
    final protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { 
     if (applyLogic(request)) { 
      //Execute desired logic; 
     } 

     filterChain.doFilter(request, response); 
    } 

    protected boolean applyLogic(HttpServletRequest request) { 
     String path = StringUtils.removeStart(request.getRequestURI(), request.getContextPath()); 

     return PatternMatchUtils.simpleMatch(new String[] { "/login" }, path); 
    } 
} 
8

:任意の正規表現パターンを(申し訳ありませんが、春のOncePerRequestFilterの親クラスを使用しています)を使用することができ

<filter> 
    <filter-name>SampleFilter</filter-name> 
    <filter-class>org.test.SampleFilter</filter-class> 
</filter> 
<filter-mapping> 
    <filter-name>SampleFilter</filter-name> 
    <servlet-name>SampleServlet</servlet-name> 
</filter-mapping> 
<servlet-mapping> 
    <servlet-name>SampleServlet</servlet-name> 
    <url-pattern>/*</url-pattern> 
</servlet-mapping> 

基本フィルタの実装基本的なサーブレットフィルタ機能のみを使用します。サーブレットの仕様は、urlの効率的なマッチングを可能にする方法と、サーブレットがjava(正規表現がJava 1.4に到着した)での正規表現の有効性よりも前に記述されているため記述されている可能性があります。

正規表現は、おそらく(いいえ、私はそれをベンチマークしていない)あまりパフォーマンスのだろうが、あなたを強く処理時間によって制約や構成を容易にするためにパフォーマンスをトレードしたいされていない場合は、このようにそれを行うことができます。

をあなたのフィルタでは

:web.xmlのその後

private String subPathFilter = ".*"; 
private Pattern pattern; 

    @Override 
    public void init(FilterConfig filterConfig) throws ServletException { 
    String subPathFilter = filterConfig.getInitParameter("subPathFilter"); 
    if (subPathFilter != null) { 
     this.subPathFilter = subPathFilter; 
    } 
    pattern = Pattern.compile(this.subPathFilter); 
    } 

    public static String getFullURL(HttpServletRequest request) { 
    // Implement this if you want to match query parameters, otherwise 
    // servletRequest.getRequestURI() or servletRequest.getRequestURL 
    // should be good enough. Also you may want to handle URL decoding here. 
    } 

    @Override 
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { 
    Matcher m = pattern.matcher(getFullURL((HttpServletRequest) servletRequest)); 
    if (m.matches()) { 

     // filter stuff here. 

    } 
    } 

...

<filter> 
    <filter-name>MyFiltersName</filter-name> 
    <filter-class>com.konadsc.servlet.MyFilter</filter-class> 
    <init-param> 
     <param-name>subPathFilter</param-name> 
     <param-value>/org/test/[^/]+/keys/.*</param-value> 
    </init-param> 
    </filter> 
    <filter-mapping> 
    <filter-name>MyFiltersName</filter-name> 
    <url-pattern>/*</url-pattern> 
    </filter-mapping> 

doFilter()のif文を反転することでパターンを一致させないようにするのが便利な場合もあります(または、除外パターンの別のinitパラメータを追加してください)。