HTTP 405 Not Allowed - Spring Boot + Spring Security

后端 未结 2 755
礼貌的吻别
礼貌的吻别 2021-01-25 15:41

I have a simple rest API which works with database. It worked properly until I added the security part. Now it gives HTTP 405 Not Allowed on the POST and DELETE requests. I have

2条回答
  •  鱼传尺愫
    2021-01-25 16:08

    You forget the csrf-Token.

    It's recommended that you add the csrf-Token in the meta-tag. You can read it in the Spring Security Documentation

    With this you can do the following:

    $(function () {
      var token = $("meta[name='_csrf']").attr("content");
      var header = $("meta[name='_csrf_header']").attr("content");
      $(document).ajaxSend(function(e, xhr, options) {
        xhr.setRequestHeader(header, token);
      });
    });
    

提交回复
热议问题