Customize the Spring security to permit and deny the specific requests

Hey guys in this post, we will discuss customizing the spring security to permit and deny the specific requests. This is the continuation of the previous post, make sure to check the previous post before proceeding.

Complete example

Let’s create a step-by-step spring boot project and add spring security to the application

Create spring boot project

There are many different ways to create a spring boot application, you can follow the below articles to create one –

>> Create spring boot application using Spring initializer
>> Create spring boot application in Spring tool suite [STS]
>> Create spring boot application in IntelliJ IDEA

Add maven dependencies

Open pom.xml and add the following dependencies –

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi=""
		<relativePath/> <!-- lookup parent from repository -->
	<description>Spring security project</description>




spring-boot-starter-web dependency for building web applications using Spring MVC. It uses the tomcat as the default embedded container. spring-boot-starter-security dependency, which will help to implement spring security.

Change the username and password

Spring Security also provides an option to change the default username and password with the help of the file

inside the properties file, we are adding our own username and password to access the URIs instead of the one provided by spring security. By adding these two properties, now we have customized the username and password of the spring security framework.

Once we added these two properties, spring-security will no longer generate the password inside the console when we run the app.

Create a Rest controller

Create inside the in.bushansirgur.springboot.controller package and add the following content

package in.bushansirgur.springboot.controller;

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

public class HomeController {
	public String showHomePage () {
		return "displaying the home page contents";
	public String protectedPage () {
		return "displying the protected page contents";

We have created two handler methods showHomePage(), which is mapped to /home, anyone can access this URI and protectedPage(), which is mapped to /protected, only authorized users can access this URI.

Create a configuration class

Let’s customize the spring security to permit and deny the specific URIs based on the requirement. Create inside the in.bushansirgur.springboot.config package and add the following content.

package in.bushansirgur.springboot.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

public class ProjectSecurityconfig extends WebSecurityConfigurerAdapter{

	protected void configure(HttpSecurity http) throws Exception {

So anytime if we want to customize the spring security then we need to extend the configuration class with WebSecurityConfigurerAdapter which will provide specific methods which will override in the configuration class based on the requirement

Here we are overriding the configure(HttpSecurity http) method, which takes HttpSecurity as an argument. We can authenticate the specific URIs with authenticated() method and permitAll() method will allow the requests to access the specific URIs without authenticated. We can specify the URIs using antMatchers() method. With this in place, now we can run our application.

Run the app

Run the application using the below maven command –

mvn spring-boot:run

Open the browser and enter the following URL –



Once the user enters the correct credentials, then the user can see the contents.

That’s it for this post, if you like this post, consider sharing this with your friends and colleagues or you can share it on any of the social media. Thank you, I will see you in the next post.

Bushan Sirgur

Hey guys, I am Bushan Sirgur from Banglore, India. Currently, I am working as an Associate project in an IT company.

Leave a Reply