春季安全 /钥匙巡带:使用多个领域确保相同的请求路径
我希望在两个不同领域(例如人类用户和S2S用户)中的用户访问相同的休息端点。我能找到的所有多租户示例(例如)建议实施 keycloakconfigresolver
根据请求路径选择一个领域。例如:
public class PathBasedKeycloakConfigResolver implements KeycloakConfigResolver {
private final KeycloakDeployment realm1Deployment;
private final KeycloakDeployment realm2Deployment;
public PathBasedKeycloakConfigResolver() throws IOException {
realm1Deployment = buildDeployment("realm1.json");
realm2Deployment = buildDeployment("realm2.json");
}
@Override
public KeycloakDeployment resolve(HttpFacade.Request request) {
String path = request.getRelativePath();
return path.startsWith("clients/") ? realm1Deployment : realm2Deployment;
}
private static KeycloakDeployment buildDeployment(String path) throws IOException {
return KeycloakDeploymentBuilder.build(new ClasspathResource(path).getInputStream());
}
}
但这要求我每个请求路径选择一个领域。
我想要不同的功能,我想尝试对多个领域的请求进行身份验证,并首先选择成功。我觉得这将是支持单个URI多个领域的逻辑方式,但我愿意为实现这一目标提供建议。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于KeyCloak提供了OAuth2功能,因此您不一定需要使用KeyCloak适配器(因此,其中许多都在不弃用,因此,甚至,请参见
如何使用多个发行人为弹簧安全配置JWT身份验证的一个示例,如您所在:
在您的情况下,单独的发行人URL将是您各自领域的发行人URL。此示例直接从,它还包含有关如何与XML配置,您是否希望使用它。
当然,远离适配器,如果您已经使用了它可能并不容易,但是由于适配器从长远来看,因此可能值得尽早评估这样做
Since Keycloak provides OAuth2 functionality, you do not necessarily need to use the keycloak adapters (a lot of them are being deprecated because of this, even, see here). Instead you can just rely on the built in functionality of Spring Security.
An example of how to configure JWT Authentication for Spring Security with multiple issuers looks like this:
The separate issuer URLs in your case would be the issuer URLs of your respective realms. This example is taken directly from the Spring Security documentation, it also contains samples on how to achieve the same with XML configuration, should you prefer to use that.
Of course, migrating away from the adapter, if you're already using it might not be easy, but since the adapter is going away in the long term anyways, it might be worth evaluating doing so as early as possible
KeyCloak适配器的折旧宣布在那里。
您应该看看。它可以与您需要的尽可能多的发行人进行操作,并解决了很多KeyCloak Spring-boot适配器的限制:
基本教程在这里。
The Keycloak adapters deprecation is announced there.
You should have a look at this OpenID adapter I wrote. It works out of the box with as many issuers as you need and solves quite a few of keycloak spring-boot adapter limitations:
Basic tutorial here.