【问题标题】:Removing image accessibility删除图像可访问性
【发布时间】:2018-01-16 03:09:40
【问题描述】:

我有一个网页,它从烧瓶中提供一个图片链接。我有它,所以图像在一两秒后消失。但是,如果您查看页面源代码,您仍然可以通过找到图片的确切 url 来获取图片。如何删除对 url 的访问或从 url 中删除图片,以便用户在图像消失后无法再查看图像。

HTML页面:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Welcome</title>
    <script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
</head>
<body>
<script type="text/javascript">
    $(document).ready(function () {
        var c = document.getElementById('image');
        console.log(typeof(c));
        setTimeout(function () {
            $('#des').css('visibility', 'hidden');
        }, 1750);
        fs.unlink("{{ url_for('static', filename = '1l1l1lI1lIlIlIlI1IlI1II1l1.jpg') }}");
        removeElement(c)
        storage.clear(c);
        sessionStorage.clear(c);
    });
</script>

<div id="des">
    <img id="image" src="{{ url_for('static', filename = '1l1l1lI1lIlIlIlI1IlI1II1l1.jpg') }}"/>
</div>
</body>
</html>

main.py

from datetime import datetime
from flask import make_response
from functools import wraps, update_wrapper
from flask import Flask, request, session, g, redirect, url_for, abort, \
    render_template, flash

app = Flask(__name__)  # create the application instance
app.config.from_object(__name__)  # load config from this file , flaskr.py

def nocache(view):
    @wraps(view)
    def no_cache(*args, **kwargs):
        response = make_response(view(*args, **kwargs))
        response.headers['Last-Modified'] = datetime.now()
        response.headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0, max-age=0'
        response.headers['Pragma'] = 'no-cache'
        response.headers['Expires'] = '-1'
        return response
    return update_wrapper(no_cache, view)


@app.route(r'/')
@nocache
def landingpage():
    return render_template('index.html')

if __name__ == '__main__':
    app.run()

【问题讨论】:

  • 您可以通过脚本提供图像,然后在时间到时撤销访问权限,但实际上,一旦图像发送给客户端,客户端将有办法检索它,如果他们努力吧。
  • 无法编辑源。您可以使用 ajax 请求加载图像并将其附加到文档中。但是,网络活动的历史记录仍然可以在浏览器控制台中找到。
  • @AliSheikhpour 将其附加到文档是什么意思?
  • 例如通过ajax请求获取图片地址,设置为空图片标签的src。 $("#myimg").attr("src",imageurl);

标签: javascript python html image flask


【解决方案1】:

首先,如果用户足够努力,您的图像将不会受到保护。您不能阻止他们进行屏幕截图或类似的操作。

有几种方法可以帮助“防止”他们获取您的图片:

您可以通过 ajax 加载图像:

<script>
    $('#my_image').attr('src', {{ url_for('image_path') }});
</script>

您还可以通过过期哈希键(数据库驱动)加载。因此,创建一个映射到文件名的表,然后通过传入哈希键使用路由加载图像。如果他们尝试从调试器打开图像,它可能会说它已过期并且不会加载图像。

def get_image(self, hash_key):
    my_image = self.image_engine.get_image(hash_key)
    if my_image.get('is_viewed') == 0:
        real_key = my_image.get('real_key')
        root_path = os.path.join(self.image_path, real_key)
        # call a function to set the image as "viewed"
        return send_from_directory(root_path, '{}.png'.format(real_key))
    else:
        return render_template('404.html')

这样做意味着您可以为哈希键添加过期时间,甚至可以添加一个表明它已被查看然后什么都不渲染的标志,或者如果他们尝试再次加载它,则为 404。

此方法确实需要更多的数据库调用,但如果您要保护图像,则必须考虑一些性能选项。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-20
    • 2020-12-25
    • 2017-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-19
    • 1970-01-01
    相关资源
    最近更新 更多