{"id":678,"date":"2012-01-22T18:31:41","date_gmt":"2012-01-22T17:31:41","guid":{"rendered":"http:\/\/doanduyhai.wordpress.com\/?p=678"},"modified":"2012-01-22T18:31:41","modified_gmt":"2012-01-22T17:31:41","slug":"spring-security-part-i-configuration-and-security-chain","status":"publish","type":"post","link":"https:\/\/www.doanduyhai.com\/blog\/?p=678","title":{"rendered":"Spring Security part I : Configuration &#038; Security Chain"},"content":{"rendered":"<p>In this series of articles we&#8217;ll dig into the <strong>Spring Security<\/strong> framework.<\/p>\n<p><strong>Spring Security<\/strong> can be considered as a flexible and portable security manager. It is a real alternative to built-in security manager available on many application servers.<\/p>\n<p> All transversal tasks related to security management <em>(login, logout, authentication, authorization<\/em> &#8230;) usually handled by the application server can be delegated to <strong>Spring Security<\/strong>. It helps decoupling the application logic &amp; security management from the proprietary security implementation provided by different JEE application servers.<\/p>\n<p><!--more--><\/p>\n<h1>I Installation<\/h1>\n<p>To enable <strong>Spring Security<\/strong>, you should add the following jar as <strong>Maven<\/strong> dependency to your project:<\/p>\n<pre class=\"brush: xml; title: ; wrap-lines: false; notranslate\" title=\"\">\n\t&amp;lt;dependency&amp;gt;\n  \t\t&amp;lt;groupId&amp;gt;org.springframework.security&amp;lt;\/groupId&amp;gt;\n  \t\t&amp;lt;artifactId&amp;gt;spring-security-core&amp;lt;\/artifactId&amp;gt;\n  \t\t&amp;lt;version&amp;gt;3.0.5.RELEASE&amp;lt;\/version&amp;gt;\n  \t\t&amp;lt;type&amp;gt;jar&amp;lt;\/type&amp;gt;\n  \t\t&amp;lt;scope&amp;gt;compile&amp;lt;\/scope&amp;gt;\n\t&amp;lt;\/dependency&amp;gt;\n  \t&amp;lt;dependency&amp;gt;\n  \t\t&amp;lt;groupId&amp;gt;org.springframework.security&amp;lt;\/groupId&amp;gt;\n  \t\t&amp;lt;artifactId&amp;gt;spring-security-web&amp;lt;\/artifactId&amp;gt;\n  \t\t&amp;lt;version&amp;gt;3.0.5.RELEASE&amp;lt;\/version&amp;gt;\n  \t\t&amp;lt;type&amp;gt;jar&amp;lt;\/type&amp;gt;\n  \t\t&amp;lt;scope&amp;gt;compile&amp;lt;\/scope&amp;gt;\n  \t&amp;lt;\/dependency&amp;gt;\n  \t&amp;lt;dependency&amp;gt;\n  \t\t&amp;lt;groupId&amp;gt;org.springframework.security&amp;lt;\/groupId&amp;gt;\n  \t\t&amp;lt;artifactId&amp;gt;spring-security-config&amp;lt;\/artifactId&amp;gt;\n  \t\t&amp;lt;version&amp;gt;3.0.5.RELEASE&amp;lt;\/version&amp;gt;\n  \t\t&amp;lt;type&amp;gt;jar&amp;lt;\/type&amp;gt;\n  \t\t&amp;lt;scope&amp;gt;compile&amp;lt;\/scope&amp;gt;\n  \t&amp;lt;\/dependency&amp;gt;\n\n<\/pre>\n<p>It is strongly recommended to create a separate <strong>spring-security.xml<\/strong> configuration file to manage all beans related to the security aspect.<\/p>\n<p>In the web.xml file, Spring&#8217;s DelegatingFilterProxy class should be declared as a servlet filter<\/p>\n<pre class=\"brush: xml; title: ; wrap-lines: false; notranslate\" title=\"\">\n     &amp;lt;filter&amp;gt;\n        &amp;lt;filter-name&amp;gt;filterChainProxy&amp;lt;\/filter-name&amp;gt;\n        &amp;lt;filter-class&amp;gt;org.springframework.web.filter.DelegatingFilterProxy&amp;lt;\/filter-class&amp;gt;\n    &amp;lt;\/filter&amp;gt;\n<\/pre>\n<p>and declared as the first filter in the to secure all the filter chain.<\/p>\n<pre class=\"brush: xml; highlight: [2]; title: ; wrap-lines: false; notranslate\" title=\"\">\n    &amp;lt;filter-mapping&amp;gt;\n    \t&amp;lt;filter-name&amp;gt;filterChainProxy&amp;lt;\/filter-name&amp;gt;\n      \t&amp;lt;url-pattern&amp;gt;\/*&amp;lt;\/url-pattern&amp;gt;\n    &amp;lt;\/filter-mapping&amp;gt;\n \t&amp;lt;filter-mapping&amp;gt;\n    \t&amp;lt;filter-name&amp;gt;ndcLogFilter&amp;lt;\/filter-name&amp;gt;\n      \t&amp;lt;url-pattern&amp;gt;\/*&amp;lt;\/url-pattern&amp;gt;\n    &amp;lt;\/filter-mapping&amp;gt;\n    ...\n<\/pre>\n<p>Please notice that the property &lt;<strong><em>filter-name<\/em><\/strong>&gt; should point to a valid <strong>bean id<\/strong> in the Spring context.<br \/>\n&nbsp;<\/p>\n<h1>II Overview<\/h1>\n<p>The security management process comes down to the following aspects:<\/p>\n<ol>\n<li><strong>Security chain<\/strong>: to be able to secure an application, we should clearly define a security chain with an entry and exit point. Every resource inside this chain will be secured according to user defined rules. The security chain is the main entry point for <strong>Spring Security<\/strong><\/li>\n<li><strong>Login\/Logout<\/strong>: any decent security framework should provides a consistent way to handle login &amp; logout events<\/li>\n<li><strong>Authentication<\/strong>: during this step, the framework tries to identify the end user with the provided credentials. The authentication can be done against a third party system plugged into <strong>Spring Security<\/strong><\/li>\n<li><strong>Anonymous role<\/strong>: it is sometimes necessary to allow unsecured access to some resources (such as <em>style sheet, image, login page<\/em> &#8230;). In this case the anonymous role management is required<\/li>\n<li><strong>Exception handling<\/strong>: a robust security system should provide a clean way to handle exceptions (<em>invalid login\/password, access denied<\/em> &#8230;)<\/li>\n<li><strong>Access management<\/strong>: in this step the framework will grant or deny access to requested resources based on the roles\/authorities granted to the user. These authorities(roles) are determined at the <strong>Authentication<\/strong> step<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h1>III Security Chain<\/h1>\n<p>Let&#8217;s have a closer look into the <strong>spring-security.xml<\/strong> configuration file<\/p>\n<pre class=\"brush: xml; highlight: [1,3,8]; title: ; wrap-lines: false; notranslate\" title=\"\">\n\t&amp;lt;bean id=&amp;quot;filterChainProxy&amp;quot; class=&amp;quot;org.springframework.security.web.FilterChainProxy&amp;quot;&amp;gt;\n\t\t&amp;lt;sec:filter-chain-map path-type=&amp;quot;ant&amp;quot;&amp;gt;\n\t\t    &amp;lt;sec:filter-chain pattern=&amp;quot;\/webServices\/**&amp;quot; filters=&amp;quot;\n\t\t           securityContextPersistenceFilterForWebServices,\n\t\t           WSAuthenticationFilter,\n\t\t           exceptionTranslationFilter,\n\t\t           filterSecurityInterceptor&amp;quot; \/&amp;gt;\n\t\t    &amp;lt;sec:filter-chain pattern=&amp;quot;\/**&amp;quot;  filters=&amp;quot;\n\t\t           securityContextPersistentFilter,\n\t\t           logoutFilter,\n\t\t           authenticationProcessingFilter,\n\t\t           anonymousFilter,\n\t\t           exceptionTranslationFilter,\n\t\t           filterSecurityInterceptor&amp;quot; \/&amp;gt;\n\t  \t&amp;lt;\/sec:filter-chain-map&amp;gt;\n\t&amp;lt;\/bean&amp;gt;\n<\/pre>\n<p>First, the id of the <strong>FilterChainProxy<\/strong> bean is set to <strong><em>filterChainProxy<\/em><\/strong>. This id is also the <strong>filter name<\/strong> of the Spring&#8217;s DelegatingFilterProxy declared previously in the <strong>web.xml<\/strong> file. It is the default convention.<\/p>\n<p>Next, the &lt;<strong><em>filter-chain-map<\/em><\/strong>&gt; allows to match a particular path pattern agains a security filter chain defined in &lt;<strong><em>filter-chain<\/em><\/strong>&gt; tag.<\/p>\n<p>The path pattern can be expressed using Ant style or regular expression and is configured by the propery <strong>path-type<\/strong>. <\/p>\n<p>You can set <em>as many filter chains as there are different path patterns<\/em>. In the above configuration, we have a filter chain dedicated to Web Services calls and a generic filter chain for all other requests.<\/p>\n<blockquote><p><em>Please notice that the Web Services filter chain is defined <strong>before<\/strong> the generic filter chain because its path pattern is a subset of the generic filter chain pattern. Since <strong>the filter chains are examined in the order of their definition<\/strong> we are sure that Web Services requests will always be intercepted first and never be processed through the generic filter chain.<\/em><\/p><\/blockquote>\n<p>Let&#8217;s examine the generic filter chain in depth.<\/p>\n<ol>\n<li><strong>securityContextPersistentFilter<\/strong>: this filter is used to store and retrieve the security context (user credentials, if any) between successive accesses to the application<\/li>\n<li><strong>logoutFilter<\/strong>: this filter handles the logout. It should be placed at the beginning of the filter chain so a click on the logout link (or button) will not go through the rest of the chain<\/li>\n<li><strong>authenticationProcessingFilter<\/strong>: this filter handles all the authentication process<\/li>\n<li><strong>anonymousFilter<\/strong>: this filter handles anonymous login and creates an Authentication object in the HTTP session for later use<\/li>\n<li><strong>exceptionTranslationFilter<\/strong>: this filter re-direct the user to an error page when Security exception is encountered<\/li>\n<li><strong>filterSecurityInterceptor<\/strong>: this filter is manaing the access management<\/li>\n<\/ol>\n<p>to be continued&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this series of articles we&#8217;ll dig into the Spring Security framework. Spring Security can be considered as a flexible and portable security manager. It is a real alternative to built-in security manager available on many application servers. All transversal&#8230;<br \/><a class=\"read-more-button\" href=\"https:\/\/www.doanduyhai.com\/blog\/?p=678\">Read more<\/a><\/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":[46,47],"_links":{"self":[{"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/678"}],"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=678"}],"version-history":[{"count":0,"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/678\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=678"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=678"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=678"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}