使用node.js中的凭据进行反向代理登录

cro*_*umb 6 proxy shiro node.js express

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

在任何其他域我可以在客户端(通常使用角度)调用...

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

...只要我已经登录(令牌未过期),就会返回用户信息(姓名,头像等).

我现在有另一个系统,它是一个节点服务器(也为客户端提供角度),我想称之为节点服务器并让它代理到上面的my_check_login_service以获取用户,在会话对象上设置信息(使用快递),然后将用户返回给客户端.而且,通过会话对象,让我相信自己的连接,并允许他们执行取决于从登录服务返回的用户的安全水平进一步API调用.

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

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

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

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

...当然,它不起作用,因为我的login_service的凭据不会发送到本地服务器.如何通过正确的凭据才能使其正常工作?

干杯.

Dar*_*ght 2

凭证很可能位于 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)
}
Run Code Online (Sandbox Code Playgroud)

此示例通过设置传递原始地址X-Forwarded-For,login_service可能会识别它...也可能不会,具体取决于配置。