【问题标题】:TastyPie Authentication from the same server来自同一服务器的 TastyPie 身份验证
【发布时间】:2014-03-09 14:36:24
【问题描述】:

我在 TastyPie 中有一个在同一个域上使用的 API。我只想允许来自我的服务器的请求。

TastyPie 有许多不同的 Authentication 选项,但是我不能使用 Session Authentication,因为没有人登录并且 API Key 可能是在我的脚本中查看。

所以我想我可以用 Django csrf 令牌 以某种方式验证帖子。这可能是任何示例(我已经搜索过)还是我错过了一个选项?

【问题讨论】:

    标签: python django tastypie


    【解决方案1】:

    This answer提供了以下获取请求IP地址的方法:

    def get_client_ip(request):
        x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
        if x_forwarded_for:
            ip = x_forwarded_for.split(',')[0]
        else:
            ip = request.META.get('REMOTE_ADDR')
        return ip
    

    您可以尝试将其与自定义 Authentication 类耦合,如下所示:

    class IpAuthentication(Authentication):
        def is_authenticated(self, request, **kwargs):
            return get_client_ip(request) in SETTINGS.ALLOWED_IPS:
    

    您必须填写自己的SETTINGS.ALLOWED_IPS 列表。然而,这是not a foolproof method,因为 IP 地址可以被伪造。

    【讨论】:

      猜你喜欢
      • 2017-09-14
      • 2020-08-29
      • 1970-01-01
      • 2014-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-14
      • 1970-01-01
      相关资源
      最近更新 更多