Compartilhamento de recursos de origem cruzada com a segurança do Spring
Eu estou tentando fazer CORS jogar bem com o Spring Security, mas não está cumprindo. Eu fiz as mudanças descritas noEste artigo e mudar esta linha emapplicationContext-security.xml
tem solicitações POST e GET funcionando para meu aplicativo (expõe temporariamente os métodos do controlador para que eu possa testar o CORS):
<intercept-url pattern="/**" access="isAuthenticated()" />
Depois de:<intercept-url pattern="/**" access="permitAll" />
Infelizmente, o seguinte URL que permite logins do Spring Security através do AJAX não está respondendo:http://localhost:8080/mutopia-server/resources/j_spring_security_check
. Eu estou fazendo o pedido AJAX dehttp://localhost:80
parahttp://localhost:8080
.
Ao tentar acessarj_spring_security_check
eu recebo(pending)
no Chrome para a solicitação de preflight OPTIONS e retornos de chamada AJAX com o código de status HTTP 0 e a mensagem "error".
O preflight é bem-sucedido com o código de status HTTP 302 e ainda recebo o retorno de chamada de erro para minha solicitação AJAX diretamente depois com o status HTTP 0 e a mensagem "error".
Código de pedido 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);
Por favor, note - POST para qualquer outro URL no meu aplicativo via CORS, exceto o login de segurança da Primavera. Eu passei por muitos artigos, então qualquer insight sobre este estranho assunto seria muito apreciado