는,이 같은 CSRF 토큰을 구하십시오CSRF 불일치 각도에서
콘솔에 새 토큰을 기록// Get CSRF token and set as header
var csrfRequest = Auth.getCSRF().then(function(data){
console.log(data.data._csrf);
$rootScope.csrf = data.data._csrf;
});
(이 잘 작동).
그런 다음 sails.js API에 로그인하려고합니다.
POST /auth/login HTTP/1.1
Host: localhost:1337
Connection: keep-alive
Content-Length: 108
Pragma: no-cache
Cache-Control: no-cache
Accept: application/json, text/plain, */*
Origin: http://localhost
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36
Content-Type: application/json;charset=UTF-8
Referer: http://localhost/sails-front/src/login/
Accept-Encoding: gzip, deflate
Accept-Language: en-GB,en-US;q=0.8,en;q=0.6
Request Payloadview parsed
{"email":"[email protected]","password":"mypass","_csrf":"PIlVO7S362OroPGBSG0X1vW2FydkP9VhK8cMk="}
_csrf
필드는 getCSRF
전화에 수신 된 것과 동일합니다 : 여기에 요청합니다.
그러나 응답 'CSRF mismatch'가 표시됩니다. 그러나, 우편 배달부와 똑같은 것을 시도해도 제대로 작동합니다 (그래서 나는 돛 서버에 문제가 있다고 생각하지 않습니다).
다음은 로그인 코드입니다.
$scope.login = function() {
Auth.login({
email: $scope.email,
password: $scope.password,
_csrf: $rootScope.csrf
},
function(res) {
$state.go('app.home');
},
function(err) {
$rootScope.error = "Failed to login";
});
};
그리고 Auth
서비스 : 컨트롤러에서 -하지 전송되는 것을보고,하지만 당신은 전체 요청을 게시
/*********************************
* login
******************************** */
function login(user, success, error) {
$http.post(API_BASE_URL + 'auth/login', user).success(function(user){
changeUser(user);
success(user);
}).error(error);
}
작동하지 않습니다
는
withCredentials
설정을 사용하여 요청과 함께 쿠키를 보낼 각도 이야기합니다. 'CSRF Mismatch'가 발생했습니다 –'$ http.defaults.withCredentials = true;로 설정하면 작동하지만 일반적으로 특정 게시물에는 필요하지 않습니다. –
@JuanSolano는 당신에게 무엇을 말할지 모르지만 ... 나를 위해 일하고 $ http.post에 대한 각도 문서 (https://docs.angularjs.org/api/ng/service/$http)에 따라 정확합니다. ... – sgress454