从 NodeJS 登录到 Keycloak:400 错误请求
Logging into Keycloak from NodeJS: 400 Bad Request
我正在尝试从 NodeJS 代码登录到 Keycloak,但我正在努力寻找工作示例。
https://www.keycloak.org/docs/latest/securing_apps/index.html#_nodejs_adapter 上的文档不完整,没有描述最重要的事情,即您如何实际登录。
我根据 keycloak 文档的基本信息和 keycloak-nodejs-connect:
的测试修改了我的示例
"keycloak-connect": "15.0.2",
"express-session": "1.17.2",
const Keycloak = require('keycloak-connect');
const session = require('express-session');
const keycloakConfig = {
serverUrl: "http://keycloak.intern/auth",
realm: "client-realm",
clientId: "test-client",
bearerOnly: true
}
const memoryStore = new session.MemoryStore();
const keycloak = new Keycloak({store: memoryStore}, keycloakConfig)
async function loginUser(username, password) {
return await keycloak.grantManager.obtainDirectly(username, password).then(grant => {
return grant
})
}
const main = async () => {
let grant = await loginUser('testuser@localhost.com', "test_password")
}
main().then(()=>{
process.exit(0)
}, (err)=>{
console.error(err)
process.exit(1)
})
但是,我遇到了错误:
Error: 400:Bad Request
在服务器端,我看到日志:
2021-11-19T10:16:49,312+01:00 WARN [org.keycloak.events] (default task-56) type=LOGIN_ERROR, realmId=client-realm, clientId=test-client, userId=null, ipAddress=192.168.111.2222, error=not_allowed, auth_method=oauth_credentials, grant_type=password, client_auth_method=client-secret
因此调用了 keycloak API,但是,用户名以某种方式不正确。
方法的签名没问题,它获取了用户名,这是它所期望的。
我在这里缺少什么?
错误not_allowed
表示不允许直接授权。在 test-client
Keycloak 客户端配置中启用 Direct Access Grants Enabled
。
我正在尝试从 NodeJS 代码登录到 Keycloak,但我正在努力寻找工作示例。
https://www.keycloak.org/docs/latest/securing_apps/index.html#_nodejs_adapter 上的文档不完整,没有描述最重要的事情,即您如何实际登录。
我根据 keycloak 文档的基本信息和 keycloak-nodejs-connect:
的测试修改了我的示例"keycloak-connect": "15.0.2",
"express-session": "1.17.2",
const Keycloak = require('keycloak-connect');
const session = require('express-session');
const keycloakConfig = {
serverUrl: "http://keycloak.intern/auth",
realm: "client-realm",
clientId: "test-client",
bearerOnly: true
}
const memoryStore = new session.MemoryStore();
const keycloak = new Keycloak({store: memoryStore}, keycloakConfig)
async function loginUser(username, password) {
return await keycloak.grantManager.obtainDirectly(username, password).then(grant => {
return grant
})
}
const main = async () => {
let grant = await loginUser('testuser@localhost.com', "test_password")
}
main().then(()=>{
process.exit(0)
}, (err)=>{
console.error(err)
process.exit(1)
})
但是,我遇到了错误:
Error: 400:Bad Request
在服务器端,我看到日志:
2021-11-19T10:16:49,312+01:00 WARN [org.keycloak.events] (default task-56) type=LOGIN_ERROR, realmId=client-realm, clientId=test-client, userId=null, ipAddress=192.168.111.2222, error=not_allowed, auth_method=oauth_credentials, grant_type=password, client_auth_method=client-secret
因此调用了 keycloak API,但是,用户名以某种方式不正确。
方法的签名没问题,它获取了用户名,这是它所期望的。
我在这里缺少什么?
错误not_allowed
表示不允许直接授权。在 test-client
Keycloak 客户端配置中启用 Direct Access Grants Enabled
。