使用自定义标头从一个域重定向到另一个域由于 HTTP 协议限制,无法在响应中设置 cookie 或 cookie。重定向本质上由与响应关联的标头(位置)组成,并且不允许将任何标头添加到目标位置。
也不允许为不同的域设置 cookie,因为它会构成重大安全风险。浏览器使用 Set-Cookie 标头存储服务器发送的 cookie 和响应,然后将它们发送回服务器以向同一域中的同一服务器发出请求。 Cookie 不会发送到不同的域。
一种方法是让源域将用户重定向到目标域作为查询参数传递的访问令牌。然后,目标域可以读取令牌并设置自己的 cookie,浏览器将存储该 cookie 并将其发送以供后续请求。
源域 (appA.py)
<code class="python">from fastapi import FastAPI, Response from fastapi.responses import RedirectResponse, HTMLResponse app = FastAPI() @app.get('/', response_class=HTMLResponse) def home(): return ''' <!DOCTYPE html> <html> <body> <h2>Click the "submit" button to be redirected to domain B</h2> <form method="POST" action="/submit"> <input type="submit" value="Submit"> </form> </body> </html> ''' @app.post('/submit') def submit(): token = 'MTQ0NjJkZmQ5OTM2NDE1ZTZjNGZmZjI3' redirect_url = f'http://example.test:8001/submit?token={token}' response = RedirectResponse(redirect_url) response.set_cookie(key='access-token', value=token, httponly=True) return response</code>
目标域 (appB.py)
<code class="python">from fastapi import FastAPI, Request, status from fastapi.responses import RedirectResponse, HTMLResponse app = FastAPI() @app.get('/', response_class=HTMLResponse) def home(): token = request.cookies.get('access-token') print(token) return 'You have been successfully redirected to domain B!' \ f' Your access token ends with: {token[-4:]}' @app.post('/submit') def submit(request: Request, token: str): redirect_url = request.url_for('home') response = RedirectResponse(redirect_url, status_code=status.HTTP_303_SEE_OTHER) response.set_cookie(key='access-token', value=token, httponly=True) return response</code>
另一种方法涉及使用 Window. postMessage() 用于跨源通信。源域将令牌发送到目标域,目标域将其存储在 localStorage 中并设置 cookie。缺点包括浏览器兼容性和敏感数据存储在 localStorage 中。
StackExchange 使用更强大的解决方案在不同站点之间自动登录。它涉及通过图像的 src 属性发送身份验证令牌,这会触发服务器响应并在目标站点上设置 cookie。
这需要浏览器接受第三方 cookie 和目标服务器上的 CORS 配置。它还会在查询字符串中发送令牌,带来潜在的安全风险。
源域 (appA.py)
<code class="python">from fastapi import FastAPI, Response from fastapi.responses import HTMLResponse app = FastAPI() @app.get('/', response_class=HTMLResponse) def home(): return ''' <!DOCTYPE html> <html> <body> <h2>Click the "submit" button to be redirected to domain B</h2> <input type="button" value="Submit" onclick="submit()"> <script> function submit() { fetch('/submit', { method: 'POST', }) .then(res => { authHeader = res.headers.get('Authorization'); if (authHeader.startsWith("Bearer ")) token = authHeader.substring(7, authHeader.length); return res.text(); }) .then(data => { var url = 'http://example.test:8001/submit?token=' + encodeURIComponent(token); var img = document.createElement('img'); img.style = 'display:none'; img.crossOrigin = 'use-credentials'; img.onerror = function(){ window.location.href = 'http://example.test:8001/'; } img.src = url; }) .catch(error => { console.error(error); }); } </script> </body> </html> ''' @app.post('/submit') def submit(): token = 'MTQ0NjJkZmQ5OTM2NDE1ZTZjNGZmZjI3' headers = {'Authorization': f'Bearer {token}'} response = Response('success', headers=headers) response.set_cookie(key='access-token', value=token, httponly=True) return response</code>
目标域 (appB) .py)
<code class="python">from fastapi import FastAPI, Request, Response from fastapi.responses import RedirectResponse from fastapi.middleware.cors import CORSMiddleware app = FastAPI() origins = ['http://localhost:8000', 'http://127.0.0.1:8000', 'https://localhost:8000', 'https://127.0.0.1:8000'] app.add_middleware( CORSMiddleware, allow_origins=origins, allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) @app.get('/') def home(request: Request): token = request.cookies.get('access-token') print(token) return 'You have been successfully redirected to domain B!' \ f' Your access token ends with: {token[-4:]}' @app.get('/submit') def submit(request: Request, token: str): response = Response('success') response.set_cookie(key='access-token', value=token, samesite='none', secure=True, httponly=True) return response</code>
在域之间传输令牌或设置 cookie 时,考虑安全影响至关重要。避免在查询字符串中发送敏感数据,因为它可能会被拦截或泄露。使用 HTTPS 连接进行安全数据传输。将 SameSite 标志设置为“无”,并使用安全标志来保护跨站点访问。
以上是如何在 HTTP 域之间使用 Cookie 和标头进行重定向?的详细内容。更多信息请关注PHP中文网其他相关文章!