petclinic-tutorial.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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>Background requirements</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. <h2>Download</h2>
  15. <ul>
  16. <li>Spring 2.0 M4 with dependencies ZIP file</li>
  17. <li>Acegi Security 1.0.0</li>
  18. </ul>
  19. <p>
  20. Unzip both files. After unzipping Acegi Security, you'll need to unzip the
  21. acegi-security-sample-tutorial.war file, because we need some files that are
  22. included within it. In the code below, we'll refer to the respective unzipped
  23. locations as %spring% and %acegi% (with the latter variable referring to the
  24. unzipped WAR, not the original ZIP). There is no need to setup any environment
  25. variables to complete the tutorial.
  26. </p>
  27. <h2>Setup database</h2>
  28. <p>Start the Hypersonic server (this is just normal Petclinic configuration):
  29. <pre>
  30. cd %spring%\samples\petclinic\db\hsqldb
  31. server
  32. </pre>
  33. </p>
  34. <p>
  35. Insert some data (again, normal Petclinic configuration):
  36. <pre>
  37. cd %spring%\samples\petclinic
  38. build setupDB
  39. </pre>
  40. </p>
  41. <h2>Setup Petclinic's web.xml</h2>
  42. <p>Edit %spring%\samples\petclinic\war\WEB-INF\web.xml and insert the following block of code.
  43. <pre>
  44. &lt;filter&gt;
  45. &lt;filter-name&gt;Acegi Filter Chain Proxy&lt;/filter-name&gt;
  46. &lt;filter-class&gt;org.acegisecurity.util.FilterToBeanProxy&lt;/filter-class&gt;
  47. &lt;init-param&gt;
  48. &lt;param-name&gt;targetClass&lt;/param-name&gt;
  49. &lt;param-value&gt;org.acegisecurity.util.FilterChainProxy&lt;/param-value&gt;
  50. &lt;/init-param&gt;
  51. &lt;/filter&gt;
  52. &lt;filter-mapping&gt;
  53. &lt;filter-name&gt;Acegi Filter Chain Proxy&lt;/filter-name&gt;
  54. &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  55. &lt;/filter-mapping&gt;
  56. </pre>
  57. Next, locate the "contextConfigLocation" parameter, and add a new line into the existing param-value.
  58. The resulting block will look like this:
  59. <pre>
  60. &lt;context-param&gt;
  61. &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
  62. &lt;param-value&gt;
  63. /WEB-INF/applicationContext-jdbc.xml
  64. /WEB-INF/applicationContext-acegi-security.xml
  65. &lt;/param-value&gt;
  66. &lt;/context-param&gt;
  67. </pre>
  68. </p>
  69. <h2>Add the necessary files</h2>
  70. <p>
  71. We now need to put some extra files into Petclinic. The following commands should work:
  72. <pre>
  73. copy %acegi%\acegilogin.jsp %spring%\samples\petclinic\war
  74. copy %acegi%\WEB-INF\users.properties %spring%\samples\petclinic\war\WEB-INF
  75. copy %acegi%\WEB-INF\applicationContext-acegi-security.xml %spring%\samples\petclinic\war\WEB-INF
  76. copy %acegi%\WEB-INF\lib\acegi-security-1.0.0.jar %spring%\samples\petclinic\war\WEB-INF\lib
  77. copy %acegi%\WEB-INF\lib\oro-2.0.8.jar %spring%\samples\petclinic\war\WEB-INF\lib
  78. copy %acegi%\WEB-INF\lib\commons-codec-1.3.jar %spring%\samples\petclinic\war\WEB-INF\lib
  79. </pre>
  80. </p>
  81. <p>
  82. To make it easier to experiment with the application, let's edit
  83. %spring%\samples\petclinic\war\WEB-INF\jsp\footer.jsp. Add a new "logout" link, as shown:
  84. <pre>
  85. &lt;table style="width:100%"&gt;&lt;tr&gt;
  86. &lt;td&gt;&lt;A href="&lt;c:url value="/welcome.htm"/&gt;"&gt;Home&lt;/A&gt;&lt;/td&gt;
  87. &lt;td&gt;&lt;A href="&lt;c:url value="/j_acegi_logout"/&gt;"&gt;Logout&lt;/A&gt;&lt;/td&gt;
  88. &lt;td style="text-align:right;color:silver"&gt;PetClinic :: a Spring Framework demonstration&lt;/td&gt;
  89. &lt;/tr&gt;&lt;/table&gt;
  90. </pre>
  91. </p>
  92. <h2>Modify the allowed URLs</h2>
  93. <p>
  94. Our last step is to specify which URLs require authorization and which do not. Let's
  95. edit %spring%\samples\petclinic\war\WEB-INF\applicationContext-acegi-security.xml.
  96. Scroll to the bottom and locate the bean definition for FilterSecurityInterceptor.
  97. Edit its objectDefinitionSource property so that it reflects the following:
  98. <pre>
  99. &lt;property name="objectDefinitionSource"&gt;
  100. &lt;value&gt;
  101. CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
  102. PATTERN_TYPE_APACHE_ANT
  103. /acegilogin.jsp=IS_AUTHENTICATED_ANONYMOUSLY
  104. /**=IS_AUTHENTICATED_REMEMBERED
  105. &lt;/value&gt;
  106. &lt;/property&gt;
  107. </pre>
  108. </p>
  109. <h2>Build and deploy the Petclinic WAR file</h2>
  110. <p>
  111. Use the Ant build and deploy to your servlet container:
  112. <pre>
  113. cd %spring%\samples\petclinic
  114. build warfile
  115. copy dist\petclinic.war %TOMCAT_HOME%\webapps
  116. </pre>
  117. </p>
  118. <p>Finally, start your container and try to visit the home page.
  119. Your request should be intercepted and you will be forced to login.</p>
  120. <h2>What now?</h2>
  121. <p>
  122. These steps can be applied to your own application. Although we do suggest
  123. that you visit <a href="http://acegisecurity.org">http://acegisecurity.org</a>
  124. and in particular review the "Suggested Steps" for getting started with Acegi
  125. Security.</p>
  126. </body>
  127. </html>