[英]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.