如何允許 django Backend django restapi 的 CSRF_TRUSTED_ORIGIN 中的所有/任何 ips 正在運行,并且前端在一個系統中處于角度狀態,我們正在嘗試使用另一個系統中的系統 ip 訪問,我能夠訪問前端并在訪問后端 POST 方法 API 時不作業它顯示在 csrf 受信任的來源中找不到。在 settings.py 中,我獲取了動態 ips。
import socket
def get_ipaddress():
host_name = socket.gethostname()
ip_address = socket.gethostbyname(host_name)
return "http://" ip_address ":4200"
ALLOWED_HOSTS=["*"]
CSRF_TRUSTED_ORIGINS=[get_ipaddress()]
嘗試使用 csrf_excempt ,但它不起作用。django4.0.1 版本,Angular 16
uj5u.com熱心網友回復:
對于 CSRF,您只需將托管 Angular 應用程式的服務器的 IP 列入白名單/允許。當您運行 Angular 應用程式時,您應該將您在瀏覽器中訪問 Angular 應用程式的 url 列入白名單,例如:"http://localhost:4200"
或http://192.168.1.1:4200
. 或https://whateveryourwebappurlis.com
。
這是您用于在瀏覽器中加載應用程式的 URL。您需要將此列入白名單。
CSRF_TRUSTED_ORIGINS=["http://localhost:4200", "https://whateveryourwebappis.com"]
Make sure that you are passing this is in the origin header of your request to django app.
Read more at: https://docs.djangoproject.com/en/4.0/ref/settings/#csrf-trusted-origins
In case you already haven't whitelisted the methods and headers, please do that as well in your settings.
CORS_ALLOW_ALL_ORIGINS=False
CSRF_TRUSTED_ORIGINS = [
"http://yourwhitelistedip.com",
]
CORS_ALLOW_METHODS = [
'DELETE',
'GET',
'OPTIONS',
'PATCH',
'POST',
'PUT',
]
CORS_ALLOW_HEADERS = [
'accept',
'accept-encoding',
'authorization',
'content-type',
'dnt',
'origin',
'user-agent',
'x-csrftoken',
'x-requested-with',
]
uj5u.com熱心網友回復:
socket.gethostbyname(host_name.local)
在 get_ipaddress 中使用上述行時,它可以作業
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/490623.html
標籤:django angularjs django-rest-framework ip django-csrf