我已经为 Express JS 创建了一个带有 Keycloak 中间件的 node.js 应用程序,众所周知,它“在我的计算机上工作”。
const Keycloak = require('keycloak-connect')
const express = require('express')
const session = require('express-session')
const app = express()
const LOGIN_PATH = '/login'
const LOGOUT_PATH = '/logout'
const SESSION_STORE_PASS = '123456789012345678901234567890!!!'
const server = app.listen(3000, function () {
const host = server.address().address
const port = server.address().port
console.log('Example app listening at http://%s:%s', host, port)
})
app.get('/', function (req, res) {
res.redirect(LOGIN_PATH)
})
// Create a session-store to be used by both the express-session
// middleware and the keycloak middleware.
const memoryStore = new session.MemoryStore()
app.use(session({
secret: SESSION_STORE_PASS,
resave: false,
saveUninitialized: true,
store: memoryStore
}))
const kcOptions = {
"realm": "nodejs-example",
"realm-public-key": "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCrVrCuTtArbgaZzL1hvh0xtL5mc7o0NqPVnYXkLvgcwiC3BjLGw1tGEGoJaXDuSaRllobm53JBhjx33UNv+5z/UMG4kytBWxheNVKnL6GgqlNabMaFfPLPCF8kAgKnsi79NMo+n6KnSY8YeUmec/p2vjO2NjsSAVcWEQMVhJ31LwIDAQAB",
"auth-server-url": "http://localhost:8080/auth",
"ssl-required": "external",
"resource": "nodejs-connect",
"public-client": true
}
console.log("Starting Keycloak connector with options: ", kcOptions)
const keycloak = new Keycloak({store: memoryStore}, kcOptions)
app.use(keycloak.middleware({
logout: LOGOUT_PATH, // <-- this is supposed to delete the session and log you out from KC as well
admin: '/'
}))
app.get('/login', keycloak.protect(), async function (req, res) {
let token
try {
const grant = await keycloak.getGrant(req, res)
token = grant.access_token
console.log(`Found token ${token}`)
} catch (e) {
console.log("Unable to find the token in KC response ", req.session)
throw new Error(e)
}
const userProfile = await keycloak.grantManager.userInfo(token)
console.log("Found user profile:", userProfile)
res.header("Content-Type", 'application/json')
return res.send(JSON.stringify(userProfile, null, 4))
})
此应用程序侦听端口 3000,但问题是浏览器将通过反向代理(端口 80)访问它。而且我无法让中间件向 Keycloak 发送正确的“redirect_uri”查询参数。它总是将具有相同端口 3000 的 URL 放置在本地服务器正在侦听的位置。
我在文档中redirection-url
找到了设置,但在 Github 存储库中没有找到该字符串的证据。