Configure in Web. XML to filter all URL requests, like "drum passes", chained.
The configuration is divided into two types A and B.
A: General Configuration
Add the following content to Web. xml:
<filter>
<filter-name>permissionFilter</filter-name>
<filter-class>com.taobao.riskm.filter.PermissionFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>permissionFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Composed of filter and filter-mapping. filter specifies the filters processing class (implements the filter interface), filter-mapping specifies the rules for filtering.
B: Advanced Configuration (Allow agents to inject spring beans)
Add the following content to Web. xml:
<filter>
<filter-name>permission</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>permission</filter-name>
<url-pattern>*.htm</url-pattern>
</filter-mapping>
In the spring bean configuration, add:
<bean id= "Permission" class= "Com.taobao.kfc.kwb.web.permission.PermissionHttpServlet" ></bean>
Because filter is loaded before the bean, that is, spring loads the class specified by the filter into the container, so that the spring bean injected in the filter is null.
Workaround:
The Delegatingfilterproxy class is added to the filter first, and "Targetfilterlifecycle" indicates all the life cycles that are acting on the filter.
The principle is that the Delegatingfilterproxy class is a proxy class, and all requests are sent to the filter agent first, and then to this bean in spring by "Filter-name".
The bean configured in spring has the same name as the <filter-name> in Web. Xml.
In addition, the spring Bean implements the filter interface, but by default the spring container manages its lifecycle (not managed by a server container such as Tomcat). If "Targetfilterlifecycle" is set to true, spring is used to manage Filter.init () and Filter.destroy (), and if False, the two methods fail!!
The biggest difference between B and a is that a is a filter, which is loaded into container, which cannot invoke subsequent beans in spring, and B is a spring bean that can refer to other beans. And the request is delegated to B through the Delegatingfilterproxy Class!
Another way to configure B:
<filter>
<filter-name>permission</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>targetBeanName</param-name>
<param-value>Spring-bean-name</param-value>
</init-param>
</filter>
That is to add a "targetbeanname" parameter, the value is the bean that actually executes the filter.
Note : Both the filter and the servlet can handle the URL, and filter is a chained process that can be passed as long as you want to continue processing, while the servlet is processed and returned! Suitable for simple logic processing.
Appendix:
<url-pattern> can choose from the following types of
/* All Resources
*.html resources that end in HTML
/fold/* the specified directory
/abc.html Specifying files
Starting with "/" and ending with "/*" are used to do the path mapping,
Previous prefix "*." The beginning is used to do the extension mapping.
Why is it wrong to define "/*.action" as a seemingly normal match?
Because this match belongs to the path mapping, also belongs to the extension mapping, causes the container to be unable to judge.
In addition, filter is like "recursion", the order in the Web. XML configuration represents the call flow of the filter, and the servlet is called without continuing to invoke other servlet! So the order in the configuration does not affect!
Summary: after work to know, every day can accumulate a lot of things, but did not have much time to write! It takes a bit of time to understand something, but it takes more time to write it ... Write out the benefits of needless to say, hope that after more squeeze some time, good precipitation.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Spring Framework's Filter application