Spring framework provides a very nice way of securing your web application.
We will go through its configuration step by step:
We will be using maven for dependency management.
1. Put in the spring security dependencies in your POM file.
Note:
The contextLoaderListener is the parent context (root context) where as the DispatcherServletContext is a child context. The Child Context beans Can Access the beans defined int he parent context but the parent context beans Cannot Access the beans defined int he child context.
We will go through its configuration step by step:
We will be using maven for dependency management.
1. Put in the spring security dependencies in your POM file.
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>3.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>3.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>3.1.0.RELEASE</version>
</dependency>
Note:
- Spring Security 3.1 builds with spring 3.0
- so when you try to include the maven dependencies for Spring Security 3.1, maven automatically brings in spring 3.0 library files
- If you have to use Spring 3.0 in your application then its fine BUT
- if you have to use Spring 3.1 then you have to either
- Declare Spring 3.1 dependencies explicitly OR
- use maven Dependency Management Tool
Step 2: SETTING UP SPRING CONFIGURATION XML FILE
The next step is to create a spring XML file configuration file for spring security:
- The above is the minimal configuration to implement a standard security for an application.
- The above configuration uses an xml namespace (http://www.springframework.org/schema/security) to define different security related tags.
Note: Whats in this config?
- <http> element creates a servlet filter - which ensures that the current user is associated with the appropriate role.
- In the example above the "servlet filter" will ensure that the user "user@example.com" is associated with "ROLE_USER"
- The name of role is ARBITRARY - so we can create as many roles as we want and give them any names.
- <authentication-manager> is how spring security authenticates the user - for this example above we are using an in-memory data store to match username and password which is user1@example.com and user1
Step 3: UPDATE WEB.XML
- The next step is to update the web.xml file
- ContextLoaderListener - The first update in the web.xml is to add ContextLoaderListener which starts the and stops the root ApplicationContext Interface of Spring application. [This goes into any application that uses Spring anyway] - CotextLoaderListener determines what configuration to load by looking at <context-param> tag for contextConfigLocation - See Image Below
Now the security.xml will be loaded when the application starts.
- For loading multiple files we could use /WEB-INF/spring/*.xml
Note:
TIP: <filter-mapping> elements in web.ml are considered in the order they are mentioned in the web.xml
springSecurityFilterChain
DelegatingFilterProxy
The org.springramework.web.filter.DelegatingFilterProxy is a servlet filter which delegates all of its work to a Spring bean (From the Root Application Context) that must implement javax.servlet.Filter
By default the bean is looked up by name using the value of <filter-name> we must ensure we use springSecurityFilterChain as value of <filter-name> ??
As we said that DelegatingFilterProxy will delegate its work to something else (a servlet filter) , what something else is called FilterChainProxy which can apply filters to the request.
Everything upto now will work so if you just run the application you can see the login screen and if you try to login using username and password described in the security config file you will be able to successfully login.