petclinic-tutorial.html 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <html>
  2. <head>
  3. <title>Tutorial: Adding Security to Spring Petclinic</title>
  4. </head>
  5. <body>
  6. <h1>Tutorial: Adding Security to Spring Petclinic</h1>
  7. <h2>Preparation</h2>
  8. <p>To complete this tutorial, you will require a servlet container (such as Tomcat)
  9. and a general understanding of using Spring without Acegi Security. The Petclinic
  10. sample itself is part of Spring and should help you learn Spring. We suggest you
  11. only try to learn one thing at a time, and start with Spring/Petclinic before
  12. Acegi Security.
  13. </p>
  14. <p>
  15. You will also need to download:
  16. <ul>
  17. <li>Spring 2.0 with dependencies ZIP file</li>
  18. <li>Acegi Security 1.0.2</li>
  19. </ul>
  20. </p>
  21. <p>
  22. Unzip both files. After unzipping Acegi Security, you'll need to unzip the
  23. acegi-security-sample-tutorial.war file, because we need some files that are
  24. included within it. In the code below, we'll refer to the respective unzipped
  25. locations as %spring% and %acegi% (with the latter variable referring to the
  26. unzipped WAR, not the original ZIP). There is no need to setup any environment
  27. variables to complete the tutorial.
  28. </p>
  29. <h2>Add required Acegi Security files to Petclinic</h2>
  30. <p>
  31. We now need to put some extra files into Petclinic. The following commands should work:
  32. <pre>
  33. mkdir %spring%\samples\petclinic\war\WEB-INF\lib
  34. copy %acegi%\acegilogin.jsp %spring%\samples\petclinic\war
  35. copy %acegi%\accessDenied.jsp %spring%\samples\petclinic\war
  36. copy %acegi%\WEB-INF\users.properties %spring%\samples\petclinic\war\WEB-INF
  37. copy %acegi%\WEB-INF\applicationContext-acegi-security.xml %spring%\samples\petclinic\war\WEB-INF
  38. copy %acegi%\WEB-INF\lib\acegi-security-1.0.0.jar %spring%\samples\petclinic\war\WEB-INF\lib
  39. copy %acegi%\WEB-INF\lib\oro-2.0.8.jar %spring%\samples\petclinic\war\WEB-INF\lib
  40. copy %acegi%\WEB-INF\lib\commons-codec-1.3.jar %spring%\samples\petclinic\war\WEB-INF\lib
  41. </pre>
  42. </p>
  43. <h2>Configure Petclinic's files</h2>
  44. <p>Edit %spring%\samples\petclinic\war\WEB-INF\web.xml and insert the following block of code.
  45. <pre>
  46. &lt;filter&gt;
  47. &lt;filter-name&gt;Acegi Filter Chain Proxy&lt;/filter-name&gt;
  48. &lt;filter-class&gt;org.acegisecurity.util.FilterToBeanProxy&lt;/filter-class&gt;
  49. &lt;init-param&gt;
  50. &lt;param-name&gt;targetClass&lt;/param-name&gt;
  51. &lt;param-value&gt;org.acegisecurity.util.FilterChainProxy&lt;/param-value&gt;
  52. &lt;/init-param&gt;
  53. &lt;/filter&gt;
  54. &lt;filter-mapping&gt;
  55. &lt;filter-name&gt;Acegi Filter Chain Proxy&lt;/filter-name&gt;
  56. &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  57. &lt;/filter-mapping&gt;
  58. </pre>
  59. Next, locate the "contextConfigLocation" parameter, and add a new line into the existing param-value.
  60. The resulting block will look like this:
  61. <pre>
  62. &lt;context-param&gt;
  63. &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
  64. &lt;param-value&gt;
  65. /WEB-INF/applicationContext-jdbc.xml
  66. /WEB-INF/applicationContext-acegi-security.xml
  67. &lt;/param-value&gt;
  68. &lt;/context-param&gt;
  69. </pre>
  70. </p>
  71. <p>
  72. To make it easier to experiment with the application, now edit
  73. %spring%\samples\petclinic\war\WEB-INF\jsp\footer.jsp. Add a new "logout" link, as shown:
  74. <pre>
  75. &lt;table style="width:100%"&gt;&lt;tr&gt;
  76. &lt;td&gt;&lt;A href="&lt;c:url value="/welcome.htm"/&gt;"&gt;Home&lt;/A&gt;&lt;/td&gt;
  77. &lt;td&gt;&lt;A href="&lt;c:url value="/j_acegi_logout"/&gt;"&gt;Logout&lt;/A&gt;&lt;/td&gt;
  78. &lt;td style="text-align:right;color:silver"&gt;PetClinic :: a Spring Framework demonstration&lt;/td&gt;
  79. &lt;/tr&gt;&lt;/table&gt;
  80. </pre>
  81. </p>
  82. <p>
  83. Our last step is to specify which URLs require authorization and which do not. Let's
  84. edit %spring%\samples\petclinic\war\WEB-INF\applicationContext-acegi-security.xml.
  85. Locate the bean definition for FilterSecurityInterceptor. Edit its objectDefinitionSource
  86. property so that it reflects the following:
  87. <pre>
  88. &lt;property name="objectDefinitionSource"&gt;
  89. &lt;value&gt;
  90. CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
  91. PATTERN_TYPE_APACHE_ANT
  92. /acegilogin.jsp=IS_AUTHENTICATED_ANONYMOUSLY
  93. /**=IS_AUTHENTICATED_REMEMBERED
  94. &lt;/value&gt;
  95. &lt;/property&gt;
  96. </pre>
  97. </p>
  98. <h2>Start Petclinic's database</h2>
  99. <p>Start the Hypersonic server (this is just normal Petclinic configuration):
  100. <pre>
  101. cd %spring%\samples\petclinic\db\hsqldb
  102. server
  103. </pre>
  104. </p>
  105. <p>
  106. Insert some data (again, normal Petclinic configuration):
  107. <pre>
  108. cd %spring%\samples\petclinic
  109. build setupDB
  110. </pre>
  111. </p>
  112. <h2>Build and deploy the Petclinic WAR file</h2>
  113. <p>
  114. Use Petclinic's Ant build script and deploy to your servlet container:
  115. <pre>
  116. cd %spring%\samples\petclinic
  117. build warfile
  118. copy dist\petclinic.war %TOMCAT_HOME%\webapps
  119. </pre>
  120. </p>
  121. <p>Finally, start your container and try to visit the home page.
  122. Your request should be intercepted and you will be forced to login.</p>
  123. <h2>Optional Bonus: Securing the Middle Tier</h2>
  124. <p>
  125. Whilst you've now secured your web requests, you might want to stop users
  126. from being able to add clinic visits unless authorized. We'll make it so
  127. you need to hold ROLE_SUPERVISOR to add a clinic visit.
  128. </p>
  129. <p>
  130. In %spring%\samples\petclinic\war\WEB-INF\applicationContext-jdbc.xml, locate
  131. the TransactionProxyFactoryBean definition. Add an additional property after
  132. the existing "preInterceptors" property:
  133. <pre>
  134. &lt;property name="postInterceptors" ref="methodSecurityInterceptor"/&gt;
  135. </pre>
  136. </p>
  137. <p>
  138. Finally, we need to add in the referred-to "methodSecurityInterceptor" bean definition.
  139. So pop an extra bean definition in, as shown below:
  140. <pre>
  141. &lt;bean id="methodSecurityInterceptor" class="org.acegisecurity.intercept.method.aopalliance.MethodSecurityInterceptor"&gt;
  142. &lt;property name="authenticationManager"&gt;&lt;ref bean="authenticationManager"/&gt;&lt;/property&gt;
  143. &lt;property name="accessDecisionManager"&gt;
  144. &lt;bean class="org.acegisecurity.vote.AffirmativeBased"&gt;
  145. &lt;property name="allowIfAllAbstainDecisions" value="false"/&gt;
  146. &lt;property name="decisionVoters"&gt;
  147. &lt;list&gt;
  148. &lt;bean class="org.acegisecurity.vote.RoleVoter"/&gt;
  149. &lt;bean class="org.acegisecurity.vote.AuthenticatedVoter"/&gt;
  150. &lt;/list&gt;
  151. &lt;/property&gt;
  152. &lt;/bean&gt;
  153. &lt;/property&gt;
  154. &lt;property name="objectDefinitionSource"&gt;
  155. &lt;value&gt;
  156. org.springframework.samples.petclinic.Clinic.*=IS_AUTHENTICATED_REMEMBERED
  157. org.springframework.samples.petclinic.Clinic.storeVisit=ROLE_SUPERVISOR
  158. &lt;/value&gt;
  159. &lt;/property&gt;
  160. &lt;/bean&gt;
  161. </pre>
  162. </p>
  163. <p>
  164. Redeploy your web application. Use the earlier process to do that. Be careful to
  165. ensure that the old Petclinic WAR is replaced by the new Petclinic WAR in your
  166. servlet container. Login as "marissa", who has ROLE_SUPERVISOR. You will be able to
  167. then view a customer and add a visit. Logout, then login as anyone other than Marissa.
  168. You will receive an access denied error when you attempt to add a visit.
  169. </p>
  170. <p>
  171. To clean things up a bit, you might want to wrap up by hiding the "add visit" link
  172. unless you are authorized to use it. Acegi Security provides a tag library to help
  173. you do that. Edit %spring%\samples\petclinic\war\WEB-INF\jsp\owner.jsp. Add
  174. the following line to the top of the file:
  175. <pre>
  176. &lt;%@ taglib prefix="authz" uri="http://acegisecurity.org/authz" %&gt;
  177. </pre>
  178. Next, scroll down and find the link to "add visit". Modify it as follows:
  179. <pre>
  180. &lt;authz:authorize ifAllGranted="ROLE_SUPERVISOR"&gt;
  181. &lt;FORM method=GET action="&lt;c:url value="/addVisit.htm"/&gt;" name="formVisitPet&lt;c:out value="${pet.id}"/&gt;"&gt;
  182. &lt;INPUT type="hidden" name="petId" value="&lt;c:out value="${pet.id}"/&gt;"/&gt;
  183. &lt;INPUT type="submit" value="Add Visit"/&gt;
  184. &lt;/FORM&gt;
  185. &lt;/authz:authorize&gt;
  186. </pre>
  187. </p>
  188. <h2>What now?</h2>
  189. <p>
  190. These steps can be applied to your own application. Although we do suggest
  191. that you visit <a href="http://acegisecurity.org">http://acegisecurity.org</a>
  192. and in particular review the "Suggested Steps" for getting started with Acegi
  193. Security. The suggested steps are optimized for learning Acegi Security quickly
  194. and applying it to your own projects. It also includes realistic time estimates
  195. for each step so you can plan your integration activities.</p>
  196. </body>
  197. </html>