How do I send spring csrf token from Postman rest client?

SpringRestHeaderCsrfPostman

Spring Problem Overview


I have csrf protection in spring framework. So in each request I send csrf token in header from ajax call, which is perfectly working.

<meta name="_csrf" content="${_csrf.token}"/>
<meta name="_csrf_header" content="${_csrf.headerName}"/>

var token = $("meta[name='_csrf']").attr("content");
var header = $("meta[name='_csrf_header']").attr("content");

In ajax

beforeSend: function(xhr) {
                xhr.setRequestHeader(header, token),
                xhr.setRequestHeader("username", "xxxx1"),
                xhr.setRequestHeader("password", "password")
            }

I haven't any idea to generate csrf token and include in header section of Postman Rest Client ? Would you please help me to send csrf token from Postman Rest Client? enter image description here

Spring Solutions


Solution 1 - Spring

The Easiest way to do this consistently so you don't have to get the token each time:

NOTE:you need to install PostMan Interceptor and activate it to have access to the browsers cookies

  1. Create a new environment so environment variables can be stored

enter image description here

  1. Create a login method with a test to store the XSRF cookie in an environment variable, in the test tab post this code

    //Replace XSFR-TOKEN with your cookie name
    var xsrfCookie = postman.getResponseCookie("XSRF-TOKEN");
    postman.setEnvironmentVariable("xsrf-token", xsrfCookie.value);
    

EDIT For anyone using the 5.5.2 postman or later you will also have to decode the cookie, and they have also provided alternative ways to obtain cookies as @Sacapuces points out

pm.environment.set("xsrf-token", decodeURIComponent(pm.cookies.get("XSRF-TOKEN")))

Now you will have an environment variable with xsrf-token in it.

  1. Save your login method

  2. Create the new post you want to create and in the headers add your XSRF-Token-Header Key, and the environment variable in handle bars to access it{{}}

enter image description here

  1. Now before running your new request make sure you run your login, it will store the environment variable, and then when you run the actually request it will automatically append it.

Solution 2 - Spring

I am able to send REST with csrf token by following the steps below:

  1. The CSRF token generated automatically by spring security when you logged in. It will be shown at the response header.

  2. The CSRF token can be used on subsequent request by setting X-CSRF-TOKEN with CSRF token on header.

Solution 3 - Spring

Firstly you need to install PostMan Interceptor and activate it to have access to the browsers cookies.

  1. You have to fetch the CSRF Token by making a GET Request: Header: "XSRF-TOKEN" and Value: "Fetch"

  2. You should see the Token in the cookie tab and can copy it (Notice: You can configure spring how the cookie should be named. Maybe your cookie has another name than "XSRF-TOKEN". Attention: You have the remove this blank char in the token from the newline)

  3. Now make your POST Request and set the header to: Header: "X-XSRF-TOKEN" and Value: "Your copied Token without blanks"

Solution 4 - Spring

For me works variant with adding X-CSRF-TOKEN to headers. enter image description here

Solution 5 - Spring

Please put X-CSRF-Token as key and FETCH as the value in the GET request header and you will receive the token in the response header

Solution 6 - Spring

If you don't want to configure environment variables etc. here is the quickest solution

https://stackoverflow.com/a/49249850/3705478

Solution 7 - Spring

I've used csrfTokenRepository() to allow spring security to generate csrf token

@EnableWebSecurity
public class AppSecurityConfig extends WebSecurityConfigurerAdapter{
    @Override
    protected void configure(HttpSecurity http) throws Exception {
    // TODO Auto-generated method stub
    http.csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());
    // your code
    } 
}

After adding these lines of code, use GET request to generate csrf token. I've used postman and I got token in the response cookies section. Copy the token and use it in POST call.

Official documentation link : https://docs.spring.io/spring-security/site/docs/5.0.x/reference/html/csrf.html

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSurendra JnawaliView Question on Stackoverflow
Solution 1 - Springjohnny 5View Answer on Stackoverflow
Solution 2 - SpringtranceholicView Answer on Stackoverflow
Solution 3 - SpringJoel NeukomView Answer on Stackoverflow
Solution 4 - SpringDmRomantsovView Answer on Stackoverflow
Solution 5 - SpringKPS250View Answer on Stackoverflow
Solution 6 - Springsofs1View Answer on Stackoverflow
Solution 7 - SpringManideep Reddy NView Answer on Stackoverflow