POST from jQuery to Spring MVC boot needs user credentials

I need to do jQuery.POST with user credentials in order for Spring Server Security to accept the request.

After googling, I haven't found a "possible solution".

This is the solution (doesn't work) which seems to be the closest thing to a real solution.

      $.ajax({
        type: 'post',
        url: urlprova,
        data: JSON.stringily(respostas),
        dataType: 'json',
        contentType: 'application/json',
        success: function(response) {
        console.log(response);
    },
      xhrFields: {
        withCredentials: true
      },
      error: function(error) {
      console.log('ERROR:', error);
     }
    });

      

How should I fix this code to add User Credentials to work with Spring MVC Security.

+3


source to share


1 answer


Spring security blocks jquery post requests as it identifies them as csrf requests. You will need to add url pattern to ignorelist csrf in your security config, for example:



    http.csrf()
        .ignoringAntMatchers("/abc/**", "/xyz/**", "/home/**")

      

+3


source







All Articles