简体   繁体   English

如何从 Postman rest 客户端发送 spring csrf 令牌?

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

I have csrf protection in spring framework.我在 spring 框架中有csrf 保护 So in each request I send csrf token in header from ajax call, which is perfectly working.因此,在每个请求中,我从 ajax 调用中发送 header 中的 csrf 令牌,这非常有效。

<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在 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 ?我不知道生成 csrf 令牌并包含在 Postman Rest 客户端的 header 部分中 Would you please help me to send csrf token from Postman Rest Client?你能帮我从 Postman Rest 客户端发送 csrf 令牌吗?在此处输入图像描述

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 注意:您需要安装PostMan Interceptor并将其激活以访问浏览器cookie

  1. Create a new environment so environment variables can be stored 创建一个新环境,以便存储环境变量

在此输入图像描述

  1. Create a login method with a test to store the XSRF cookie in an environment variable, in the test tab post this code 创建一个带有测试的登录方法,将XSRF cookie存储在环境变量中,在测试选项卡中发布此代码

     //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 编辑对于使用5.5.2邮递员或更高版本的任何人,您还必须解码cookie,他们还提供了获取cookie的替代方法,因为@Sacapuces指出

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

Now you will have an environment variable with xsrf-token in it. 现在,您将拥有一个带有xsrf-token的环境变量。

  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{{}} 创建要创建的新帖子,并在标题中添加XSRF-Token-Header Key,并在句柄栏中添加环境变量以访问它{{}}

在此输入图像描述

  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. 现在,在运行新请求之前,请确保运行登录,它将存储环境变量,然后当您运行实际请求时,它将自动附加它。

I am able to send REST with csrf token by following the steps below: 我可以通过以下步骤发送带有csrf令牌的REST:

  1. The CSRF token generated automatically by spring security when you logged in. It will be shown at the response header. 登录时,Spring安全性会自动生成CSRF令牌。它将显示在响应头中。

  2. The CSRF token can be used on subsequent request by setting X-CSRF-TOKEN with CSRF token on header. 通过在标头上设置带有CSRF令牌的X-CSRF-TOKEN,可以在后续请求中使用CSRF令牌。

Firstly you need to install PostMan Interceptor and activate it to have access to the browsers cookies. 首先,您需要安装PostMan Interceptor并激活它以访问浏览器cookie。

  1. You have to fetch the CSRF Token by making a GET Request: Header: "XSRF-TOKEN" and Value: "Fetch" 您必须通过发出GET请求来获取CSRF令牌:标题:“XSRF-TOKEN”和值:“获取”

  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) 你应该在cookie选项卡中看到令牌并且可以复制它(注意:你可以配置spring如何命名cookie。也许你的cookie有另一个名字而不是“XSRF-TOKEN”。注意:你有删除这个空白的字符在来自换行符的令牌)

  3. Now make your POST Request and set the header to: Header: "X-XSRF-TOKEN" and Value: "Your copied Token without blanks" 现在制作你的POST请求并将标题设置为:标题:“X-XSRF-TOKEN”和值:“你复制的令牌没有空格”

For me works variant with adding X-CSRF-TOKEN to headers. 对我而言,工作变量是将X-CSRF-TOKEN添加到标题中。 在此输入图像描述

If you don't want to configure environment variables etc. here is the quickest solution 如果您不想配置环境变量等,这是最快的解决方案

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

请将X-CSRF-Token作为密钥和FETCH作为GET请求头中的值,您将在响应头中收到令牌

I've used csrfTokenRepository() to allow spring security to generate csrf token我使用 csrfTokenRepository() 允许 spring 安全生成 csrf 令牌

@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.添加这几行代码后,使用GET请求生成csrf token。 I've used postman and I got token in the response cookies section.我使用了 postman 并且在响应 cookies 部分中获得了令牌。 Copy the token and use it in POST call.复制令牌并在 POST 调用中使用它。

Official documentation link: https://docs.spring.io/spring-security/site/docs/5.0.x/reference/html/csrf.html官方文档链接: https://docs.spring.io/spring-security/site/docs/5.0.x/reference/html/csrf.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM