Book Image

SPRING COOKBOOK

Book Image

SPRING COOKBOOK

Overview of this book

Table of Contents (19 chapters)
Spring Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Enabling Spring Security


To enable Spring Security, you need to add some Maven dependencies. You also need to create some configuration classes.

How to do it…

Here are the steps to enable Spring Security:

  1. Add the Maven dependencies for Spring Security in pom.xml. Note that the version number is different from Spring Core:

    <dependency> 
        <groupId>org.springframework.security</groupId> 
        <artifactId>spring-security-web</artifactId> 
        <version>3.2.5.RELEASE</version> 
    </dependency> 
       
    <dependency> 
        <groupId>org.springframework.security</groupId> 
        <artifactId>spring-security-config</artifactId> 
        <version>3.2.5.RELEASE</version> 
    </dependency> 
  2. Create a class for the Spring Security configuration:

    @Configuration
    @EnableWebSecurity
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
    }
  3. Declare the configuration class in the ServletInitializer class:

    public class ServletInitializer...