我通过curl命令发送POST请求时遇到问题。
@RequestMapping(value = "/abc/def/{parameter}/enum", method = RequestMethod.POST)
public ResponseEntity<classA> function(@PathVariable(value = "parameter") int parameter, @RequestBody String parameter2) {
a = list.get(parameter);
a.setParameter(enumA.getValue(parameter2));
ResponseEntity<classA> response = new ResponseEntity<>(a, HttpStatus.OK);
return response;
}
然后我想通过curl命令发送POST:
curl -H "Content-Type: application/json" -X POST -d '{"parameter2" : "enum"}' https://user:password@localhost:port/abc/def/1/enum -k
我得到回应:
{"timestamp":123456789,"status":403,"error":"Forbidden","message":"Expected CSRF token not found. Has your session expired?","path":"/abc/def/1/enum/"}
有想法吗?
问题是:
Expected CSRF token not found.
您的应用程序(如我所见,是Spring MVC)已启用CSRF保护,因此您需要在发布时发送“ _csrf”参数。有关更多信息,请访问:
http : //docs.spring.io/spring-security/site/docs/current/reference/html/csrf.html
https://spring.io/blog/2013/08/21/spring-security -3-2-0-rc1-highlights-csrf-protection /
CSRF令牌值随用户会话而变化,如果您想查看此csrf令牌,则可以使用Web浏览器访问应用程序并查看页面的HTML代码,在form标记中,您将看到以下内容:
<input type="hidden"
name= _csrf
value= 964f8675-a57a-4f85-b196-976d71ffef96 />
因此,您需要在POST中发送此参数。
curl -H "Content-Type: application/json" -X POST -d '{"parameter2" : "enum","_csrf":"964f8675-a57a-4f85-b196-976d71ffef96"}' -u username:password https://localhost:port/abc/def/1/enum
小心!:正如我所说,此令牌将随着用户会话而改变,因此您将无法始终使用相同的令牌。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句