{"id":1127,"date":"2019-01-31T14:17:53","date_gmt":"2019-01-31T06:17:53","guid":{"rendered":"http:\/\/van-yzt.com\/?p=1127"},"modified":"2019-01-31T14:17:53","modified_gmt":"2019-01-31T06:17:53","slug":"filter","status":"publish","type":"post","link":"https:\/\/huzi-baozi.com\/?p=1127","title":{"rendered":"filter"},"content":{"rendered":"<p>In Spring, we can use FilterChainProxy to manage custom filters properly.<\/p>\n<h2>\nCustom resource xml file<\/h2>\n<pre><code class=\"language-xml\">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;\n&lt;beans xmlns=&quot;http:\/\/www.springframework.org\/schema\/beans&quot;\n       xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot;\n       xsi:schemaLocation=&quot;http:\/\/www.springframework.org\/schema\/beans http:\/\/www.springframework.org\/schema\/beans\/spring-beans.xsd&quot;&gt;\n\n    &lt;bean id=&quot;webFilterChain&quot; class=&quot;org.springframework.security.web.DefaultSecurityFilterChain&quot;&gt;\n        &lt;constructor-arg name=&quot;requestMatcher&quot;&gt;\n            &lt;bean class=&quot;org.springframework.security.web.util.matcher.NegatedRequestMatcher&quot;&gt;\n                &lt;constructor-arg name=&quot;requestMatcher&quot;&gt;\n                    &lt;bean class=&quot;org.springframework.security.web.util.matcher.RegexRequestMatcher&quot;&gt;\n                        &lt;constructor-arg name=&quot;httpMethod&quot; value=&quot;&quot;\/&gt;\n                        &lt;constructor-arg name=&quot;pattern&quot; value=&quot;\/api\/.*&quot;\/&gt;\n                    &lt;\/bean&gt;\n                &lt;\/constructor-arg&gt;\n            &lt;\/bean&gt;\n        &lt;\/constructor-arg&gt;\n        &lt;constructor-arg name=&quot;filters&quot;&gt;\n            &lt;list&gt;\n                &lt;bean class=&quot;org.springframework.security.web.csrf.CsrfFilter&quot;&gt;\n                    &lt;constructor-arg name=&quot;csrfTokenRepository&quot;&gt;\n                        &lt;bean class=&quot;org.springframework.security.web.csrf.CookieCsrfTokenRepository&quot;\/&gt;\n                    &lt;\/constructor-arg&gt;\n                &lt;\/bean&gt;\n                &lt;ref bean=&quot;springSessionRepositoryFilter&quot;\/&gt;\n            &lt;\/list&gt;\n        &lt;\/constructor-arg&gt;\n    &lt;\/bean&gt;\n\n    &lt;bean id=&quot;customFilterChainProxy&quot; class=&quot;org.springframework.security.web.FilterChainProxy&quot;&gt;\n        &lt;constructor-arg&gt;\n            &lt;list&gt;\n                &lt;ref bean=&quot;webFilterChain&quot;\/&gt;\n            &lt;\/list&gt;\n        &lt;\/constructor-arg&gt;\n    &lt;\/bean&gt;\n\n&lt;\/beans&gt;\n<\/code><\/pre>\n<h2>\nweb.xml filter configuration<\/h2>\n<pre><code class=\"language-xml\">    &lt;context-param&gt;\n        &lt;param-name&gt;contextConfigLocation&lt;\/param-name&gt;\n        &lt;param-value&gt;\n            &lt;!-- ... --&gt;\n            classpath:spring\/filter.xml\n        &lt;\/param-value&gt;\n    &lt;\/context-param&gt;\n\n &lt;!-- Custom Filter --&gt;\n    &lt;filter&gt;\n        &lt;filter-name&gt;customFilterChainProxy&lt;\/filter-name&gt;\n        &lt;filter-class&gt;org.springframework.web.filter.DelegatingFilterProxy&lt;\/filter-class&gt;\n    &lt;\/filter&gt;\n    &lt;filter-mapping&gt;\n        &lt;filter-name&gt;customFilterChainProxy&lt;\/filter-name&gt;\n        &lt;url-pattern&gt;\/*&lt;\/url-pattern&gt;\n    &lt;\/filter-mapping&gt;\n<\/code><\/pre>\n<h2>\nExplain<\/h2>\n<p><code>customFilterChainProxy<\/code> filter-name in web.xml. This tells Container(Tomcat) to add filter by Class <code>org.springframework.web.filter.DelegatingFilterProxy<\/code>.<\/p>\n<p><code>DelegatingFilterProxy<\/code> : Spring will search filter-name (<code>customFilterChainProxy<\/code>) in Spring context for Good Management.And <code>customFilterChainProxy<\/code> is initialized in <code>filter.xml<\/code> resource file.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Spring, we can use FilterChainProxy to manage custom filters properly. Custom resource xml file &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt; &lt;beans xmlns=&quot;http:\/\/www.springframework.org\/schema\/beans&quot; xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot; xsi:schemaLocation=&quot;http:\/\/www.springframework.org\/schema\/beans http:\/\/www.springframework.org\/schema\/beans\/spring-beans.xsd&quot;&gt; &lt;bean id=&quot;webFilterChain&quot; class=&quot;org.springframework.security.web.DefaultSecurityFilterChain&quot;&gt; &lt;constructor-arg name=&quot;requestMatcher&quot;&gt; &lt;bean class=&quot;org.springframework.security.web.util.matcher.NegatedRequestMatcher&quot;&gt; &lt;constructor-arg name=&quot;requestMatcher&quot;&gt; &lt;bean class=&quot;org.springframework.security.web.util.matcher.RegexRequestMatcher&quot;&gt; &lt;constructor-arg name=&quot;httpMethod&quot; value=&quot;&quot;\/&gt; &lt;constructor-arg name=&quot;pattern&quot; value=&quot;\/api\/.*&quot;\/&gt; &lt;\/bean&gt; &lt;\/constructor-arg&gt; &lt;\/bean&gt; &lt;\/constructor-arg&gt; &lt;constructor-arg name=&quot;filters&quot;&gt; &lt;list&gt; &lt;bean class=&quot;org.springframework.security.web.csrf.CsrfFilter&quot;&gt; &lt;constructor-arg name=&quot;csrfTokenRepository&quot;&gt; &lt;bean class=&quot;org.springframework.security.web.csrf.CookieCsrfTokenRepository&quot;\/&gt; &lt;\/constructor-arg&gt; &lt;\/bean&gt; &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/huzi-baozi.com\/?p=1127\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;filter&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-1127","post","type-post","status-publish","format-standard","hentry","category-snippet"],"_links":{"self":[{"href":"https:\/\/huzi-baozi.com\/index.php?rest_route=\/wp\/v2\/posts\/1127","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/huzi-baozi.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/huzi-baozi.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/huzi-baozi.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/huzi-baozi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1127"}],"version-history":[{"count":1,"href":"https:\/\/huzi-baozi.com\/index.php?rest_route=\/wp\/v2\/posts\/1127\/revisions"}],"predecessor-version":[{"id":1128,"href":"https:\/\/huzi-baozi.com\/index.php?rest_route=\/wp\/v2\/posts\/1127\/revisions\/1128"}],"wp:attachment":[{"href":"https:\/\/huzi-baozi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1127"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/huzi-baozi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1127"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/huzi-baozi.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1127"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}