我正在创建一个express微服务体系结构,并使用快递网关作为API网关。
我可以通过快速网关公开我的服务和端点,其中一个服务(Books)有两个角色(管理,用户),有两个不同的登录起点(管理员使用JWT,用户使用Firebase auth)。
我成功地将管理端点/v1/admin与快递网关提供的JWT一起安全了,现在我想创建一个策略/插件(我不明白其中的区别)来使用我的CheckIfAuthenticatedFirebase中间件来保护我的用户端点/v1/user。
因此,我需要一些帮助,以了解我是否必须创建一个插件或一个政策和步骤来做它。
这是我的gateway.config.yml:
http:
port: 8080
admin:
port: 9876
host: localhost
apiEndpoints:
bookAdmin:
path: '/v1/admin*'
bookUser:
path: '/v1/user*'
serviceEndpoints:
book:
url: 'http://localhost:5000'
policies:
- cors
- log
- proxy
- jwt
- request-transformer
pipelines:
bookAdminPipeline:
apiEndpoints:
- bookAdmin
policies:
-
cors:
action:
origin: '*'
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE'
-
jwt:
action:
secretOrPublicKey: 'JWTKey'
checkCredentialExistence: false
-
proxy:
action:
serviceEndpoint: book
bookUserPipeline:
apiEndpoints:
- bookUser
policies:
-
cors:
action:
origin: '*'
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE'
-
proxy:
action:
serviceEndpoint: book这是我的firebase-middleware.js:
var admin = require('../authentication/firebase');
getAuthToken = (req, res, next) => {
if (
req.headers.authorization &&
req.headers.authorization.split(' ')[0] === 'Bearer'
) {
req.authToken = req.headers.authorization.split(' ')[1];
} else {
req.authToken = null;
}
next();
};
checkIfAuthenticated = (req, res, next) => {
getAuthToken(req, res, async () => {
try {
const { authToken } = req;
const userInfo = await admin
.auth()
.verifyIdToken(authToken);
req.authId = userInfo.uid;
return next();
} catch (e) {
return res
.status(401)
.send({ error: 'You are not authorized to make this request' });
}
});
};
module.exports = checkIfAuthenticated非常感谢
发布于 2020-06-03 22:21:59
您必须创建一个策略并通过插件调用它。
在您的插件目录中创建一个文件夹,假设文件夹名是auth。在该manifest.js
module.exports ={module.exports={ version:'1.2.0',init: pluginContext){ let module.exports=require(‘./ policy /auth’) pluginContext.registerPolicy(policy) },策略:‘auth’,//这是让CLI自动添加到gateway.config模式中的“策略”白名单:{ //这是让CLI询问params‘例如插件配置customer-auth’$id:“https://express-gateway.io/schemas/plugins/blacklist.json”}
module.exports ={ name:'auth',schema:{ $id:'http://express-gateway.io/schemas/policies/example-policy.json',type:'object',属性:{ baseUrl:{ type:'string',格式:'url',默认值:‘} },策略:(actionParams) => { const = this;返回(req,res,next) => { //自定义逻辑};};
现在,您只需将清单路径放入system.config.yml中即可。
插件: auth: package:“./plugins/auth/plom.js”
最后一步是在策略部分的gateway.config.yml文件中声明此策略。
策略:-基本- auth - cors -表达式-请求-转换器-密钥-auth-日志- oauth2 -代理-速率-限制-auth
现在,您可以像使用任何其他策略一样轻松地使用它。:)
https://stackoverflow.com/questions/62181135
复制相似问题