Django XFrameOptionsMiddleware(X-Frame-Options) - 通过客户端IP允许iframe [英] Django XFrameOptionsMiddleware (X-Frame-Options) - allow iframe by client IP

查看:2147
本文介绍了Django XFrameOptionsMiddleware(X-Frame-Options) - 通过客户端IP允许iframe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Django XFrameOptionsMiddleware来控制点击劫持,但是我有一个客户需要能够在网络中的iframe中浏览该应用。我想要在视图方法中应用(或删除)xframe_options_exempt装饰器。

I'm using Django XFrameOptionsMiddleware to control clickjacking, but I have a customer that needs to be able to browse the app in an iframe from within their network. I want to be able to apply (or remove) the xframe_options_exempt decorator from within the view method.

推荐答案

最佳方法是覆盖get_xframe_options_value。 XFRAME_EXEMPT_IPS是一个glob_list,用于使用fnmatch(192.168。*)来检测允许的网络。

Best approach is to override get_xframe_options_value. XFRAME_EXEMPT_IPS is a glob_list in my case to detect allowable networks using fnmatch (192.168.*).

class TFXFrameOptionsMiddleware(XFrameOptionsMiddleware):
    def get_xframe_options_value(self, request, response):
        if request.META['REMOTE_ADDR'] in settings.XFRAME_EXEMPT_IPS:
            return 'ALLOWALL' # non standard, equivalent to omitting
        return getattr(settings, 'X_FRAME_OPTIONS', 'SAMEORIGIN').upper()

这篇关于Django XFrameOptionsMiddleware(X-Frame-Options) - 通过客户端IP允许iframe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆