【问题标题】:Express (NodeJS) - authentication for proxy appExpress (NodeJS) - 代理应用程序的身份验证
【发布时间】:2020-12-29 03:15:18
【问题描述】:

我有两台服务器:

  • 服务器 A - Express/FeathersJS - 可公开访问,通过 jwt 进行身份验证
  • 服务器 X - Django 应用程序 - 不可公开访问 - 无需身份验证

服务器 A 由几个 API、一个通​​过 JWT 的身份验证 API 和一个前端 javascript 应用程序组成,以允许用户访问 API 和登录。

Server X 包含一个前端应用和一些 API,但不能公开访问,也没有身份验证来访问这些应用。

我的目标是让用户登录到服务器 A,然后通过代理访问服务器 X。

所以 - 我已经实现了 express 中间件 http-proxy-middleware。它的工作原理,除非我尝试使用某种身份验证来保护路由 - 验证 JWT 的内置方法需要一个 Authorization bearer jwt 标头,这对于 GET 请求来说是不可能的,就像在我的情况下访问这个应用。

大家有什么建议吗?

【问题讨论】:

    标签: node.js express proxy jwt middleware


    【解决方案1】:

    解决方案:

    如此处所述:How to add a users auth in feathers middleware?

    1. 登录成功后在document.cookie中设置cookie。
    
        app.authenticate({
            strategy: 'local',
            username: $('#inputUsername').val(),
            password: $('#inputPassword').val(),
            
        }).then( result => {
    
            document.cookie = "feathers-jwt=" + result.accessToken;
            window.location.href = "/";
            
        }).catch(error => {});
    
    
    1. 在代理路由上解析 cookie:
    // authenticateCookie.js
    
    module.exports = function authenticateCookie(req, res, next){
        const cookies = req.cookies;
        const token = cookies['feathers-jwt'];
        
        if(token){
            logger('Found cookie in feathers-jwt');
            req.authentication = {
                strategy: 'jwt',
                accessToken: token,
            };
        }
        
        next();
    };
    
    // proxyMiddleware.js
    
        app.use(urlRegex, 
    
            // parse and handle jwt cookies
            cookieParser(),
            authenticateCookie,
    
            // logging function
            (req, res, next) => {
                logger(req.url);
                next();
            },  
    
            // validate jwt cookie
            authenticate('jwt'),
    
            // proxy requests upstream
            createProxyMiddleware({
                target: url,
                changeOrigin: true,
                auth: auth,
            }), 
        )
    

    【讨论】:

      猜你喜欢
      • 2015-07-03
      • 1970-01-01
      • 2014-10-07
      • 1970-01-01
      • 1970-01-01
      • 2017-07-28
      • 1970-01-01
      • 1970-01-01
      • 2017-07-02
      相关资源
      最近更新 更多