[英]How can I make my Django API only accept requests that come from my website itself?
我必須為我的 Django 應用程序創建一個 API 應用程序,以處理我在公共和 staticAA81259CEF8E959C624DF1D456E5D3297ZZ237103B69DAAAF712DA9 文件中無法擁有的敏感內容我怎樣才能使這個視圖只接受來自我自己網站的請求,並拒絕來自“外部”的任何請求(如果有人在下面復制了我的請求,他們應該會收到錯誤)?
如果有任何其他安全問題,請在您的回復中提及。 我的網站托管在 Heroku 上。
我的 javascript 文件中的請求:
var clientSecret = await fetch('https://url.com/api/', {
method: 'POST',
body: params,
headers: {
'Content-Type': 'text/plain'
},
}).then(r => r.json())
我對 API ( https://url.com/api/
) 的看法:
from rest_framework.request import Request as RESTRequest
from rest_framework.response import Response
from rest_framework.decorators import api_view
import requests
@api_view(['POST'])
def payment(request, *args, **kwargs):
... #define headers_in and params_in here
response = requests.post('https://outboundapirequest.com/v1/request',
headers=headers_in,
data=params_in)
return Response(response.json()['value'])
通過使用“跨域資源共享 (CORS)”
在 Django 設置中,您可以設置允許向您的 API 服務器發出請求的所有域的列表。
像這樣
CORS_ALLOWED_ORIGINS = [ "http://localhost:8080", "http://127.0.0.1:9000" ]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.