我有 Node.js 微服务在 Nginx 服务器
后面运行我创建了一个新的 Node.js 服务来验证和授权访问所有其他服务
我希望nginx允许根据身份验证服务响应访问请求的服务
例如:
如果用户请求"/用户/ID/编辑">
我希望 Nginx 首先将请求定向到"/auth">
然后基于"/auth"响应Nginx决定继续使用"/user/id/edit">
或返回错误作为响应
我找不到有关此案例的任何教程或文档
从Spotify-Web-Auth-API-Example中获取的示例代码:
app.get('/login', function(req, res) {
var state = generateRandomString(16);
res.cookie(stateKey, state);
// your application requests authorization
var scope = 'user-read-private user-read-email';
res.redirect('https://accounts.spotify.com/authorize?' +
querystring.stringify({
response_type: 'code',
client_id: client_id,
scope: scope,
redirect_uri: redirect_uri,
state: state
}));
});
从上面的代码中可以看出,在节点.js代码本身中,您可以执行重定向(将请求重定向到登录端点(到授权服务,然后控制访问。
您不需要显式的nginx配置。
此外,请阅读此相关查询 - Node.js应用程序的授权方法和设计模式