How to match URL pattern like /abc?path=xyz in web.xml

3.4k Views Asked by At

I have a html file at some location in my environment, which I need to restrict from accessing from all users. So I am just trying to add a filter and restrict the html page or to redirect it to another page. Here is the code I am using,

<filter>
    <filter-name>PageCheckFilter</filter-name>
    <filter-class>com.src.filter.PageCheckFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>PageCheckFilter</filter-name>
    <url-pattern>/app?asset=test&path=/xyz-0.1.2/page.html</url-pattern>
</filter-mapping>

I have tried many URL patterns to match this URL but all in vain. Please suggest me something so that I could restrict this page from accessing.

2

There are 2 best solutions below

4
On

The way you are doing is not the correct way to achieve.

What you can do it write a filter which servers pages on proper Authentication. Write a authentication filter and map all your request to that filter.

So that the filter forwards the request if and only if valid user/client requested the particular page.

For ex: checking the logged in user in session or not.

Look at out Filter tag info, you can find a good example to start with a filter.

https://stackoverflow.com/tags/servlet-filters/info

2
On

You can't include query parameters in your filter mapping! According to the servlet specification, the URL path used for mapping is the request URL minus context path and parameters (see chapter 12.1 - Use or URL Paths). So the query string is removed from the URL before it is mapped to your pattern.

Even if it were possible to have a query string in your mapping, it would only work if the query parameters are not reordered (e.g. /app?path=/xyz-0.1.2/page.html&asset=test).

Instead, you should map your filter to all requests to /app and analyze the query parameter within the filter by yourself!