In spring boot security, we will see how to configure CSRF – Cross-Site Request Forgery attack in spring Boot Security step by step. Cross-Site Request Forgery attack is an attack executes unwanted calls on a web application without intervention of the end user.The end user is forced to execute these calls that corrupt the users data in the database or show unwanted information in the browser.

What is CSRF attack?

CSRF – Cross-Site Request Forgery attack is an attack that forces the end user to make an unwanted calls to the web application servers where the end user is already authenticated. When a user is authenticated with a website, the session information is created and stored in both server and browser. If any call is made in the browser to the website, all calls will be executed without any validation. This is called CSRF attack.

Step 1 – Spring Boot Security taglibs

In the spring boot application, add spring boot security and spring boot security tag library dependency in the pom.xml file. These dependency adds the spring boot security module and spring boot security tag libraries.


Step 2 – Add Spring boot CSRF token in Jsp

The CSRF token must be included in all the post forms in the jsp or template files. If it is a json calls, add the token in the header. The following example shows how to add CSRF toke in jsp file


<%@ taglib prefix="sec" uri="" %>
<h1>Welcome to Spring Boot Security</h1>  
<h2>Login Page</h2>    
<form method="POST" action="/login">  
    User Name : <input type="text" name="username" value="user"/><br><br>  
    Password  : <input type="password" name="password" value="password"/><br><br> 
       <sec:csrfInput />   
     <input type="submit" name="submit"/>

Step 3 – Remove the CSRF disable code

If any csrf disable code added in the spring boot security configuration, remove all the csrf disable code. The example below shows the commented csrf disable code.

package com.yawintutor; 
import org.springframework.context.annotation.Configuration; 

public class SpringBootSecurityConfiguration extends WebSecurityConfigurerAdapter {      
      public void configure(AuthenticationManagerBuilder auth) throws Exception {

      public void configure(HttpSecurity http) throws Exception {

Leave a Reply