---------------------------------------------
Tutorial: Adding Security to Spring Petclinic
---------------------------------------------
Tutorial: Adding Security to Spring Petclinic
* Preparation
To complete this tutorial, you will require a servlet container (such as Tomcat)
and a general understanding of using Spring without Spring Security. The Petclinic
sample itself is part of Spring and should help you learn Spring. We suggest you
only try to learn one thing at a time, and start with Spring/Petclinic before
Spring Security.
You will also need to download:
* {{{http://www.springframework.org/download}Spring 2.5.5 with dependencies ZIP file}}
* {{{http://www.springframework.org/download}Spring Security 2.0.4}}
Unzip both files. After unzipping Spring Security, you'll need to unzip the
spring-security-sample-tutorial-2.0.4.war file, because we need some files that are
included within it. After unzipping the war file, you will see a folder called
spring-security-samples-tutorial-2.0.4.
In the code below, we'll refer to the respective unzipped
locations as %spring% and %spring-sec-tutorial% (with the later variable
referring to the unzipped WAR, not the original ZIP). There is no need to setup
any environment variables to complete the tutorial.
* 1st part: Run the Petclinic application without Spring Security
In order to make sure that you work in a stable environment, we will first
set up the Petclinic application, without Spring Security.
** Start Petclinic's database
Start the Hypersonic server:
+------------------------------------------------------
cd %spring%\samples\petclinic\db\hsqldb
server
+------------------------------------------------------
Insert some data:
+------------------------------------------------------
cd %spring%\samples\petclinic
ant setupDB
+------------------------------------------------------
** Build and deploy the Petclinic WAR file
Use Petclinic's Ant build script and deploy to your servlet container:
+------------------------------------------------------
cd %spring%\samples\petclinic
build warfile
copy dist\petclinic.war %TOMCAT_HOME%\webapps
+------------------------------------------------------
Finally, start your container and try to visit the petclinic home page.
You are now able to browse the whole application without any authentication needed
* Second part: set up Spring security
** Add required Spring Security files to Petclinic
We now need to put some extra files into Petclinic.
The following example is based on Windows MS-DOS. It only involves file copy.
We believe you can adapt it easily on any operating system.
+------------------------------------------------------
copy %spring-sec-tutorial%\WEB-INF\applicationContext-security-ns.xml %spring%\samples\petclinic\war\WEB-INF
copy %spring-sec-tutorial%\WEB-INF\lib\spring-security-core-2.0.4.jar %spring%\samples\petclinic\war\WEB-INF\lib
copy %spring-sec-tutorial%\WEB-INF\lib\spring-security-core-tiger-2.0.4.jar %spring%\samples\petclinic\war\WEB-INF\lib
copy %spring-sec-tutorial%\WEB-INF\lib\spring-security-acl-2.0.4.jar %spring%\samples\petclinic\war\WEB-INF\lib
copy %spring-sec-tutorial%\WEB-INF\lib\spring-security-taglibs-2.0.4.jar %spring%\samples\petclinic\war\WEB-INF\lib
copy %spring-sec-tutorial%\WEB-INF\lib\commons-codec-1.3.jar %spring%\samples\petclinic\war\WEB-INF\lib
+------------------------------------------------------
** Configure Petclinic's files
Edit %spring%\samples\petclinic\war\WEB-INF\web.xml. The "contextConfigLocation" specifies Spring configuration files that should be used
by the petclinic application. Locate the "contextConfigLocation" parameter and add a new line into
the existing param-value. Now that we are using Spring Security, It should also declare
applicationContext-security.xml (Spring config file for Spring Security).
The resulting block will look like this:
+------------------------------------------------------
| ">Home | PetClinic :: a Spring Framework demonstration | ">Logout |