Integrating Spring Security into a Spring Boot application

Spring Security is a powerful framework that provides authentication, authorization, and other security features for Java applications. In this article, we will explore how to integrate Spring Security into a Spring Boot application, allowing us to easily secure our application and protect sensitive resources.


Before we begin, make sure you have the following tools installed:

  • Java Development Kit (JDK)
  • Spring Boot
  • Maven or Gradle (build tools)
  • IDE (Eclipse, IntelliJ, or any other of your choice)


To get started, create a new Spring Boot application. You can use the Spring Initializr ( or your IDE's built-in project creation feature to generate the application structure.

Adding Spring Security dependencies

In your project's build configuration file (pom.xml for Maven or build.gradle for Gradle), add the Spring Security dependency. For Maven, add the following to your dependencies section:


For Gradle, add the following to your dependencies section:

implementation 'org.springframework.boot:spring-boot-starter-security'

Remember to sync or reload your project dependencies after making these changes.

Configuring Spring Security

Once the dependency is added, we need to configure Spring Security to define our security rules and settings. Create a new Java class, SecurityConfig, and annotate it with @Configuration and @EnableWebSecurity:

public class SecurityConfig extends WebSecurityConfigurerAdapter {


This class will serve as our main configuration class for Spring Security. Extend WebSecurityConfigurerAdapter to get a default configuration and override the necessary methods to customize the security behavior.

For example, to define a simple in-memory user with a username and password, add the following method:

protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        .password("{noop}password") // Add {noop} to specify that the password is not encoded

This method configures the authentication manager to use an in-memory user with the username "user" and password "password". The roles method specifies the user's roles or authorities.

Additionally, to enable login form-based authentication, add the following configuration:

protected void configure(HttpSecurity http) throws Exception {
            .antMatchers("/login", "/css/**", "/js/**").permitAll()

This configuration allows access to the "/login" page and any CSS or JavaScript resources without authentication. All other requests require authentication. The formLogin method sets up the login form and the logout method enables logout functionality.

Running the application

With the security configuration in place, we can run our Spring Boot application and test the security features. Visit the "/login" page in your browser, and you should see the login form. Enter the username and password we configured and submit the form. If everything is set up correctly, you should be redirected to the default Spring Boot homepage ("/") with access to protected resources.


Integrating Spring Security into a Spring Boot application allows us to easily add robust security features to our application. In this article, we covered the basic steps of adding Spring Security as a dependency, configuring security rules, and testing the application. With this foundation, you can explore more advanced features and configurations provided by Spring Security to further enhance the security of your Spring Boot application.

noob to master © copyleft