简体   繁体   English

Flask 监控仪表板不适用于 after_request

[英]Flask Monitoring Dashboard not working with after_request

Flask Monitoring Dashboard Module is not working fine when @app.after_request is used in Flask Server.在 Flask 服务器中使用 @app.after_request 时,Flask 监控仪表板模块无法正常工作。

If I remove the @app.after_request then the Dashboard is working fine.如果我删除 @app.after_request,那么仪表板工作正常。

I want to include few things in @app.after_request.我想在@app.after_request 中包含一些内容。

@app.after_request
def after_request(response):
    s= 'request:'+str(request.data)+" \n "+str(request.remote_addr)+"  "+str(request.method)+"  "+ str(request.scheme)+"  "+ str(request.full_path)+"  "+str(response.status)+" \n "+"response returned :"+ str(response.data)
    print(s)
    debugPrint(str(inspect.stack()[0][3]),s)
    response.headers['Strict-Transport-Security'] = 'max-age=31536000; includeSubDomains'
    response.headers['Content-Security-Policy'] = "default-src 'self'"
    response.headers['X-Content-Type-Options'] = 'nosniff'
    response.headers['X-Frame-Options'] = 'SAMEORIGIN'
    response.headers['X-XSS-Protection'] = '1; mode=block'

    return response

when this code is included, the flask monitoring Dashboard is showing包含此代码时,将显示 flask 监控仪表板

烧瓶仪表板

Console Log when I am checking Flask Monitoring Dashboard我检查 Flask 监控仪表板时的控制台日志

控制台日志

Kindly please help me to resolve this issue.请帮我解决这个问题。

As @Halvor Sakshaug observes above, your problem is the line:正如@Halvor Sakshaug 在上面所观察到的,您的问题是:

    response.headers['Content-Security-Policy'] = "default-src 'self'"

If you look into your page console you'll see a bunch of messages of the form:如果你查看你的页面控制台,你会看到一堆形式的消息:

Refused to load the script 'https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.js' because it violates the following Content Security Policy directive: "default-src 'self'". Note that 'script-src-elem' was not explicitly set, so 'default-src' is used as a fallback.

Refused to load the script 'https://unpkg.com/sunburst-chart' because it violates the following Content Security Policy directive: "default-src 'self'". Note that 'script-src-elem' was not explicitly set, so 'default-src' is used as a fallback.

Since the external URLs are loading critical resources (eg Angular), and your desired content policy prevents this, at the moment it is only by relaxing your content security policy that you can make the Flask Monitoring Dashboard work.由于外部 URL 正在加载关键资源(例如 Angular),而您所需的内容策略会阻止这种情况,目前只有放松您的内容安全策略,您才能使 Flask 监控仪表板工作。

A line that allows all the FMD required external resources is:允许所有 FMD 所需的外部资源的行是:

    response.headers['Content-Security-Policy'] = "script-src 'self' cdnjs.cloudflare.com/ajax/ ajax.googleapis.com/ajax/ unpkg.com/sunburst-chart;"

PS Note that your code is also broken because the request var is undefined in the scope of the after_request function. PS请注意,您的代码也被破坏了,因为在 after_request function 的after_request中未定义request变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM