解决 Keycloak 报错 KC-SERVICES0013 认证失败异常指南
分析 Keycloak 在单点登录或客户端凭证流程中报 KC-SERVICES0013 错误的常见根因,提供用户密码、客户端密钥与域配置修复方案。
问题现象
客户端尝试通过 Keycloak 进行用户登录或获取 Token 时被拒绝,服务日志抛出错误:
WARN [org.keycloak.events] (default task-12) type=LOGIN_ERROR, realmId=master, clientId=my-app, userId=null, ipAddress=192.168.1.10, error=invalid_user_credentials, auth_method=openid-connect
WARN [org.keycloak.services] (default task-12) KC-SERVICES0013: Failed authentication: Invalid credentials
触发根因
- 用户提交的密码与哈希记录不匹配,或账号处于 Temporary Password(强制首登改密)状态。
- 使用 Client Credentials 模式时,请求中携带的
client_secret过期或使用了不正确的 Client Authenticator 类型。 - 外部 LDAP/Active Directory 用户同步连接断开,无法验证远程目录凭证。
解决步骤与操作命令
步骤一:通过 Admin CLI 重置管理员或用户密码
如果管理员账号无法登录,可进入容器直接重置密码:
docker exec -it keycloak /opt/keycloak/bin/kcadm.sh config credentials --server http://localhost:8080 --realm master --user admin --password <current-admin-pass>
# 为普通用户重置永久密码
/opt/keycloak/bin/kcadm.sh set-password -r my-realm --username target-user --new-password NewPassword123! --temporary=false
步骤二:核实客户端 Client Secret 与配置
进入 Keycloak 控制台:
- 导航到 Clients -> 选择对应的
clientId。 - 检查 Client authentication 是否开启(若为私有客户端需开启)。
- 进入 Credentials 标签页,复制最新的 Client Secret,更新应用端配置。
步骤三:检查 LDAP 用户提供程序连通性
若接入了 LDAP,在 User Federation -> LDAP 页面点击 Test Connection 与 Test Authentication,确保服务账号未被锁定。
验证确认
使用 curl 模拟 token 获取接口:
curl -X POST "http://keycloak.example.com/realms/my-realm/protocol/openid-connect/token" -H "Content-Type: application/x-www-form-urlencoded" -d "grant_type=password" -d "client_id=my-app" -d "client_secret=your-secret" -d "username=target-user" -d "password=NewPassword123!"
接口返回 access_token 即表示凭证校验恢复正常。


