java通过curl命令发送POST请求
我无法通过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命令发送帖子:
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/"}
想法
# 1 楼答案
问题是:
你的应用程序(我可以看到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代码,在表单标记中,您将看到如下内容:
所以你需要在你的帖子中发送这个参数
小心正如我所说的,这个令牌将随着用户会话而改变,因此您将无法始终使用相同的令牌