美文网首页
跨域 img 设置 cookie

跨域 img 设置 cookie

作者: 苍老师的眼泪 | 来源:发表于2022-05-29 03:16 被阅读0次

从技术层面来讲,我们可以设置 <img> 标签需要带上cookie等凭据来向后端请求图片资源,后端检测凭据是否合法来决定是否返回相应资源
以 img 标签为例,其它例如 <audio>、<img>、<link>、<script> 和 <video> 均有一个跨域属性 (crossOrigin property),它允许你配置元素获取数据的 CORS 请求。

设置 crossorigin="use-credentials"表示请求将带上凭据:

<img crossorigin="use-credentials" src="https://asset.xxx.xxx/cxk.jpeg" alt="">

不能通过js代码设置跨域的domain:

Note: The domain must match the domain of the JavaScript origin. Setting cookies to foreign domains will be silently ignored.
https://stackoverflow.com/a/6761443

用于演示携带的 cookie
因为是跨域请求,这时候要浏览器带上cookie就需要设置以下红色框的内容,又因为是 https 请求,所以要设置绿框的内容


image.png

web服务器的设置(nginx):

    add_header 'Access-Control-Allow-Origin' '具体的某个源';
    add_header 'Access-Control-Allow-Credentials' 'true';

最终可以看到请求中携带了cookie:


image.png

相关文章

  • 跨域 img 设置 cookie

    从技术层面来讲,我们可以设置 标签需要带上cookie等凭据来向后端请求图片资源,后端检测凭据是否合法来决定是...

  • 跨域

    跨域:ajax 不能跨域img css script 标签 可以跨域例如:《img src="images/...

  • 跨域设置cookie

    当时的需求 大致是:前后台交互,但两个的地址不一样。需要后台识别当前的用户,那么后台就需要设置一个session,...

  • cookie

    key 键 value 值 expires 过期时间 domain 主域,不可以跨主域设置cookie,不过可以通...

  • [头参数]06 - Cookie

    目录 测试Cookie的流程 过期时间 设置httpOnly 设置Secure 跨域问题 0. 概述 常用参数 m...

  • 跨域资源共享 CORS

    跨域解决办法 1、 对于不用发送cookie的请求来说,直接设置: 2、如果需要发送cookie,xhr请求需要设...

  • Set-Cookie后,Cookie丢失问题解决(跨域)

    问题 iframe跨域访问,服务端设置免登cookie(response.addHeader("Set-Cooki...

  • 带cookie的跨域1

    带cookie的跨域1 1、Access-Control-Allow-Origin:*解决不了带Cookie的跨域...

  • node跨域设置cookie

    在做vue+express 做项目。 想做一个免登陆功能,选择session方案。 服务器设置session 后,...

  • 2021-09-22-🌦🌦 cookie 为什么作为token

    现象: 后端配置了cores跨域,前端直接请求,没有通过代理,这样如果设置cookie,只能设置到, localh...

网友评论

      本文标题:跨域 img 设置 cookie

      本文链接:https://www.haomeiwen.com/subject/hdxgprtx.html