我如何 return 在 Keycloak (3.4.3) 的自定义 spnego Authenticator 中将自定义错误消息发送到前端?
How do I return custom error messages to frontend in custom spnego Authenticator in Keycloak (3.4.3)?
我正在 Keycloak 中构建一个自定义身份验证器,它不仅可以让用户登录到 keycloaks SSO 实例,还可以通过 rest-Call 登录到遗留 (SAP-) 系统。
现在我从这个旧版 API 收到错误消息,我想在通过我的自定义身份验证器登录时向最终用户显示消息(由 callee-param 国际化),但我没有了解如何执行此操作。
我认为这与例如spnego authenticator on github 中的以下行:
...
else {
context.getEvent().error(Errors.INVALID_USER_CREDENTIALS);
context.failure(AuthenticationFlowError.INVALID_CREDENTIALS);
}
...
当将 .error() 值更改为 "test!" 时,这会被记录下来,但向用户显示的错误也是无效的凭据行。
因此,AuthenticationFlowError 似乎是一个 ENUM,我认为它不适用于来自第三方的动态国际化消息。但是有没有办法告诉我的 authenticationcontext return 来自我的第三方系统的错误消息?
非常感谢任何帮助
所以,查看 keycloak-sources(开源 ftw!)我刚刚找到了如何做到这一点:
可以这样代替上面的行:
else {
//errorresponse from restcall to thirdparty-api (just removing prefix here)
responseString = responseString.replace("sap_error_", "");
//actually I try to do a somewhat silent idplogin, so this might fit.
//you can change this error to the errormsg from thirdparty, too,
// so its collected in the logs error=-part.
context.getEvent().error(Errors.IDENTITY_PROVIDER_ERROR);
//here we're building the actual responseHandler.
//One might even want to set the status to status from thirdparty.
Response challengeResponse = context.form().setError(responseString)
.createErrorPage(Response.Status.INTERNAL_SERVER_ERROR);
//And in the end we apply the failureChallenge to the Context
context.failureChallenge(AuthenticationFlowError.IDENTITY_PROVIDER_ERROR,
challengeResponse);
}
我对代码进行了注释以明确所做的事情。
edit:由于这个问题刚刚获得赞成票:请保重! .setError()-content 必须小于 255 个字符,否则会出现异常(DB-Field 太短 iirc,测试到 keycloak 7)。
此致,
多米尼克
我正在 Keycloak 中构建一个自定义身份验证器,它不仅可以让用户登录到 keycloaks SSO 实例,还可以通过 rest-Call 登录到遗留 (SAP-) 系统。
现在我从这个旧版 API 收到错误消息,我想在通过我的自定义身份验证器登录时向最终用户显示消息(由 callee-param 国际化),但我没有了解如何执行此操作。
我认为这与例如spnego authenticator on github 中的以下行:
...
else {
context.getEvent().error(Errors.INVALID_USER_CREDENTIALS);
context.failure(AuthenticationFlowError.INVALID_CREDENTIALS);
}
...
当将 .error() 值更改为 "test!" 时,这会被记录下来,但向用户显示的错误也是无效的凭据行。 因此,AuthenticationFlowError 似乎是一个 ENUM,我认为它不适用于来自第三方的动态国际化消息。但是有没有办法告诉我的 authenticationcontext return 来自我的第三方系统的错误消息?
非常感谢任何帮助
所以,查看 keycloak-sources(开源 ftw!)我刚刚找到了如何做到这一点:
可以这样代替上面的行:
else {
//errorresponse from restcall to thirdparty-api (just removing prefix here)
responseString = responseString.replace("sap_error_", "");
//actually I try to do a somewhat silent idplogin, so this might fit.
//you can change this error to the errormsg from thirdparty, too,
// so its collected in the logs error=-part.
context.getEvent().error(Errors.IDENTITY_PROVIDER_ERROR);
//here we're building the actual responseHandler.
//One might even want to set the status to status from thirdparty.
Response challengeResponse = context.form().setError(responseString)
.createErrorPage(Response.Status.INTERNAL_SERVER_ERROR);
//And in the end we apply the failureChallenge to the Context
context.failureChallenge(AuthenticationFlowError.IDENTITY_PROVIDER_ERROR,
challengeResponse);
}
我对代码进行了注释以明确所做的事情。
edit:由于这个问题刚刚获得赞成票:请保重! .setError()-content 必须小于 255 个字符,否则会出现异常(DB-Field 太短 iirc,测试到 keycloak 7)。
此致, 多米尼克