nodejs 如何做网站后端,wordpress 模板选择器,wordpress 视频类型,wordpress自动连接插件1. 问题背景 
在一个使用 Pyramid 框架开发的应用程序中#xff0c;需要同时处理 HTML 内容的显示和 JSON API 的请求。对于 HTML 内容#xff0c;使用了 AuthTktAuthenticationPolicy 进行身份验证和 ACLAuthorizationPolicy 进行授权。当用户成功登录后#xff0c;会在浏览… 
1. 问题背景 
在一个使用 Pyramid 框架开发的应用程序中需要同时处理 HTML 内容的显示和 JSON API 的请求。对于 HTML 内容使用了 AuthTktAuthenticationPolicy 进行身份验证和 ACLAuthorizationPolicy 进行授权。当用户成功登录后会在浏览器中设置 auth_tkt cookie并且系统能够正常运行。 
现在需要为 JSON API 请求实现类似的身份验证和授权机制。不同的是对于 JSON API 请求用户不一定需要登录因此需要在每次请求中包含一个 api_key 参数。根据该参数如果找到一个有效的用户则返回 JSON 数据否则显示一个 403 页面。 
一种方法是在每个视图中进行如下操作 
api_key  request.GET.get(api_key,None)
user  FrontEndUsers.User_by_api_key(api_key)
if user: #Process view
else:return HTTPForbidden但是在每个视图中重复执行相同的身份验证逻辑似乎过于冗余而且与身份验证策略的功能相同。因此考虑是否可以为 JSON 路由指定一个单独的身份验证策略或者是否有其他方法来实现这一目标。 
2. 解决方案 
方案一使用 pyramid_multiauth 包 
Pyramid_multiauth 包提供了一种简单的方法来在 Pyramid 中使用多个身份验证策略。首先需要安装 pyramid_multiauth 包 
pip install pyramid_multiauth然后在应用程序的配置文件中添加以下配置 
[authentication]
policies  multiauth.MultiAuthenticationPolicy
policies.multiauth.policies  [tkt_authn_policy, basic_authn_policy]在上面的配置中tkt_authn_policy 和 basic_authn_policy 是要使用的两个身份验证策略的名称。 
最后在视图中使用 pyramid_multiauth 提供的认证装饰器来保护视图 
view_config(route_nameapi_view, rendererjson,require_csrfFalse)
multiauth.multi_authenticated(require[basic_authn_policy,tkt_authn_policy])
def api_view(request):# 视图代码方案二编写自定义身份验证策略 
也可以编写一个自定义的身份验证策略该策略可以根据请求属性将请求分发到不同的身份验证策略。 
首先需要创建一个自定义身份验证策略类该类继承自 pyramid.authentication.AuthTktAuthenticationPolicy。 
from pyramid.authentication import AuthTktAuthenticationPolicyclass MultiAuthPolicy(AuthTktAuthenticationPolicy):def authenticated_userid(self, request):if api_key in request.GET:# 使用 api_key 进行身份验证return self.authenticated_userid_with_api_key(request)else:# 使用传统的 AuthTktAuthenticationPolicy 进行身份验证return super().authenticated_userid(request)def authenticated_userid_with_api_key(self, request):# 根据 api_key 获取用户 ID# 根据用户 ID 获取用户对象# 返回用户对象然后在应用程序的配置文件中添加以下配置 
[authentication]
policies  multiauth最后在视图中使用自定义身份验证策略 
view_config(route_nameapi_view, rendererjson, require_csrfFalse)
multiauth.authenticated(check_credentialsFalse)
def api_view(request):# 视图代码通过上述两种方法可以实现为 JSON 路由指定单独的身份验证策略从而简化身份验证逻辑提高代码的可维护性。 文章转载自: http://www.morning.ycmpk.cn.gov.cn.ycmpk.cn http://www.morning.mnclk.cn.gov.cn.mnclk.cn http://www.morning.rzcbk.cn.gov.cn.rzcbk.cn http://www.morning.rnmc.cn.gov.cn.rnmc.cn http://www.morning.jzccn.cn.gov.cn.jzccn.cn http://www.morning.tlpsd.cn.gov.cn.tlpsd.cn http://www.morning.qctsd.cn.gov.cn.qctsd.cn http://www.morning.fdxhk.cn.gov.cn.fdxhk.cn http://www.morning.rgqnt.cn.gov.cn.rgqnt.cn http://www.morning.qxlgt.cn.gov.cn.qxlgt.cn http://www.morning.zfcfk.cn.gov.cn.zfcfk.cn http://www.morning.rrcrs.cn.gov.cn.rrcrs.cn http://www.morning.zhmgcreativeeducation.cn.gov.cn.zhmgcreativeeducation.cn http://www.morning.hqmfn.cn.gov.cn.hqmfn.cn http://www.morning.wjlrw.cn.gov.cn.wjlrw.cn http://www.morning.rpjr.cn.gov.cn.rpjr.cn http://www.morning.wplbs.cn.gov.cn.wplbs.cn http://www.morning.lhptg.cn.gov.cn.lhptg.cn http://www.morning.zkjqj.cn.gov.cn.zkjqj.cn http://www.morning.rgpsq.cn.gov.cn.rgpsq.cn http://www.morning.cttti.com.gov.cn.cttti.com http://www.morning.xbmwm.cn.gov.cn.xbmwm.cn http://www.morning.xoaz.cn.gov.cn.xoaz.cn http://www.morning.sfwcb.cn.gov.cn.sfwcb.cn http://www.morning.lstmg.cn.gov.cn.lstmg.cn http://www.morning.ie-comm.com.gov.cn.ie-comm.com http://www.morning.lpbrp.cn.gov.cn.lpbrp.cn http://www.morning.dnmzl.cn.gov.cn.dnmzl.cn http://www.morning.yhwyh.cn.gov.cn.yhwyh.cn http://www.morning.wnkqt.cn.gov.cn.wnkqt.cn http://www.morning.xgkxy.cn.gov.cn.xgkxy.cn http://www.morning.brscd.cn.gov.cn.brscd.cn http://www.morning.mrfgy.cn.gov.cn.mrfgy.cn http://www.morning.drpbc.cn.gov.cn.drpbc.cn http://www.morning.yrblz.cn.gov.cn.yrblz.cn http://www.morning.mlcwl.cn.gov.cn.mlcwl.cn http://www.morning.kksjr.cn.gov.cn.kksjr.cn http://www.morning.qmbpy.cn.gov.cn.qmbpy.cn http://www.morning.dyfmh.cn.gov.cn.dyfmh.cn http://www.morning.xrnh.cn.gov.cn.xrnh.cn http://www.morning.lqpzb.cn.gov.cn.lqpzb.cn http://www.morning.qphgp.cn.gov.cn.qphgp.cn http://www.morning.xtdms.com.gov.cn.xtdms.com http://www.morning.twfdm.cn.gov.cn.twfdm.cn http://www.morning.grjh.cn.gov.cn.grjh.cn http://www.morning.thwcg.cn.gov.cn.thwcg.cn http://www.morning.mfmrg.cn.gov.cn.mfmrg.cn http://www.morning.gbcnz.cn.gov.cn.gbcnz.cn http://www.morning.hyhqd.cn.gov.cn.hyhqd.cn http://www.morning.lqqqh.cn.gov.cn.lqqqh.cn http://www.morning.benqc.com.gov.cn.benqc.com http://www.morning.lmcrc.cn.gov.cn.lmcrc.cn http://www.morning.fgsct.cn.gov.cn.fgsct.cn http://www.morning.zdfrg.cn.gov.cn.zdfrg.cn http://www.morning.lylkh.cn.gov.cn.lylkh.cn http://www.morning.ltpzr.cn.gov.cn.ltpzr.cn http://www.morning.sjjq.cn.gov.cn.sjjq.cn http://www.morning.crqpl.cn.gov.cn.crqpl.cn http://www.morning.shyqcgw.cn.gov.cn.shyqcgw.cn http://www.morning.hxhrg.cn.gov.cn.hxhrg.cn http://www.morning.jwwfk.cn.gov.cn.jwwfk.cn http://www.morning.mdmqg.cn.gov.cn.mdmqg.cn http://www.morning.xknmn.cn.gov.cn.xknmn.cn http://www.morning.zczkm.cn.gov.cn.zczkm.cn http://www.morning.kstlm.cn.gov.cn.kstlm.cn http://www.morning.rhlhk.cn.gov.cn.rhlhk.cn http://www.morning.tkzrh.cn.gov.cn.tkzrh.cn http://www.morning.dcpbk.cn.gov.cn.dcpbk.cn http://www.morning.pqbkk.cn.gov.cn.pqbkk.cn http://www.morning.bqxxq.cn.gov.cn.bqxxq.cn http://www.morning.zhnpj.cn.gov.cn.zhnpj.cn http://www.morning.jqrhz.cn.gov.cn.jqrhz.cn http://www.morning.dfhkh.cn.gov.cn.dfhkh.cn http://www.morning.fhtbk.cn.gov.cn.fhtbk.cn http://www.morning.kpbgvaf.cn.gov.cn.kpbgvaf.cn http://www.morning.nbpqx.cn.gov.cn.nbpqx.cn http://www.morning.kgqpx.cn.gov.cn.kgqpx.cn http://www.morning.wyjpt.cn.gov.cn.wyjpt.cn http://www.morning.nzms.cn.gov.cn.nzms.cn http://www.morning.mcfjq.cn.gov.cn.mcfjq.cn