保护 Django 和 React 中可公开访问的端点

问题描述 投票:0回答:1

我有一个端点 url.py

from django.urls import path 
from . import views 

app_name = 'Main'
urlpatterns = [
    path("helloKitty/", views.helloKitty, name='helloKitty'),
]

views.py

def helloKitty(request):
    hello = pd.read_csv('static/data/helloKitty_data.csv')
    hello = hello.to_json(orient='records')
    return HttpResponse(hello)

目前 /helloKitty 上的任何人都可以访问它,需要阻止它。

我在前端使用React来访问此端点并检索数据

import React from "react";
import { create } from 'zustand';
import axios from 'axios';

const kittyStore = create((set) => ({
    kitten: [],

    fetchKitty: async () => {
        const response = await axios.get('/helloKitty');
        const hello = response.data.map((h) => {
            return {
                name: h.name,
                age: h.age,
            }
        });
        set({ kitten })
    },
}));

export default kittyStore;

需要保护端点

/helloKitty
不被公开访问,并且只有React应用程序可以查看和获取数据。

javascript python reactjs django
1个回答
0
投票

可以使用中间件进行身份验证 https://docs.djangoproject.com/en/5.0/topics/http/middleware/

例如,您可以编写中间件来检查 React 请求中的身份验证标头

© www.soinside.com 2019 - 2024. All rights reserved.