Authenticate by IP address in Django

后端 未结 6 1970
别那么骄傲
别那么骄傲 2020-12-24 15:08

I have a small Django application with a view that I want to restrict to certain users. Anyone from a specific network should be able to see that view without any further au

6条回答
  •  感动是毒
    2020-12-24 15:39

    You can try this decorator. I have tested its working fine:

    allowedIps = ['129.0.0.1', '127.0.0.1']
    def allow_by_ip(view_func):
        def authorize(request, *args, **kwargs):
            user_ip = request.META['REMOTE_ADDR']
            for ip in allowedIps:
                if ip==user_ip:
                    return view_func(request, *args, **kwargs)
            return HttpResponse('Invalid Ip Access!')
        return authorize
    

提交回复
热议问题