Spring Security configuration

To add Spring Security to our Spring web application, we need to perform a basic Spring Security setup. To do this, follow these steps:

  1. Add Spring JARs or Spring Security dependencies.
  2. Update web.xml with springSecurityFilterChain.
  3. Create a Spring Security configuration file.

Spring Security setup

We can either download and add Spring Security JARs to classpath or we can provide dependencies to Maven.

Adding JARs to the classpath

There are three important JARs that we need for Spring Security. These can be downloaded from the Spring website and are as follows (the version should match other Spring JARs used in the project):

  • spring-security-config-3.X.X.RELEASE.jars: This contains support for Spring Security's XML namespace
  • spring-security-core-3.X.X.RELEASE.jars: This provides the essential Spring Security library
  • spring-security-web-3.X.X.RELEASE.jars: This provides Spring Security's filter-based web security support

If we have developed a Maven application, then we need to update pom.xml.

Spring Security dependencies – pom.xml

Update dependencies to Maven. We have spring-security-core, spring-security-web, and spring-security-config:

<properties>
   <spring.security.version>3.1.4.RELEASE</spring.security.version>
</properties>

<!-- Spring Security -->
<dependency>
   <groupId>org.springframework.security</groupId>
   <artifactId>spring-security-core</artifactId>
   <version>${spring.security.version}</version>
</dependency> 

<dependency>
   <groupId>org.springframework.security</groupId>
   <artifactId>spring-security-web</artifactId>
   <version>${spring.security.version}</version>
</dependency>
 
<dependency>
   <groupId>org.springframework.security</groupId>
   <artifactId>spring-security-config</artifactId>
   <version>${spring.security.version}</version>
</dependency>

Namespace configuration

In the Spring configuration file, we need to add one more entry to schema, which is related to Spring Security and the corresponding schemaLocation and their xsd, which lives in Spring JARs. The security prefixed elements go here.

In the SpringDispatcher-servlet.xml file, you'll find the following code:

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:security="http://www.springframework.org/schema/security"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
   http://www.springframework.org/schema/security
   http://www.springframework.org/schema/security/spring-security-3.1.xsd">
</beans>
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.77.131