Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 627
Surendra Jnawali Avatar asked Nov 28 '14 05:11

Surendra Jnawali


People also ask

How use CSRF token in spring boot?

To protect MVC applications, Spring adds a CSRF token to each generated view. This token must be submitted to the server on every HTTP request that modifies state (PATCH, POST, PUT and DELETE — not GET). This protects our application against CSRF attacks since an attacker can't get this token from their own page.

How do I pass CSRF token in REST API?

The CSRF token is stored in the client. The CSRF token is required for any later REST API calls. The client must send a valid token with every API request. The token is sent in a custom request HTTP header.

How is CSRF token sent to client?

The CSRF token can be transmitted to the client as part of a response payload, such as a HTML or JSON response. It can then be transmitted back to the server as a hidden field on a form submission, or via an AJAX request as a custom header value or part of a JSON payload.


2 Answers

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.
like image 137
johnny 5 Avatar answered Nov 01 '22 20:11

johnny 5


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.

like image 38
tranceholic Avatar answered Nov 01 '22 22:11

tranceholic