溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何進(jìn)行NoAuthMiddlewareBase及NoAuthMiddleware解析

發(fā)布時(shí)間:2021-12-28 15:46:45 來源:億速云 閱讀:124 作者:柒染 欄目:云計(jì)算

這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)碛嘘P(guān)如何進(jìn)行NoAuthMiddlewareBase及NoAuthMiddleware解析,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

NoAuthMiddlewareBase解析

class NoAuthMiddlewareBase(base_wsgi.Middleware):"""如果請(qǐng)求頭里沒有指定對(duì)應(yīng)的請(qǐng)求令牌,則返回一個(gè)偽造的令牌."""    def base_call(self, req, project_id_in_path, always_admin=True):if 'X-Auth-Token' not in req.headers:
            user_id = req.headers.get('X-Auth-User', 'admin')#設(shè)置默認(rèn)的用戶ID
            project_id = req.headers.get('X-Auth-Project-Id', 'admin')#設(shè)置默認(rèn)的項(xiàng)目IDif project_id_in_path:
                os_url = '/'.join([req.url.rstrip('/'), project_id])else:
                os_url = req.url.rstrip('/')
            res = webob.Response()# NOTE(vish): This is expecting and returning Auth(1.1), whereas            #             keystone uses 2.0 auth.  We should probably allow            #             2.0 auth here as well.            res.headers['X-Auth-Token'] = '%s:%s' % (user_id, project_id)#偽造token
            res.headers['X-Server-Management-Url'] = os_url #設(shè)置url
            res.content_type = 'text/plain' #設(shè)置content-type            res.status = '204'            return res

        token = req.headers['X-Auth-Token']
        user_id, _sep, project_id = token.partition(':')
        project_id = project_id or user_id
        remote_address = getattr(req, 'remote_address', '127.0.0.1')if CONF.api.use_forwarded_for:
            remote_address = req.headers.get('X-Forwarded-For', remote_address)
        is_admin = always_admin or (user_id == 'admin')
        ctx = context.RequestContext(user_id,
                                     project_id,                                     is_admin=is_admin,                                     remote_address=remote_address)#封裝請(qǐng)求上下文

        req.environ['nova.context'] = ctxreturn self.application

NoAuthMiddleware解析

class NoAuthMiddleware(NoAuthMiddlewareBase):
#繼承父類    
@webob.dec.wsgify(RequestClass=wsgi.Request)def __call__(self, req):
return self.base_call(req, True, always_admin=False)
#調(diào)用父類的具體實(shí)現(xiàn)

上述就是小編為大家分享的如何進(jìn)行NoAuthMiddlewareBase及NoAuthMiddleware解析了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI