Filter Application of Spring framework, spring framework filter

Source: Internet
Author: User

Filter Application of Spring framework, spring framework filter

Configure in web. xml to filter all URL requests, just like "drumming and passing flowers.

Configuration can be divided into two types: A and B.

1. add the following content to xml: 2 <filter> 3 <filter-name> permissionFilter </filter-name> 4 <filter-class> com. taobao. riskm. filter. permissionFilter </filter-class> 5 </filter> 6 <filter-mapping> 7 <filter-name> permissionFilter </filter-name> 8 <url-pattern>/* </url-pattern> 9 </filter-mapping> 10 consists of filter and filter-mapping. Filter specifies the Filter processing class (implementing the filter Interface), and filter-mapping specifies the Filter rules.

B: Advanced Configuration(Allow proxy injection of Spring bean)

1. add the following content to xml: 2 <filter> 3 <filter-name> permission </filter-name> 4 <filter-class> org. springframework. web. filter. delegatingFilterProxy </filter-class> 5 <init-param> 6 <param-name> targetFilterLifecycle </param-name> 7 <param-value> true </param-value> 8 </init-param> 9 </filter> 10 <filter-mapping> 11 <filter-name> permission </filter-name> 12 <url-pattern> *. htm </url-pattern> 13 </filter-mapping> 14 15 Add: 16 <bean id = "permission" class = "com. taobao. kfc. kwb. web. permission. permissionHttpServlet "> </bean>

Because the filter is loaded first than the bean, that is, spring will first load the class specified by the filter to the container, so that the spring bean injected in the filter will be null.

Solution:
First, add the DelegatingFilterProxy class to the filter. "targetFilterLifecycle" indicates all the lifecycles acting on the filter.
The principle is that the DelegatingFilterProxy class is a proxy class. All requests are sent to the filter proxy first, and then the bean is assigned to spring according to "filter-name.

The bean name configured in Spring must be the same as <filter-name> in web. xml.

In addition, spring bean implements the Filter interface, but by default, it is managed by spring containers (not managed by tomcat server containers ). If "targetFilterLifecycle" is set to True, spring manages Filter. init () and Filter. destroy (). If it is set to false, the two methods are invalid !!

The biggest difference between B and A is that A is A filter that is first loaded into the container and cannot call the subsequent bean in spring. B is A spring bean, other beans can be referenced, and requests are all delegate to B through the DelegatingFilterProxy class!

Another configuration method of 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, add a "targetBeanName" parameter, whose value is the bean that actually executes the Filter.

Note:: Both the Filter and servlet can process the URL. The Filter is a chained process, which can be passed as long as you want to continue processing. The Servlet is a processing and returns! Suitable for simple logic processing.

Appendix:
<Url-pattern> you can select the following forms:

/* All resources
*. Html resources ending with html
/Fold/* specify the Directory
/Abc.html specified file
What starts with "/" and ends with "/*" is used for path ing,
The extension ing starts.

Why is it wrong to define "/*. action" such a seemingly normal match?
Because this match is a path ing and also an extension ing, the container cannot judge.

In addition, filters are like "recursion". The order in the web. xml configuration represents the filter call process. After the servlet is called, it will not continue to call other servlets! Therefore, the order in the configuration does not affect!

Summary:I learned after work that I can accumulate a lot of things every day, but I really don't have much time to write it out! It takes some time to understand something, but it takes more time to write it ...... I don't need to say much about the benefits of writing it out. I hope to squeeze more time in the future to settle down.

(Full text)

 

Thanks to the original author path: http://blog.csdn.net/whuslei/article/details/8134848

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.