簡體   English   中英

如何在 fastify 中使用 node-sspi

[英]how to use node-sspi with fastify

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

我正在嘗試使用 justify 而不是 express。 看起來它應該可以正常工作,但事實並非如此。 它幾乎相當於 express 代碼段。 我在身份驗證函數中收到一個錯誤,告訴我我傳遞了錯誤的參數。

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",

問題是像中間件這樣的表達在 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
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM