Javascript 将cookie设置为不出现在Axios和Fetch中
我正在使用我的API端点进行身份验证,并在响应中使用Lexik JWT令牌设置一个仅http的cookie,当使用postman对其进行测试时,一切正常cookie设置正确需要注意的是,CORS是通过Nelmio CORS捆绑包启用的Javascript 将cookie设置为不出现在Axios和Fetch中,javascript,reactjs,symfony,Javascript,Reactjs,Symfony,我正在使用我的API端点进行身份验证,并在响应中使用Lexik JWT令牌设置一个仅http的cookie,当使用postman对其进行测试时,一切正常cookie设置正确需要注意的是,CORS是通过Nelmio CORS捆绑包启用的 nelmio_cors: defaults: allow_credentials: true origin_regex: true allow_origin: ['%env(CORS_ALLOW_ORIGIN
nelmio_cors:
defaults:
allow_credentials: true
origin_regex: true
allow_origin: ['%env(CORS_ALLOW_ORIGIN)%']
allow_methods: ['GET', 'OPTIONS', 'POST', 'PUT', 'PATCH', 'DELETE']
allow_headers: ['Access-Control-Allow-Origin', 'X-Requested-With', 'X-HTTP-Method-Override', 'Content-Type', 'Accept']
expose_headers: ['Link']
max_age: 3600
以下是LexikEvents::AUTHENTICATION\u SUCCESS
<?php
namespace App\EventSubscriber;
use Symfony\Component\HttpFoundation\Response;
use Lexik\Bundle\JWTAuthenticationBundle\Events as LexikEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationSuccessEvent;
use Symfony\Component\HttpFoundation\Cookie;
class LexikLoginSuccessSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
LexikEvents::AUTHENTICATION_SUCCESS => ['onAuthenticationSuccess']
];
}
public function onAuthenticationSuccess(AuthenticationSuccessEvent $event)
{
/** @var Response $response */
$response = $event->getResponse();
$hourLater = (new \DateTime())->modify('+1hours');
$cookie = new Cookie('jwt_token', $event->getData()['token'], $hourLater);
$response->headers->setCookie($cookie);
}
}
启动onSubmit函数后,响应确实是一个JWT令牌,但SET COOKIE标头不存在,COOKIE未设置。在axios文档中研究了3个小时后,阅读了大量类似问题,建议进行不同的修复和测试,我最终得出以下结论:
第一步:
对于AXIOS请确保将配置中的withCredentials设置为true(如果没有设置,您可能会丢失它,如果没有问题,请转至下一步) 请注意,不需要maxRedirects(有问题的代码)
对于FETCH请确保将配置中的凭据设置为“include”(如果没有设置,您可能会丢失它,如果没有问题,请转到下一步)
第二步:
这是有趣的部分,您的服务器是否运行在本地主机域以外的其他域上,比如127.0.0.1:8000 url?下面是一个问题:Google Chrome和基于Chrome引擎的浏览器将阻止来自任何端口后固定url的Cookie(我不确定其他浏览器,但以防万一在-127.0.0.1:80上服务您的后端,使用hosts文件将您的url映射到域,使用localtunnel/ngrok以防您的浏览器决定投诉您的后端url)
现在,您应该全部设置为存储来自响应的跨源http纯cookies,无论后端语言是什么,启用CORS后应该基本相同
const onSubmit = e => {
e.preventDefault();
fetch('http://127.0.0.1:8000/api/get-cookies', {
method: 'POST',
credentials: 'include',
body: JSON.stringify({
username: form.username,
password: form.password,
}),
headers: {
"Content-Type": "application/json",
}
})
.then( response => response.json()).then( json => console.log(json)).catch( error => console.log(error));
Axios.post('http://127.0.0.1:8000/api/get-cookies', {
username: form.username,
password: form.password,
}, {
withCredentials: true,
maxRedirects: 0,
headers: {
"Content-Type": "application/json",
}
}).then(response => console.log(response)).catch( error => console.log(error));
}
config = { ...yourConfig, withCredentials: true }
Axios.post(url, data, config).then.....
config = { ...yourConfig, credentials: "include"
fetch(url, config).then....