{"id":725,"date":"2012-02-05T17:22:57","date_gmt":"2012-02-05T16:22:57","guid":{"rendered":"http:\/\/doanduyhai.wordpress.com\/?p=725"},"modified":"2012-02-05T17:22:57","modified_gmt":"2012-02-05T16:22:57","slug":"spring-security-part-iii-authenticationprocessingfilter-anonymousfilter","status":"publish","type":"post","link":"https:\/\/www.doanduyhai.com\/blog\/?p=725","title":{"rendered":"Spring Security part III : AuthenticationProcessingFilter &#038; AnonymousFilter"},"content":{"rendered":"<p>In this post we&#8217;ll examine in depth the <strong>AuthenticationProcessingFilter<\/strong> &amp; <strong>AnonymousFilter<\/strong><\/p>\n<p><!--more--><\/p>\n<h1>V AuthenticationProcessingFilter<\/h1>\n<p>The <strong>AuthenticationProcessingFilter<\/strong> interface is quite complex. Indeed it acts like a router and delegates all sub-tasks of authentication to other actors. The <strong>AuthenticationProcessingFilter<\/strong> collaborates with the following interfaces:<\/p>\n<ul>\n<li><strong>AuthenticationManager<\/strong>: responsible for the authentication of the user with provided credentials<\/li>\n<li><strong>AuthenticationSuccessHandler<\/strong>: responsible for the processing if authentication is successfull<\/li>\n<li><strong>AuthenticationFailureHandler<\/strong>: responsible for the processing if authentication is not successfull<\/li>\n<\/ul>\n<p>Spring configuration:<\/p>\n<pre class=\"brush: xml; highlight: [2,4,6,7,11,12]; title: ; toolbar: false; wrap-lines: false; notranslate\" title=\"\">\n&amp;lt;bean id=&amp;quot;authenticationProcessingFilter&amp;quot; \nclass=&amp;quot;org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter&amp;quot;&amp;gt;\n  \t&amp;lt;property name=&amp;quot;authenticationManager&amp;quot; ref=&amp;quot;authenticationManager&amp;quot;\/&amp;gt;\n  \t&amp;lt;property name=&amp;quot;filterProcessesUrl&amp;quot; value=&amp;quot;\/j_myApplication_security_check&amp;quot;\/&amp;gt;\n  \t&amp;lt;property name=&amp;quot;authenticationSuccessHandler&amp;quot;&amp;gt;\n\t\t&amp;lt;bean class=&amp;quot;org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler&amp;quot;&amp;gt;\n\t\t\t&amp;lt;property name=&amp;quot;defaultTargetUrl&amp;quot; value=&amp;quot;\/pages\/Home\/home.html&amp;quot; \/&amp;gt;\n\t\t&amp;lt;\/bean&amp;gt;\n\t&amp;lt;\/property&amp;gt;\n\t&amp;lt;property name=&amp;quot;authenticationFailureHandler&amp;quot;&amp;gt;\n\t\t&amp;lt;bean class=&amp;quot;org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler&amp;quot;&amp;gt;\n\t\t\t&amp;lt;property name=&amp;quot;defaultFailureUrl&amp;quot; value=&amp;quot;\/pages\/Security\/login.html?login_error=1&amp;quot; \/&amp;gt;\n\t\t&amp;lt;\/bean&amp;gt;\n\t&amp;lt;\/property&amp;gt;\n&amp;lt;\/bean&amp;gt; \n<\/pre>\n<p>First we use the <strong>UsernamePasswordAuthenticationFilter<\/strong> as implementation of the <strong>AuthenticationProcessingFilter<\/strong> interface. If you are using a CAS server or JAAS, you should use a dedicated implementation or write your own.<\/p>\n<p>Next, at <strong>line 3<\/strong>, an <strong>AuthenticationManager<\/strong> is injected. We&#8217;ll look into this <strong>AuthenticationManager<\/strong> very soon.<\/p>\n<p>At <strong>line 4<\/strong> we define the URL for the login process (<strong><em>\/j_myApplication_security_check<\/em><\/strong>). <strong>This URL should be the form POST Url for the login page<\/strong>. The default value is <strong><em>j_spring_security_check<\/em><\/strong> but you could change it here.<\/p>\n<p>&nbsp;<\/p>\n<h4>1) AuthenticationSuccessHandler<\/h4>\n<p>At <strong>lines 6 &amp; 7<\/strong> we inject an <strong>AuthenticationSuccessHandler<\/strong>. Spring provides by default 2 implementations;<\/p>\n<ol>\n<li><strong>SimpleUrlAuthenticationSuccessHandler<\/strong>: redirects automatically the user to the <em>defaultTargetUrl<\/em> set as parameter<\/li>\n<li><strong>SavedRequestAwareAuthenticationSuccessHandler<\/strong>: redirects the user to the requested page. If the initial request does not target any specific page, redirect it to the <em>defaultTargetUrl<\/em> set as parameter<\/li>\n<\/ol>\n<p>By default the <em>defaultTargetUrl<\/em> points to the home page.<\/p>\n<p>&nbsp;<\/p>\n<h4>2) AuthenticationFailureHandler<\/h4>\n<p>At <strong>lines 11 &amp; 12<\/strong> we inject the <strong>SimpleUrlAuthenticationFailureHandler<\/strong> for the <strong>AuthenticationProcessingFilter<\/strong>. This implementation simply redirects the user to an error page in case of unsuccessfull authentication. The error page URL set through the <em>defaultFailureUrl<\/em> parameter.<\/p>\n<p>&nbsp;<\/p>\n<h4>3) AuthenticationManager<\/h4>\n<p>Now we dig into the configuration of the <strong>AuthenticationManager<\/strong><\/p>\n<pre class=\"brush: xml; highlight: [4,5,10,11,14,18]; title: ; toolbar: false; wrap-lines: false; notranslate\" title=\"\">\n&amp;lt;bean id=&amp;quot;authenticationManager&amp;quot; class=&amp;quot;org.springframework.security.authentication.ProviderManager&amp;quot;&amp;gt;\n\t&amp;lt;property name=&amp;quot;providers&amp;quot;&amp;gt;\n\t\t&amp;lt;list&amp;gt;\n\t\t\t&amp;lt;ref bean=&amp;quot;myAuthenticationProvider&amp;quot; \/&amp;gt;\n\t\t\t&amp;lt;ref bean=&amp;quot;anonymousProvider&amp;quot; \/&amp;gt;\n\t\t&amp;lt;\/list&amp;gt;\n\t&amp;lt;\/property&amp;gt;\n&amp;lt;\/bean&amp;gt;\n    \n&amp;lt;bean id=&amp;quot;anonymousProvider&amp;quot; class=&amp;quot;org.springframework.security.authentication.AnonymousAuthenticationProvider&amp;quot;&amp;gt;\n\t&amp;lt;property name=&amp;quot;key&amp;quot; value=&amp;quot;unique_key_123&amp;quot; \/&amp;gt;\n&amp;lt;\/bean&amp;gt;\n    \n&amp;lt;bean id=&amp;quot;myAuthenticationProvider&amp;quot; class=&amp;quot;org.springframework.security.authentication.dao.DaoAuthenticationProvider&amp;quot;&amp;gt;\n\t&amp;lt;property name=&amp;quot;userDetailsService&amp;quot; ref=&amp;quot;myUserDetailsService&amp;quot; \/&amp;gt;\n&amp;lt;\/bean&amp;gt;\n\t\n&amp;lt;bean id=&amp;quot;myUserDetailsService&amp;quot; class=&amp;quot;com.myApplication.service.TechWebSecurityService&amp;quot; &amp;gt;\n\t&amp;lt;property name=&amp;quot;userDao&amp;quot; ref=&amp;quot;myUserDao&amp;quot; \/&amp;gt;\n&amp;lt;\/bean&amp;gt;\n<\/pre>\n<p>First, the implementation of the <strong>AuthenticationManager<\/strong> interface is the <strong>ProviderManager<\/strong> class which only delegates the job of authentication to a list of authentication providers.<\/p>\n<p>Amoung the list of inject authentication provider is the <strong>AnonymousAuthenticationProvider<\/strong>. As expected this provider is usefull checking anonymous role. Please notice the unique key passed in at <strong>line 11<\/strong>.<\/p>\n<p>Next is the user-define provider:  <em>myAuthenticationProvider<\/em>. It is based on the <strong>DaoAuthenticationProvider<\/strong> implementation whic performs authentication against a datasource. If you are using JAAS for authentication please consider the <strong>JaasAuthenticationProvider<\/strong> class.<\/p>\n<p>For the <strong>DaoAuthenticationProvider<\/strong> to work we must inject an <strong>UserDetailService<\/strong> whose task is to check user credentials against a datasource. It is up to you to implement the <strong>UserDetailService<\/strong>. In the example this service simply delegates request to a DAO class (<strong>line 19<\/strong>)<\/p>\n<p>&nbsp;<\/p>\n<h1>VI AnonymousFilter<\/h1>\n<p>Next in the security filter chain is the <strong>AnonymousFilter<\/strong>.<\/p>\n<pre class=\"brush: xml; highlight: [2,3]; title: ; toolbar: false; wrap-lines: false; notranslate\" title=\"\">\n&amp;lt;bean id=&amp;quot;anonymousFilter&amp;quot; class=&amp;quot;org.springframework.security.web.authentication.AnonymousAuthenticationFilter&amp;quot; &amp;gt;\n\t&amp;lt;property name=&amp;quot;key&amp;quot; value=&amp;quot;unique_key_123&amp;quot; \/&amp;gt;\n\t&amp;lt;property name=&amp;quot;userAttribute&amp;quot; value=&amp;quot;anonymousUser,ROLE_ANONYMOUS&amp;quot; \/&amp;gt;\n&amp;lt;\/bean&amp;gt; \n<\/pre>\n<p>The <strong>AnonymousFilter<\/strong> job is to grant an anonymous role to the user. By default if there is no token in the security context, this filter will create one with anonymous attribute (defined via the <em>userAttribute<\/em> parameter) and the <em>key<\/em> value.<\/p>\n<p>Later, when the <strong>AnonymousAuthenticationProvider<\/strong> check the security context, if it finds a token with <strong>ROLE_ANONYMOUS<\/strong> role and a <strong>matching key value<\/strong> then the anonymous authentication is successfull.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post we&#8217;ll examine in depth the AuthenticationProcessingFilter &amp; AnonymousFilter<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[30,14],"tags":[],"_links":{"self":[{"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/725"}],"collection":[{"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=725"}],"version-history":[{"count":0,"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/725\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=725"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=725"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=725"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}