ASP.NET Core 2.1 : 十四.静态文件与访问授权、防盗链

我的网站的图片不想被公开浏览、下载、盗链怎么办?本文主要通过解读一下asp.net core对于静态文件的处理方式的相关源码,来看一下为什么是wwwroot文件夹,如何修改或新增一个静态文件夹,为什么新增的文件夹名字不会被当做controller处理?访问授权怎么做?(asp.net core 系列目录

一、静态文件夹

所谓静态文件,直观的说就是wwwroot目录下的一些直接提供给访问者的文件,例如css,图片、js文件等。 当然这个wwwroot目录是默认目录,

这个是在main->createdefaultbuilder的时候做了默认设置。

public static class hostingenvironmentextensions
    {
        public static void initialize(this ihostingenvironment hostingenvironment, string contentrootpath, webhostoptions options)
        {
           //省略部分代码
            var webroot = options.webroot;
            if (webroot == null)
            {
                // default to /wwwroot if it exists.
                var wwwroot = path.combine(hostingenvironment.contentrootpath, "wwwroot");
                if (directory.exists(wwwroot))
                {
                    hostingenvironment.webrootpath = wwwroot;
                }
            }
            else
            {
                hostingenvironment.webrootpath = path.combine(hostingenvironment.contentrootpath, webroot);
            }
           //省略部分代码
        }
    }

二、处理方式

前文关于中间件部分说过,在startup文件中,有一个 app.usestaticfiles() 方法的调用,这里是将静态文件的处理中间件作为了“处理管道”的一部分,

并且这个中间件是写在 app.usemvc 之前, 所以当一个请求进来之后, 会先判断是否为静态文件的请求,如果是,则在此做了请求处理,这时候请求会发生短路,不会进入后面的mvc中间件处理步骤。

        public void configure(iapplicationbuilder app, ihostingenvironment env)
        {
            if (env.isdevelopment())
            {
                app.usedeveloperexceptionpage();
            }
            else
            {
                app.useexceptionhandler("/home/error");
            }

            app.usestaticfiles();

            app.usecookiepolicy();
            app.useauthentication();
            app.usemvc(routes =>
            {
                routes.maproute(
                    name: "default",
                    template: "{controller=home}/{action=index}/{id?}");
            });
        }

三、新增静态文件目录

除了这个默认的wwwroot目录,需要新增一个目录来作为静态文件的目录,可以startup文件的 app.usestaticfiles() 下面继续use,例如下面代码

            app.usefileserver(new fileserveroptions
            {
                fileprovider = new physicalfileprovider(
        path.combine(directory.getcurrentdirectory(), "newfilespath")),
                requestpath = "/newfiles"
            });

含义就是指定应用程序目录中的一个名为“newfilespath”的文件夹,将它也设置问静态文件目录, 而这个目录的访问路径为"/newfiles"

例如文件夹"newfilespath"下面有一个test.jpg, 那么我们可以通过这样的地址来访问它:http://localhost:64237/newfiles/test.jpg。

四、中间件的处理方式

静态文件的处理中间件为staticfilemiddleware,主要的处理方法 invoke 代码如下

        public async task invoke(httpcontext context)
        {
            var filecontext = new staticfilecontext(context, _options, _matchurl, _logger, _fileprovider, _contenttypeprovider);

            if (!filecontext.validatemethod())
            {
                _logger.logrequestmethodnotsupported(context.request.method);
            }
            else if (!filecontext.validatepath())
            {
                _logger.logpathmismatch(filecontext.subpath);
            }
            else if (!filecontext.lookupcontenttype())
            {
                _logger.logfiletypenotsupported(filecontext.subpath);
            }
            else if (!filecontext.lookupfileinfo())
            {
                _logger.logfilenotfound(filecontext.subpath);
            }
            else
            {
                // if we get here, we can try to serve the file
                filecontext.comprehendrequestheaders();
                switch (filecontext.getpreconditionstate())
                {
                    case staticfilecontext.preconditionstate.unspecified:
                    case staticfilecontext.preconditionstate.shouldprocess:
                        if (filecontext.isheadmethod)
                        {
                            await filecontext.sendstatusasync(constants.status200ok);
                            return;
                        }
                        try
                        {
                            if (filecontext.israngerequest)
                            {
                                await filecontext.sendrangeasync();
                                return;
                            }
                            await filecontext.sendasync();
                            _logger.logfileserved(filecontext.subpath, filecontext.physicalpath);
                            return;
                        }
                        catch (filenotfoundexception)
                        {
                            context.response.clear();
                        }
                        break;
                    case staticfilecontext.preconditionstate.notmodified:
                        _logger.logpathnotmodified(filecontext.subpath);
                        await filecontext.sendstatusasync(constants.status304notmodified);
                        return;

                    case staticfilecontext.preconditionstate.preconditionfailed:
                        _logger.logpreconditionfailed(filecontext.subpath);
                        await filecontext.sendstatusasync(constants.status412preconditionfailed);
                        return;

                    default:
                        var exception = new notimplementedexception(filecontext.getpreconditionstate().tostring());
                        debug.fail(exception.tostring());
                        throw exception;
                }
            }
            await _next(context);
        }

当httpcontext进入此中间件后会尝试封装成staticfilecontext, 然后对其逐步判断,例如请求的url是否与设置的静态目录一致, 判断文件是否存在,判断文件类型等,

若符合要求 ,会进一步判断文件是否有修改等。

五、静态文件的授权管理

默认情况下,静态文件是不需要授权,可以公开访问的。

因为即使采用了授权, app.useauthentication(); 一般也是写在 app.usestaticfiles() 后面的,那么如果我们想对其进行授权管理,首先想到可以改写 staticfilemiddleware 这个中间件,

在其中添加一些自定义的判断条件,但貌似不够友好。而且这里只能做一些大类的判断,比如请求的ip地址是否在允许范围内这样的还行,如果要根据登录用户的权限来判断(比如用户只能看到自己上传的图片)就不行了,

因为权限的判断写在这个中间件之后。所以可以通过filter的方式来处理,首先可以在应用目录中新建一个"images"文件夹, 而这时就不要把它设置为静态文件目录了,这样这个"images"目录的文件默认情况下是不允许访问的,

然后通过controller返回文件的方式来处理请求,如下代码所示

    [route("api/[controller]")]
    [authorizefilter]
    public class filecontroller : controller
    {
        [httpget("{name}")]
        public fileresult get(string name)
        {
            var file = path.combine(directory.getcurrentdirectory(), "images", name);

            return physicalfile(file, "application/octet-stream");
        }

    }

在authorizefilter中进行相关判断,代码如下

    public class authorizefilter: actionfilterattribute
    {
        public override void onactionexecuting(actionexecutingcontext context)
        {
            base.onactionexecuting(context);

            if (context.routedata.values["controller"].tostring().tolower().equals("file"))
            {
                bool isallow = false;//在此进行一系列访问权限验证,如果失败,返回一个默认图片,例如logo或不允许访问的提示图片

                if (!isallow)
                {
                    var file = path.combine(directory.getcurrentdirectory(), "images", "default.png");

                    context.result = new physicalfileresult(file, "application/octet-stream");

                }
            }
        }
    }
本文链接:https://2i3i.com/aspnetcore2_14.html ,转载请注明来源地址。
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇