petclinic-tutorial.xml 8.2 KB

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