abiieez
abiieez

Reputation: 3189

URL Pattern with Spring MVC and Spring Security

I have seen some url-pattern in my xml e.g, in filter-mapping, intercept-url, mvc:resources, etc. Are these patterns always the same ? What's the difference among these URL patterns /, /*, /** ?

Upvotes: 2

Views: 3688

Answers (1)

M. Deinum
M. Deinum

Reputation: 125312

It depends in which context you ask this question:

  1. Servlet/Filter mappings
  2. Spring Security mappings

Servlet/Filter mappings

In the Web application deployment descriptor, the following syntax is used to define mappings:

  • A string beginning with a ‘/’ character and ending with a ‘/*’ suffix is used for path mapping.
  • A string beginning with a ‘*.’ prefix is used as an extension mapping.
  • The empty string ("") is a special URL pattern that exactly maps to the application's context root, i.e., requests of the form http://host:port/<contextroot>/. In this case the path info is ’/’ and the servlet path and context path is empty string (““).
  • A string containing only the ’/’ character indicates the "default" servlet of the application. In this case the servlet path is the request URI minus the context path and the path info is null.
  • All other strings are used for exact matches only

This comes from the Servlet Specification (JSR 315) (section 12.2).

In a Servlet/Filter mapping scenario / means the "default" servlet, normally this is where the DefaultServlet (in Tomcat that is) is mapped to. Basically it handles all incoming requests and doesn't pass them on further down the chain for processing (basically this is the last-catch-all mapping).

/* in the servlet mapping scenario means all incoming URLs (when it cannot be processed it will be handed of the the last-catch-all-mapping).


Spring Security mappings

Now when talking about Spring, /, /* and /** have a different meaning. They refer to so called Ant-style path expressions.

Where / means only / (the root of your application), where /* means the root including one level deep and where /** means everything.

So /foo/* will match a URL with /foo/bar but will not match /foo/bar/baz. Whereas /** or /foo/** would match all of them.

Upvotes: 8

Related Questions