Spring Boot Login Page tutorial

last modified July 6, 2020

Spring Boot Login Page tutorial shows how to work with a default login page. Spring security secures all HTTP endpoints by default. A user has to login in a default HTTP form.

To enable Spring Boot security, we add spring-boot-starter-security to the dependencies.

Spring Boot Login Page example

The following example shows how to set up a simple login page in a Spring Boot application.

│   ├───java
│   │   └───com
│   │       └───zetcode
│   │           │   Application.java
│   │           └───controller
│   │                   MyController.java
│   └───resources
│           application.properties

This is the project structure of the Spring Boot application.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"









This is the Maven build file. We have starters for web and security.

logging.pattern.console=%d{dd-MM-yyyy HH:mm:ss} %magenta([%thread]) %highlight(%-5level) %logger.%M - %msg%n

In the application.properties file, we turn off the Spring Boot banner and configure the console logging pattern.

package com.zetcode.controller;

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

public class MyController {

    public String home() {

        return "This is home page";

We have a simple home page.

We run the application and navigate to localhost:8080. We are redirected to the http://localhost:8080/login page.

17-06-2019 17:48:45 [main] INFO  org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration.getOrDeducePassword -

Using generated security password: df7ce50b-abae-43a1-abe1-0e17fd81a454

In the console, we can see a password generated for a default user called user. These credentials are provided to the authentication form.

Login form
Figure: Login form

Spring uses Bootstrap to define the UI.

spring.security.user.name = admin
spring.security.user.password = s$cret

With these two options, we can have a new user name and password. The autogenerated user is turned off with these settings.

In this tutorial, we have worked with a default login form.

List all Spring Boot tutorials.