【问题标题】:Port error when deploying express server with OIDC to Azure App Service将带有 OIDC 的快速服务器部署到 Azure 应用服务时出现端口错误
【发布时间】:2021-03-02 07:44:50
【问题描述】:

我正在尝试将服务器部署到 azure 应用服务。服务器代码如下。

我从日志流中得到的错误是:

2020-11-18T23:36:06.088Z 错误 - 容器 [容器名称] 未响应端口:8080 上的 HTTP ping,站点启动失败。查看容器日志进行调试。

我将 PORT 设置为 8080,并且我知道配置正在启动,因为我可以在日志中看到“服务器正在侦听端口 8080”。当我看到其他帖子时,我尝试将 WEBSITES_PORT 更改为 80 和 8080,但我认为我的问题有所不同。

在我使用 OIDC 库添加身份验证之前,此站点正在运行。

该应用使用下面的服务器代码在本地运行。

const https = require('https')

const express = require('express')
const path = require('path')
const app = express()
const fs = require('fs')

const key = fs.readFileSync('./key.pem')
const cert = fs.readFileSync('./cert.pem')

require('dotenv').config()
app.use(express.json())
app.use(express.urlencoded({
  extended: true
}))
app.use(express.static('express'))
var cors = require('cors')
const OktaJwtVerifier = require('@okta/jwt-verifier')
const session = require('express-session')
const {
  ExpressOIDC
} = require('@okta/oidc-middleware')

var getUserInfo = require('./getUserInfo')

// session support is required to use ExpressOIDC
app.use(
  session({
    secret: 'this should be secure',
    resave: true,
    saveUninitialized: false,
    cookie: {
      httpOnly: false,
      secure: true,
    },
  })
)

const oidc = new ExpressOIDC({
  issuer: process.env.ISSUER || 'https://[custom auth server domain].gov/oauth2/default',
  client_id: process.env.CLIENT_ID || 'xxxxxxxxxxxxxxxxx',
  client_secret: process.env.CLIENT_SECRET || 'xxxxxxxxxxxxxxxxxx',
  redirect_uri: process.env.REDIRECT_URI ||
    'https://localhost:3000/authorization-code/callback',
  appBaseUrl: process.env.APP_BASE_URL || 'https://localhost:3000',
  scope: 'openid profile',
})

// ExpressOIDC attaches handlers for the /login and /authorization-code/callback routes
app.use(oidc.router)

app.use(cors())
app.options('*', cors())
app.get('/userinfo', (req, res) => {
  let domain = 'dev'

  if (req.isAuthenticated()) {
    getUserInfo.userRequest(res, req.userContext, domain)
  }
})

app.get('/authStatus', (req, res) => {
  if (req.isAuthenticated()) {
    res.send(req.userContext.userinfo)
  }
})

app.post('/forces-logout', oidc.forceLogoutAndRevoke(), (req, res) => {
  // Nothing here will execute, after the redirects the user will end up wherever the `routes.logoutCallback.path` specifies (default `/`)
})

var linkObj = {not relevant links used hrefs on html based on env}

// default URL for website
app.get('/', function(req, res) {
  res.sendFile(path.join(__dirname + '/express/index.html'))
  //__dirname : It will resolve to your project folder.
})

// FAQ Path
app.get('/help', function(req, res) {
  res.sendFile(path.join(__dirname + '/express/help.html'))
  //__dirname : It will resolve to your project folder.
})

app.get('/links', (req, res) => {
  res.json(linkObj)
})

app.post('/forces-logout', oidc.forceLogoutAndRevoke(), (req, res) => {
  // Nothing here will execute, after the redirects the user will end up wherever the `routes.logoutCallback.path` specifies (default `/`)
})

// default URL for website
app.get('*', function(req, res) {
  res.sendFile(path.join(__dirname + '/express/index.html'))
  //__dirname : It will resolve to your project folder.
})

const port = normalizePort(process.env.PORT || '3000')
if (process.env.PORT) {
  const server = https.createServer(app)
  server.listen(port)
} else {
  const server = https.createServer({
    key: key,
    cert: cert
  }, app)
  server.listen(port)
}

console.debug('Server listening on port ' + port)

function normalizePort(val) {
  var port = parseInt(val, 10)

  if (isNaN(port)) {
    // named pipe
    return val
  }

  if (port >= 0) {
    // port number
    return port
  }

  return false
}

【问题讨论】:

    标签: express azure-web-app-service port openid-connect okta


    【解决方案1】:

    我相信这句话可能会给你带来问题:

    const port = normalizePort(process.env.PORT || '3000')
    

    我会尝试将其更改为:

    const port = normalizePort(process.env.PORT || '8080')
    

    您还需要更改这些行以获得您的公共 URL,而不是 localhost:

      redirect_uri: process.env.REDIRECT_URI ||
        'https://localhost:3000/authorization-code/callback',
      appBaseUrl: process.env.APP_BASE_URL || 'https://localhost:3000',
    

    更改这些后,您需要将 Okta 上的应用更新为生产重定向 URI。

    【讨论】:

    • 谢谢马特。 process.env 变量仅在部署到 Azure 时才起作用。 || 右边的变量仅适用于本地。我在 Okta 中为本地设置了一个 OIDC 配置,另一个用于我无法部署的开发环境。
    猜你喜欢
    • 2014-05-28
    • 2020-05-18
    • 2018-01-12
    • 2014-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多