Search Tutorials

Spring Boot Security - Custom Login Page Example | JavaInUse

Spring Boot Form Security Example - Creating a custom Login Page

In a previous post we had implemented Spring Boot Security for a Form Application.
It made use of the default Spring Login Page. In this tutorial we will adding our own custom login web page. On log out we will be directed to this login page with some logout message.

Spring Boot Security - Table Of Contents

Spring Boot + Simple Security Configuration Spring Boot Form Security Login Hello World Example Spring Boot Security - Custom Login Page Example Spring Boot Security - JDBC Authentication Example Spring Boot Security - Creating Users Programmatically Using JdbcUserDetailsManager Spring Boot Security - Password Encoding Using Bcrypt Spring Boot Security - Enabling CSRF Protection Spring Boot Security - Authentication Handler Example Spring Boot Security - Introduction to OAuth Spring Boot OAuth2 Part 1 - Getting The Authorization Code Spring Boot OAuth2 Part 2 - Getting The Access Token And Using it to Fetch Data.


This tutorial is explained in the below Youtube Video.

Lets Begin-

We will be modifying the code we developed in the previous Spring Boot Security for a Form Application
Maven Project will be as follows-

The Custom login page login.jsp is as follows-
<%@ taglib prefix="spring" uri="" %>
<%@ taglib prefix="c" uri="" %>
<%@ taglib prefix="form" uri="" %>

<c:set var="contextPath" value=""/>

<!DOCTYPE html>
<html lang="en">
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <meta name="description" content="">
    <meta name="author" content="">

    <title>Log in with your credentials</title>

    <link rel="stylesheet" href="">


<div class="container">

    <form method="POST" action="/login" class="form-signin">
        <h2 class="form-heading">Log in</h2>

        <div class="form-group ">
            <input name="username" type="text" class="form-control" placeholder="Username"
            <input name="password" type="password" class="form-control" placeholder="Password"/>

            <button class="btn btn-lg btn-primary btn-block" type="submit">Log In</button>


<!-- /container -->
<script src=""></script>
<script src=""></script></body>

Next in the Controller, add a GET method which returns the custom login page with RequestMapping of /login.
package com.javainuse.controllers;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.javainuse.model.Employee;
import com.javainuse.service.EmployeeService;

public class EmployeeController {

	EmployeeService employeeService;

	public ModelAndView firstPage() {
		return new ModelAndView("welcome");

	@RequestMapping(value = "/addNewEmployee", method = RequestMethod.GET)
	public ModelAndView show() {
		return new ModelAndView("addEmployee", "emp", new Employee());

	@RequestMapping(value = "/addNewEmployee", method = RequestMethod.POST)
	public ModelAndView processRequest(@ModelAttribute("emp") Employee emp) {
		List<Employee> employees = employeeService.getAllEmployees();
		ModelAndView model = new ModelAndView("getEmployees");
		model.addObject("employees", employees);
		return model;

	public ModelAndView getEmployees() {
		List<Employee> employees = employeeService.getAllEmployees();
		ModelAndView model = new ModelAndView("getEmployees");
		model.addObject("employees", employees);
		return model;
	@RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login(Model model, String error, String logout) {
        if (error != null)
            model.addAttribute("errorMsg", "Your username and password are invalid.");

        if (logout != null)
            model.addAttribute("msg", "You have been logged out successfully.");

        return "login";


Finally modify the Spring Security configuration where we specify the custom Login Page to be called during login.
package com.javainuse.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

public class EmployeeSecurityConfiguration extends WebSecurityConfigurerAdapter {

	public void configure(WebSecurity web) throws Exception {

	protected void configure(HttpSecurity http) throws Exception {
		http.authorizeRequests().antMatchers("/").permitAll().antMatchers("/welcome").hasAnyRole("USER", "ADMIN")
				.antMatchers("/getEmployees").hasAnyRole("USER", "ADMIN").antMatchers("/addNewEmployee")


	public void configureGlobal(AuthenticationManagerBuilder authenticationMgr) throws Exception {
				.withUser("javainuse").password("javainuse").authorities("ROLE_USER", "ROLE_ADMIN");


These are the only changes required.
  • Go to localhost:8080/welcome, we will be redirected to the custom login page.
  • Enter wrong password.
  • On log out we get the screen as follows-

    Download Source Code

    Download it -
    Spring Boot Security - Custom Login Page Example