3

I need to make a jQuery.POST with the user credentials that the Spring Server Security needs to accept the request.

After googling i didn't found a "possible solution".

This is the solution (doesn't work) that seems to be nearest 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 i have to fix that code to add the User Credentials for working with the Spring MVC Security.

Alberto Crespo
  • 2,429
  • 5
  • 28
  • 51
  • Is Spring Security configured to use HTTP basic? If so, try this: http://stackoverflow.com/questions/5507234/how-to-use-basic-auth-and-jquery-and-ajax – Bohuslav Burghardt Nov 21 '14 at 15:09
  • It is a good idea but the problem is that the username and the password there aren't in the actual html page (they are in the previous one, the login page). Do you know how i could get them again? Thank you – Alberto Crespo Nov 21 '14 at 15:14

1 Answers1

3

Spring security will block jquery post requests as it identifies them as csrf requests. You will need to add the url pattern to the csrf ignorelist in your security config, like so:

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