【问题标题】:In mojolicious, how to protect images from public view在 mojolicious 中,如何保护图像不被公众看到
【发布时间】:2018-07-21 08:00:41
【问题描述】:

请有人帮助我。我在 mojolicious Lite 中有应用程序

我想在没有会话登录的情况下阻止所有人的图像

当我输入http://m.y.i.p:3000/images/imageX.jpg

我只想在会话登录时显示图像。

我的html代码是

<a href="images/imagex.jpg">shwo image 1 </a>

【问题讨论】:

    标签: perl mojolicious mojolicious-lite


    【解决方案1】:

    与任何其他内容相同。为请求设置处理程序,呈现(或不呈现)内容。

    get '/images/*img' => sub {
        my $c = shift;
        if (!$c->session("is_authenticated")) {
            return $c->render( text => "Forbidden", status => 403 );
        }
        my $file = $c->param("img");
        if (!open(my $fh, '<', $IMAGE_DIR/$file)) {
            return $c->render( text => "Not found", status => 404 );
        }
        my $data = do { local $/; <$fh> };
        close $fh;
        $c->render( data => $data, format => 'jpg' );
    };
    

    您的请求处理程序将优先于从公共文件夹提供内容的默认处理程序,但是一旦您有了这个处理程序,您就不需要将它提供的文件存储在公共文件夹中。

    【讨论】:

    • Ty,找到了。
    【解决方案2】:

    另一种解决办法是

    get '/pays/*img' => sub {
    my $self = shift;
    
    my $img = $self->param('img');
    plugin 'RenderFile';
    
    my @imgext = split(/\./, $img);
    my $ext = $imgext[-1];
    
    
    $self->render_file(
    'filepath' => "/directiry/$img",
    'format'   => "$ext",                 # will change Content-Type "application/x-download" to "application/pdf"
    'content_disposition' => 'inline',   # will change Content-Disposition from "attachment" to "inline"
                         # delete file after completed
    

    );

    它是使用插件'RenderFile';

    【讨论】:

    • 这只是一个用图像响应的处理程序。根本没有授权。
    猜你喜欢
    • 2015-08-21
    • 2020-07-21
    • 2017-10-20
    • 1970-01-01
    • 1970-01-01
    • 2013-09-23
    • 1970-01-01
    • 2010-11-22
    相关资源
    最近更新 更多