【问题标题】:Feathers Js Restrict Access To Page on Server SideFeathers Js 限制对服务器端页面的访问
【发布时间】:2017-02-24 20:40:31
【问题描述】:

我正在使用 feathers.js,并试图限制已登录的用户访问 payment-info.html 页面。

const app = feathers();

app.configure(configuration(path.join(__dirname, '..')));

app.use(compress())
  .options('*', cors())
  .use(cors())
  .use(favicon( path.join(app.get('public'), 'favicon.ico') ))

  .use('/payment-info.html', function(req,res,next){
  if(req.isAuthenticated()){
    next();
  } else {
    // 401 Not Authorized
    next(new Error(401));
  }
  })

  .use('/', serveStatic( app.get('public') ))
  .use(bodyParser.json())
  .use(bodyParser.urlencoded({ extended: true }))
  .configure(hooks())
  .configure(rest())
  .configure(socketio())
  .configure(services)
  .configure(middleware);

module.exports = app;

但是,即使用户已登录,req.isAuthenticated() 也会返回 false。有没有办法将公共目录中的页面的访问权限限制为只有已登录的用户?

【问题讨论】:

    标签: javascript express feathersjs


    【解决方案1】:

    要在页面加载场景中进行限制,您首先需要确保令牌在 cookie 中。查看 feathers-authentication documentation 了解如何启用 cookie。但非常重要的是,您要小心不要通过 cookie 将自己暴露在 CSRF 攻击中。

    使用当前版本的 feathers-authentication 插件,您必须手动进行设置。您需要从 cookie 中读取令牌以供渲染中间件使用:

    const jwt = require('jsonwebtoken');
    const cookieParser = require('cookie-parser');
    
    app.use(cookieParser());
    app.use('/payment-info.html', function(req, res, next) {
      let token = req.cookies['feathers-jwt'];
      if (token) {
        // Get the JWT secret to verify the token.
        let secret = app.get('auth').token.secret;
        jwt.verify(token, secret, function(err, decoded) {
          if (err) {
            return res.status(401).send('You are not authorized to view that page.');
          }
          return next();
        });
      } else {
        return res.status(401).send('You are not authorized to view that page.');
      }
    });

    绝对不要让任何服务直接使用 cookie 中的令牌,这一点很重要。渲染中间件可以拉取令牌并使用它来发出服务请求,就好像它只是另一个客户端一样,但是您永远不想从 cookie 中拉取它并将其放置在 req.feathers 对象上以在 a 内部进行授权服务。这就是您打开 API 以应对 CSRF 攻击的方式。

    此外,如果您要启用 CORS,您很可能希望确保为渲染中间件禁用 CORS。仅在您的 Feathers 服务之前启用 CORS。

    feathers-authentication@0.7.x 的另一个缺点是 cookie 的过期时间与令牌的过期时间不匹配。如文档中所述,您需要手动设置 cookie 的 maxAge 过期时间以匹配您希望令牌有效的时间。

    feathers-authentication@1.x.x(目前处于预发布状态)将包括对服务器端渲染的更好支持,因此您不必自己连接它。它还将负责使 cookie 与令牌一起过期。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多