Points To Remember


  • Add dependency of spring security
  • Add custom username password in application.properties
  • A unique password is generated each time application is started if no authentication process is specified.
  • You can configure your own authentication  providers, managers, filters, entry points, tokens etc as required.

Getting started with Spring Security and Spring Boot

In order to apply Spring Security to a Spring Boot application, firstly you need to add the dependency in the application as follows

  • In Maven you can do it as follows.
    <dependencies>
    <dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-web</artifactId>
    <version>4.0.3.RELEASE</version>
    </dependency>
    </dependencies>
  • In Gradle you can do it as follows.
    dependencies {
    compile 'org.springframework.security:spring-security-web:4.0.3.RELEASE'
    }
Your initial project may look like as shown in the image

Create a controller HomeController as in out case and just add a /home mapping for the application.

package com.ekiras.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* Created by ekansh on 18/11/15.
*/
@RestController
@RequestMapping(value = "/")
public class HomeController {

@RequestMapping(value = "home")
public String home(){
return "Hello World";
}
}

Now, just run the application using command

gradle run

Now open the application in browser, you will get a popup to enter the user name and password. This is the default security provided by spring security.

You can login using the default

  • username - user
  • password - generated at application startup as shown in image below


Note : A unique password is generated each time the application is started.

You can also create a custom username and password by specifying it in the application.properties as follows

security.user.name=ekansh
security.user.password=password

Now you can login with the credentials

  • username - ekansh
  • password - password