Securing a Spring Boot JSP Application usng Spring Securiy

In this article we will see How to add Spring Security support in a Spring Boot JSP application. We will use the HTTP Basic authentication to secure a spring boot web application.

Project structure

Following is the project structure, indicating where to put jsps and static resources like css and js files, this project structure shows standard way of creating a spring boot web project.

Dependencies (pom.xml)

In order to create a Spring Boot JSP application with Spring Security we have to add spring-boot-starter-tomcat and tomcat-embed-jasper in pom.xml, tomcat-embed-jasper is required to compile JSPs.

spring-boot-starter-security is to be added to pom.xml to make it support spring security configuration and features.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns=""






Spring boot application launch class

SpringBootServletInitializer" is used to run a SpringApplication from a traditional WAR deployment, it binds Servlet, Filter and ServletContextInitializer beans from the application context to the server.


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.ComponentScan;

 * @author nagesh.chauhan
public class App extends SpringBootServletInitializer {

	protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
		return application.sources(App.class);

	public static void main(String[] args) {, args);
Here, @SpringBootApplication does all the magic, a class having main() and annotated with @SpringBootApplication is called Spring boot application launch class.

Spring Security Configuration

To add spring security support to a Spring Boot JSP project we need to add a configuration class extending WebSecurityConfigurerAdapter.

Here AuthenticationManagerBuilder is used to define in memory user and password with roles and HttpSecurity is where we define rules related to application security.

According to configuration, any url starting from "/home" will not require any authentication while urls starting from "/user" and "/admin" will require authentication as per their respective roles.


import org.springframework.context.annotation.Configuration;

public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {

	protected void configure(AuthenticationManagerBuilder auth) throws Exception {
				.withUser("admin").password("{noop}adminpassword").roles("USER", "ADMIN");


	protected void configure(HttpSecurity http) throws Exception {
				.hasAnyRole("USER", "ADMIN").antMatchers("/admin/**").hasRole("ADMIN").and().csrf().disable().formLogin()

Controller class

HomeController is annotated with @Controller, telling spring that the class will handle incoming requests, Model parameter is used to send data from Controller to JSP and "index" returned as a string is name of JSP.

package com.tb.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

public class HomeController {

	@GetMapping({ "/", "/home" })
	private String home(Model model) {
		model.addAttribute("message", "Hello World !");
		return "index";
	@GetMapping({ "user" })
	private String user(Model model) {
		model.addAttribute("message", "Hello User !");
		return "index";
	@GetMapping({ "admin" })
	private String admin(Model model) {
		model.addAttribute("message", "Hello Admin !");
		return "index";

Add JSPs and Static resources

<!DOCTYPE html>
<%@ taglib prefix="spring" uri=""%>
<%@ taglib prefix="c" uri=""%>
<title>Spring Security Basic AUTH Example</title>
<link href="/css/style.css" rel="stylesheet">
<script src="/js/app.js"></script>
	<h2>Spring Security Basic AUTH Example</h2>


html {
	display: table;
	width: 100%;

body {
	display: table-cell;
	text-align: center;
	vertical-align: middle;

    console.log("Hello World!");

Changes in

We have added "prefix" and "suffix" properties of spring.mvc.view, this will map jsp names passed in controller to fully qualified named respective to webapp folder.

spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp

Run and Test the application

Here we are all done with "Creating a spring boot jsp application supporting sprinig security", do a maven clean-install and you will find .war file created in "target" folder, this file can be used to deploy on externam application server like tomcat.

While using IDE like Eclipse, we can also run the application from using run as > Java Application and the application will start on default 8080 port.

If everything goes right you will see a JSP view as shown below on both http://localhost:8080/ and "http://localhost:8080/home":

1) Not secured URL


2) Secured URL


Use user as username and userpassword as password, after authentication you will see a page like this.

3) Access Denied

User: "user" have ROLE_USER only, if we try to access /admin/* urls with this user there will be a 403 - Access Denied error because "/admin/*" urls require ROLE_ADMIN.


We are done will "Integrating Spring security with Spring boot and JSP", in upcoming applications we will see more about "Spring Boot" and "Spring Framework".

Download complete project code from: GitHub

About The Author

Nagesh Chauhan

Nagesh Chauhan has 8+ years of software design and development experience in variety of technologies like - Core Java, Java 8 (Streams, Lambda), J2EE (Servlet, JSP), Spring Framework (MVC, IOC, JDBC, SECURITY etc), Spring Boot and Microservices, Kafla, Redis, Cassandra and Spark.