0

我正在尝试从 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)
})

但是,我遇到了错误:

错误:400:错误请求

在服务器端,我看到日志:

2021-11-19T10:16:49,312+01:00 WARN [org.keycloak.events](默认任务 56)类型=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,但是,用户名不知何故没有正确给出。

该方法的签名是确定的,它得到了用户名,它是如何期望的。

我在这里缺少什么?

4

1 回答 1

1

错误not_allowed表示不允许直接授予。Direct Access Grants Enabledtest-clientKeycloak 客户端配置中启用。

于 2021-11-19T09:52:18.213 回答