[英]Django - Changing the url to redirect from a middleware on the fly
我正在構建一個 SSO 登錄,旨在通過電子郵件發送的鏈接使用。
每個鏈接都應該自動連接用戶 (SSO),並且可以多次點擊(它們有一個 TTL,這取決於電子郵件)
它工作正常,但我擔心最終用戶在社交網絡上分享他的 url(基本上是復制/粘貼包含 SSO 令牌的 url),允許任何人通過鏈接自動登錄。
我的第一次嘗試是嘗試從我的 SSOMiddleware 中刪除 GET SSO_TOKEN 參數,如下所示:
if remove_token_middleware:
request.GET._mutable = True # GET is not mutable by default, we force it
# Remove the token from the url to avoid displaying it to the client (avoids sharing sso token when copy/pasting url)
del request.GET[SSO_TOKEN_PARAM]
request.GET._mutable = False # Restore default mutability
return login(request, user) if service.get("auto_auth") else None
基本上,我的想法是,由於 SSO_TOKEN 在request.get
object 中,因此將其從中刪除最終會改變 url 用戶被重定向的位置
在我的 controller 中,這是用戶如何“重定向”(使用render
)
return render(request, 'campagne_emprunt/liste_offres_prets.html', locals())
使用render
時,沒有重定向,SSO 令牌在 URL 中仍然可見(在瀏覽器地址欄中)。
有沒有辦法以某種方式告訴 Django 即時更改目的地 url?
所以為了重定向,有一個內置的重定向 function 的 Django 你使用而不是渲染我認為..試一試,
from django.shortcuts import redirect
def redirect_view(request):
response = redirect('/redirect-success/')
return response
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.