使用来自node.js的凭证反向代理login

我目前有一个服务器在Tomcat servlet中使用Shiro Token系统运行Spring,以检查用户是否已经login。 它允许跨域请求。

在任何其他领域,我可以在客户端(通常使用angular度)调用…

http.get('https://<my_check_login_service>', {withCredentials: true})

…只要我已经login(令牌不过期)返回用户信息(名称,头像等)。

我现在有另一个系统是一个节点服务器(也为客户端服务angular),我想调用节点服务器,并代理到上面的my_check_login_service获取用户,设置会话对象的信息(使用快递),然后将用户返回给客户端。 而且,通过会话对象,允许我信任他们的连接,并允许他们根据从login服务返回的用户的安全级别执行进一步的API调用。

在node.js路由器,我可以代理这样做…

 app.get('/checklogin', function(req, res) { req.pipe(request.get("https://<my_check_login_service>").pipe(res); } 

…但我不知道如何将正确的凭据传递给服务。 如果我做 …

http.get('checkLogin', {withCredentials: true})

…当然,它不起作用,因为我的login_service的凭据不会发送到本地服务器。 我怎样才能通过正确的凭据,使这项工作?

干杯。

证书最有可能在HTTP标头中,传递所有头文件(包括请求和响应),原始请求的地址应使其工作:

 app.get('/checklogin', function(req, res) { console.dir(req.headers) //You can inspect the headers here and pass only required values const options = { url: 'https://<my_check_login_service>', headers: Object.assign( //Tell the login service about address of original request {'X-Forwarded-For': req.connection.remoteAddress} req.headers) } req.pipe(request.get(options)) .on('response', (response) => res.set(response.headers)) .pipe(res) } 

这个例子通过设置X-Forwarded-For传递原始地址, login_service可以识别它或者不依赖于configuration。