petclinic-tutorial.apt 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. ---------------------------------------------
  2. Tutorial: Adding Security to Spring Petclinic
  3. ---------------------------------------------
  4. Tutorial: Adding Security to Spring Petclinic
  5. * Preparation
  6. To complete this tutorial, you will require a servlet container (such as Tomcat)
  7. and a general understanding of using Spring without Spring Security. The Petclinic
  8. sample itself is part of Spring and should help you learn Spring. We suggest you
  9. only try to learn one thing at a time, and start with Spring/Petclinic before
  10. Spring Security.
  11. You will also need to download:
  12. * {{{http://www.springframework.org/download}Spring 2.5.5 with dependencies ZIP file}}
  13. * {{{http://www.springframework.org/download}Spring Security 2.0.4}}
  14. Unzip both files. After unzipping Spring Security, you'll need to unzip the
  15. spring-security-sample-tutorial-2.0.4.war file, because we need some files that are
  16. included within it. After unzipping the war file, you will see a folder called
  17. spring-security-samples-tutorial-2.0.4.
  18. In the code below, we'll refer to the respective unzipped
  19. locations as %spring% and %spring-sec-tutorial% (with the later variable
  20. referring to the unzipped WAR, not the original ZIP). There is no need to setup
  21. any environment variables to complete the tutorial.
  22. * 1st part: Run the Petclinic application without Spring Security
  23. In order to make sure that you work in a stable environment, we will first
  24. set up the Petclinic application, without Spring Security.
  25. ** Start Petclinic's database
  26. Start the Hypersonic server:
  27. +------------------------------------------------------
  28. cd %spring%\samples\petclinic\db\hsqldb
  29. server
  30. +------------------------------------------------------
  31. Insert some data:
  32. +------------------------------------------------------
  33. cd %spring%\samples\petclinic
  34. ant setupDB
  35. +------------------------------------------------------
  36. ** Build and deploy the Petclinic WAR file
  37. Use Petclinic's Ant build script and deploy to your servlet container:
  38. +------------------------------------------------------
  39. cd %spring%\samples\petclinic
  40. build warfile
  41. copy dist\petclinic.war %TOMCAT_HOME%\webapps
  42. +------------------------------------------------------
  43. Finally, start your container and try to visit the petclinic home page.
  44. You are now able to browse the whole application without any authentication needed
  45. * Second part: set up Spring security
  46. ** Add required Spring Security files to Petclinic
  47. We now need to put some extra files into Petclinic.
  48. The following example is based on Windows MS-DOS. It only involves file copy.
  49. We believe you can adapt it easily on any operating system.
  50. +------------------------------------------------------
  51. copy %spring-sec-tutorial%\WEB-INF\applicationContext-security-ns.xml %spring%\samples\petclinic\war\WEB-INF
  52. copy %spring-sec-tutorial%\WEB-INF\lib\spring-security-core-2.0.4.jar %spring%\samples\petclinic\war\WEB-INF\lib
  53. copy %spring-sec-tutorial%\WEB-INF\lib\spring-security-core-tiger-2.0.4.jar %spring%\samples\petclinic\war\WEB-INF\lib
  54. copy %spring-sec-tutorial%\WEB-INF\lib\spring-security-acl-2.0.4.jar %spring%\samples\petclinic\war\WEB-INF\lib
  55. copy %spring-sec-tutorial%\WEB-INF\lib\spring-security-taglibs-2.0.4.jar %spring%\samples\petclinic\war\WEB-INF\lib
  56. copy %spring-sec-tutorial%\WEB-INF\lib\commons-codec-1.3.jar %spring%\samples\petclinic\war\WEB-INF\lib
  57. +------------------------------------------------------
  58. ** Configure Petclinic's files
  59. Edit %spring%\samples\petclinic\war\WEB-INF\web.xml. The "contextConfigLocation" specifies Spring configuration files that should be used
  60. by the petclinic application. Locate the "contextConfigLocation" parameter and add a new line into
  61. the existing param-value. Now that we are using Spring Security, It should also declare
  62. applicationContext-security.xml (Spring config file for Spring Security).
  63. The resulting block will look like this:
  64. +------------------------------------------------------
  65. <context-param>
  66. <param-name>contextConfigLocation</param-name>
  67. <param-value>
  68. /WEB-INF/applicationContext-jdbc.xml
  69. /WEB-INF/applicationContext-security.xml
  70. </param-value>
  71. </context-param>
  72. +------------------------------------------------------
  73. Still inside web.xml, insert the following block of code.
  74. It should be inserted right after the </context-param> end-tag.
  75. +------------------------------------------------------
  76. <filter>
  77. <filter-name>springSecurityFilterChain</filter-name>
  78. <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  79. </filter>
  80. <filter-mapping>
  81. <filter-name>springSecurityFilterChain</filter-name>
  82. <url-pattern>/*</url-pattern>
  83. </filter-mapping>
  84. +------------------------------------------------------
  85. Our last step is to specify which URLs require authorization and which do not. Let's
  86. edit %spring%\samples\petclinic\war\WEB-INF\applicationContext-security-ns.xml.
  87. All URLs ending with '.do' will be protected.
  88. +------------------------------------------------------
  89. <http auto-config="true">
  90. <intercept-url pattern="/*.do" access="ROLE_USER" />
  91. <intercept-url pattern="/**" access="IS_AUTHENTICATED_ANONYMOUSLY" />
  92. </http>
  93. +------------------------------------------------------
  94. ** Test
  95. Redeploy your web application. Use the earlier process to do that. Be careful to
  96. ensure that the old Petclinic WAR is replaced by the new Petclinic WAR in your
  97. servlet container.
  98. Finally, start your container and try to visit the home page.
  99. Your request should be intercepted and you will be forced to login.
  100. You can now log in using the usernames and passwords that are documented at the end
  101. of applicationContext-security-ns.xml file.
  102. ** Log out
  103. To make it easier to experiment with the application, users should be able to log out of the application.
  104. Edit %spring%\samples\petclinic\war\WEB-INF\jsp\footer.jsp. Add a new "logout" link, as shown:
  105. +------------------------------------------------------
  106. <table class="footer">
  107. <tr>
  108. <td><a href="<c:url value="/welcome.do"/>">Home</a></td>
  109. <td style="text-align:right;color:silver">PetClinic :: a Spring Framework demonstration</td>
  110. <td align="right"><img src="<c:url value="/images/springsource-logo.png"/>"/></td>
  111. <td align="right"><a href="<c:url value="/j_spring_security_logout"/>">Logout</a></td>
  112. </tr>
  113. </table>
  114. +------------------------------------------------------
  115. * Optional Bonus: Securing the Middle Tier
  116. Whilst you've now secured your web requests, you might want to stop users
  117. from being able to add clinic visits unless authorized. We'll make it so
  118. you need to hold ROLE_SUPERVISOR to add a clinic visit.
  119. ** protect-pointcut
  120. Finally, we need to declare a protect-pointcut that will hold the security restriction.
  121. Inside %spring%\samples\petclinic\war\WEB-INF\applicationContext-security.xml, update
  122. the global-method-security tag as follows:
  123. +------------------------------------------------------
  124. <global-method-security secured-annotations="enabled">
  125. <protect-pointcut expression="execution(* org.springframework.samples.petclinic.Clinic.storeVisit(..))"
  126. access="ROLE_SUPERVISOR"/>
  127. </global-method-security>
  128. +------------------------------------------------------
  129. Redeploy your web application.
  130. Login as "peter" that does not have the "ROLE_SUPERVISOR" role.
  131. * Click on "Find owners"
  132. * Keep the "last name" field blank and validate
  133. * Select one owner in the list
  134. * Click on "add visit"
  135. * Add a description and validate
  136. Access should be denied.
  137. Now log out and try "rod", who has ROLE_SUPERVISOR. It should be working.
  138. ** The "sec" tag-library
  139. To clean things up a bit, you might want to wrap up by hiding the "add visit" link
  140. unless you are authorized to use it. Spring Security provides a tag library to help
  141. you do that. Edit %spring%\samples\petclinic\war\WEB-INF\jsp\owner.jsp (please
  142. make sure that you are opening owner.jsp, not owners.jsp !!). Add
  143. the following line to the top of the file:
  144. +------------------------------------------------------
  145. <%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
  146. +------------------------------------------------------
  147. Next, scroll down and find the link to "add visit". Modify it as follows:
  148. +------------------------------------------------------
  149. <sec:authorize ifAllGranted="ROLE_SUPERVISOR">
  150. <form method="GET" action="<c:url value="/addVisit.do"/>" name="formVisitPet${pet.id}">
  151. <input type="hidden" name="petId" value="${pet.id}"/>
  152. <p class="submit"><input type="submit" value="Add Visit"/></p>
  153. </form>
  154. </sec:authorize>
  155. +------------------------------------------------------
  156. * What now?
  157. These steps can be applied to your own application. We suggest that you review
  158. the "Suggested Steps" for getting started with Spring
  159. Security on the web site. The suggested steps are optimized for learning Spring Security quickly
  160. and applying it to your own projects. This section also includes realistic time estimates
  161. for each step so you can plan your integration activities.