【问题标题】:how to use node-sspi with fastify如何在 fastify 中使用 node-sspi
【发布时间】:2020-08-27 15:08:38
【问题描述】:

我想用https://github.com/abbr/nodesspi

我正在尝试使用 justify 而不是 express。看起来它应该可以工作,但事实并非如此。它几乎等同于 express sn-p。我在身份验证函数中收到一个错误,告诉我我传递了错误的参数。

const fastify = require('fastify')({
  logger: true,
})

fastify.route({
  method: 'GET',
  url: '/login',
  onRequest: function (req, res, next) {
    var nodeSSPI = require('node-sspi')
    var nodeSSPIObj = new nodeSSPI({
      retrieveGroups: true
    })
    nodeSSPIObj.authenticate(req, res, function (err) {
      res.finished || next()
    })
  },
  handler: function (req, res, next) {
    var out =
      'Hello ' +
      req.connection.user +
      '! Your sid is ' +
      req.connection.userSid +
      ' and you belong to following groups:<br/><ul>'
    if (req.connection.userGroups) {
      for (var i in req.connection.userGroups) {
        out += '<li>' + req.connection.userGroups[i] + '</li><br/>\n'
      }
    }
    out += '</ul>'
    res.send(out)
  }
})

fastify.listen(4000, err => {
  if (err) throw err
})

"fastify": "^3.3.0", "node-sspi": "^0.2.8",

【问题讨论】:

    标签: node.js sspi fastify node-sspi


    【解决方案1】:

    问题是 express like 中间件在 fastify 实例上不起作用。解决方案涉及fastify-express

    const fastify = require('fastify')({ logger: true })
    
    fastify.register(async (fastify) => {
      await fastify.register(require('fastify-express'))
      const nodeSSPI = require('node-sspi')
      const nodeSSPIObj = new nodeSSPI()
    
      fastify.use((req, reply, done) => {
        nodeSSPIObj.authenticate(req, reply, (err) => {
          if (err) return reply.code(500).send({ error: err })
          reply.finished || done()
        })
      })
    
      fastify.get('/', (req, reply) => {
        const { userSid, user, userGroups } = req.connection
        return reply.send({ userSid, user, userGroups })
      })
    })
    
    fastify.listen(3000, (err) => {
      if (err) throw err
    })
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-05
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 2020-05-09
      • 1970-01-01
      • 2021-10-23
      相关资源
      最近更新 更多