Feathers Js 在服务器端限制对页面的访问

Feathers Js Restrict Access To Page on Server Side

我正在使用 feathers.js 并试图限制已登录用户访问支付-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() returns false,即使用户已登录。有没有办法将 public 目录中的页面的访问权限限制为仅允许用户访问已登录?

要在页面加载场景中进行限制,您首先需要确保令牌在 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 对象上以在内部进行授权的一项服务。这就是您 API 面对 CSRF 攻击的方式。

此外,如果您完全启用了 CORS,您很可能希望确保为呈现中间件禁用 CORS。仅在您的 Feathers 服务之前启用 CORS。

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

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