当前位置: 首页 > news >正文

山西网站建设多少钱海外独立站

山西网站建设多少钱,海外独立站,宜春网站制作,网站转app免费odoo 17 后端路由接口认证自定义 在接口中, 我们都知道有3中常用的认证方式 user 用户级认证public 访问时赋予公共用户none 不做任何用户级处理 一般不做数据库重要数据校验, 仅做访问处理 以上是源码提供的三种方式 接下来我们自定义一个认证方式 首先找到的这认证是在…

odoo 17 后端路由接口认证自定义

在接口中, 我们都知道有3中常用的认证方式

  • user 用户级认证
  • public 访问时赋予公共用户
  • none 不做任何用户级处理 一般不做数据库重要数据校验, 仅做访问处理

以上是源码提供的三种方式

接下来我们自定义一个认证方式

首先找到的这认证是在 ir.http 模型内完成的, 认真源码如下

@classmethod
def _authenticate(cls, endpoint):auth = 'none' if http.is_cors_preflight(request, endpoint) else endpoint.routing['auth']try:if request.session.uid is not None:if not security.check_session(request.session, request.env):request.session.logout(keep_db=True)request.env = api.Environment(request.env.cr, None, request.session.context)getattr(cls, f'_auth_method_{auth}')()except (AccessDenied, http.SessionExpiredException, werkzeug.exceptions.HTTPException):raiseexcept Exception:_logger.info("Exception during request Authentication.", exc_info=True)raise AccessDenied()

源码中可以看到 getattr(cls, f'_auth_method_{auth}')() 这里匹配并执行, 很明显可以看到调用了这个类的方法 _auth_method_user, _auth_method_public, _auth_method_none 往后继续操作

@classmethod
def _get_public_users(cls):return [request.env['ir.model.data']._xmlid_to_res_model_res_id('base.public_user')[1]]@classmethod
def _auth_method_user(cls):if request.env.uid in [None] + cls._get_public_users():raise http.SessionExpiredException("Session expired")@classmethod
def _auth_method_none(cls):request.env = api.Environment(request.env.cr, None, request.env.context)

这时候我们只需继承 ir.http 模型进行操作, 下面是例子

from odoo import models
from odoo.http import requestclass IrHttp(models.AbstractModel):_inherit = 'ir.http'@classmethoddef _auth_method_wechat(cls):user = request.env.ref('xxx.user_wxapp_user_manager')  # 给后端用户作为认证request.uid = user.idrequest.user = user

这里以通过添加微信认证wechat为例, 将模块安装后,系统会增加一个微信认证功能, 更多操作请在方法中编写逻辑

main.py 文件

普通接口访问如下

@http.route('/demo/auth/reload', type='http', auth='user', methods=['GET'], cors='*', csrf=False)
def auth_reload(self):"""认证接口, 需要通过用户级认证"""return Response(json.dumps({}), status=200, content_type='application/json')

自定义认证访问, 只需修改 auth='wechat' 即可

@http.route('/demo/auth/reload', type='http', auth='wechat', methods=['GET'], cors='*', csrf=False)
def auth_reload(self):"""自定义认证接口, 通过 _auth_method_wechat 方法处理用户需求"""return Response(json.dumps({}), status=200, content_type='application/json')

最后是模块的目录结构

└─demo│  __init__.py│  __manifest__.py│├─controllers│      main.py│      __init__.py│├─models│      ir_http.py│      __init__.py

注意, 别忘了将 controllers 和 models 导出到包外

http://www.dt0577.cn/news/7728.html

相关文章:

  • 生物制药公司网站建设比百度好用的搜索引擎
  • 义乌1688网站网页设计通州优化公司
  • 小程序开发网站设计制作淘宝指数
  • 宁波房产信息网官方网站seo网站优化方案摘要
  • 58同城给做网站搜狗搜索引擎优化指南
  • 网站建设需要做什么准备工作最近热搜新闻事件
  • 做营销型网站多少钱静态网站模板
  • 谈谈网站建设会有哪些问题南沙seo培训
  • 内网建设网站需要什么条件北京关键词优化报价
  • 手机端首页设计福州seo公司
  • 做网站需要购买地域名吗优化排名推广关键词
  • 珍岛做网站怎么样网络软文推广平台
  • 网站设计模式有哪些四川省人民政府官网
  • 软件开发好做吗惠州seo按天计费
  • 网站前期准备工作网页开发公司
  • 深圳鼎诚网站建设信息流广告是什么意思
  • 淄博网站建设哪家好无锡网站建设优化公司
  • 温州建设网站制作数据分析培训机构哪家好
  • yellow片观看完整版广东网站seo
  • 公司网站怎么做百度竞价广州专门做seo的公司
  • 做网站做58好还是赶集好整站优化的公司
  • c2c电子商务网站的建站目的排名优化推广
  • 提供网站建设课程代码百度指数的网址是什么
  • 网站建设前期准备方案大数据精准获客软件
  • 网站备案费一般是多少seo流量优化
  • 房产网手机版网站建设目标seo技巧与技术
  • 科技风格网站专业seo整站优化
  • 做网站能收回吗天津网站策划
  • 凡科免费做的网站百度搜索引擎下载免费
  • 自己做网站视频怎么在百度发广告