【问题标题】:How can I stop calls to the database for each image request? [finished]如何停止对每个图像请求的数据库调用? [完成的]
【发布时间】:2018-02-20 01:36:24
【问题描述】:

Passport with Google Auth 正在验证对我的服务器的每个图像请求。

身份验证调用passport.deserialize()

为了反序列化,我调用我的数据库并在此处提取用户配置文件:

passport.deserializeUser(function(id, done) {
  User.findById(id, function(err, user) {
    done(err, user);
  });
});

这里的文档:

http://www.passportjs.org/docs/

【问题讨论】:

  • 您是否需要以任何方式限制对图像的访问?我的意思是说,您是否关心未经身份验证的用户是否会访问您的图片?
  • 是的,对于每个需要身份验证的项目。

标签: javascript express google-oauth passport.js


【解决方案1】:

由于您不关心您的图像是否受到保护,因此最简单的方法是不让护照验证这些路线。请务必记住,.use() 语句的顺序很重要。

考虑:

// this will not get authenticated
app.use(express.static('public'));

// All routes setup after this will get authenticated using the local strategy. 
app.use(passport.authenticate('local'));

【讨论】:

  • 同样,Morgan 在静态中间件之后添加到中间件堆栈。或者仅在某些 url 模式上定义它。
猜你喜欢
  • 2021-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-12
  • 1970-01-01
  • 2022-10-24
相关资源
最近更新 更多