Intercambio de recursos de origen cruzado con Spring Security
Estoy tratando de hacer que CORS juegue bien con Spring Security pero no está cumpliendo. Hice los cambios descritos enEste artículo y cambiando esta linea enapplicationContext-security.xml
tiene solicitudes POST y GET trabajando para mi aplicación (expone temporalmente los métodos del controlador, por lo que puedo probar CORS):
<intercept-url pattern="/**" access="isAuthenticated()" />
Después:<intercept-url pattern="/**" access="permitAll" />
Desafortunadamente, la siguiente URL que permite los inicios de sesión de Spring Security a través de AJAX no responde:http://localhost:8080/mutopia-server/resources/j_spring_security_check
. Estoy haciendo la solicitud de AJAX desdehttp://localhost:80
ahttp://localhost:8080
.
Al intentar accederj_spring_security_check
yo obtengo(pending)
en Chrome para la solicitud de verificación previa OPCIONES y devoluciones de llamada AJAX con el código de estado HTTP 0 y el mensaje "error".
La verificación previa se realiza correctamente con el código de estado HTTP 302 y todavía recibo la devolución de llamada de error para mi solicitud AJAX directamente después con el estado HTTP 0 y el mensaje "error".
Código de solicitud AJAXfunction get(url, json) {
var args = {
type: 'GET',
url: url,
// async: false,
// crossDomain: true,
xhrFields: {
withCredentials: false
},
success: function(response) {
console.debug(url, response);
},
error: function(xhr) {
console.error(url, xhr.status, xhr.statusText);
}
};
if (json) {
args.contentType = 'application/json'
}
$.ajax(args);
}
function post(url, json, data, dataEncode) {
var args = {
type: 'POST',
url: url,
// async: false,
crossDomain: true,
xhrFields: {
withCredentials: false
},
beforeSend: function(xhr){
// This is always added by default
// Ignoring this prevents preflight - but expects browser to follow 302 location change
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
xhr.setRequestHeader("X-Ajax-call", "true");
},
success: function(data, textStatus, xhr) {
// var location = xhr.getResponseHeader('Location');
console.error('success', url, xhr.getAllResponseHeaders());
},
error: function(xhr) {
console.error(url, xhr.status, xhr.statusText);
console.error('fail', url, xhr.getAllResponseHeaders());
}
}
if (json) {
args.contentType = 'application/json'
}
if (typeof data != 'undefined') {
// Send JSON raw in the body
args.data = dataEncode ? JSON.stringify(data) : data;
}
console.debug('args', args);
$.ajax(args);
}
var loginJSON = {"j_username": "username", "j_password": "password"};
// Fails
post('http://localhost:8080/mutopia-server/resources/j_spring_security_check', false, loginJSON, false);
// Works
post('http://localhost/mutopia-server/resources/j_spring_security_check', false, loginJSON, false);
// Works
get('http://localhost:8080/mutopia-server/landuses?projectId=6', true);
// Works
post('http://localhost:8080/mutopia-server/params', true, {
"name": "testing",
"local": false,
"generated": false,
"project": 6
}, true);
Tenga en cuenta que puedo enviar por correo electrónico a cualquier otra URL de mi aplicación a través de CORS, excepto el inicio de sesión de Spring Security. He revisado muchos artículos, por lo que cualquier información sobre este extraño problema sería muy apreciada.