http-headers - 如何在 LoopBack 的控制器函数中访问标头?
问题描述
我在环回中有一个函数,它使用它自己特殊的身份验证形式。我不想让这个身份验证阶段与端点本身分开完成。我想在端点代码中进行身份验证。
为此,我需要访问 Authorization 标头。
如何在环回控制器功能中执行此操作?
@get('/item/{itemId}', {
description: `Get a specific item`,
responses: {}
})
async getItem(
@param.path.string('itemId') itemId: string,
): Promise<LabResult[]> {
// How do I get headers from here?
const auth = somehowGetHeaders().get("Authorization");
}
解决方案
您可以通过注入请求对象来访问标头。例如 。. .
@get('/item/{itemId}', {
description: `Get a specific item`,
responses: {}
})
async getItem(
@param.path.string('itemId') itemId: string,
@inject(RestBindings.Http.REQUEST) private req: Request
): Promise<LabResult[]> {
console.log('headers', req.headers);
// using header information here you can authenticate
}
推荐阅读
- javascript - p:commandLink window.onload
- c# - 为什么传递变量失败,错误值类型“字符串”无法转换为“字符串()”?
- php - 如何根据重音区分确切的字符?
- ipfs - 如何区分“ipfs object links”“ipfs object stat”?我的意思是文件的大小
- python - 如何防止自动toctree条目编号?
- python - 如何在神经网络中手动计算分类?
- php - 组合 2 个 For Each 循环 PHP
- mysql - 我有一个包含非英文单词的文本文件,我需要将它放入 mysql 我该怎么做?
- java - 将 Kotlin 算法翻译成 Java 代码
- paypal - 使用 PayPal 订阅 API 收取税款