To add Spring Security to our Spring web application, we need to perform a basic Spring Security setup. To do this, follow these steps:
web.xml
with springSecurityFilterChain
.We can either download and add Spring Security JARs to classpath or we can provide dependencies to Maven.
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 namespacespring-security-core-3.X.X.RELEASE.jars
: This provides the essential Spring Security libraryspring-security-web-3.X.X.RELEASE.jars
: This provides Spring Security's filter-based web security supportIf we have developed a Maven application, then we need to update 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>
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>