java - 使用Java Servlet Filter Adapter时如何使用KeyCloak获取用户数据

标签 java filter saml-2.0 service-provider keycloak

我正在尝试使用 KeyCloak 开发服务提供商并针对 OpenAM IDP 进行身份验证。

我按照以下说明操作: http://keycloak.github.io/docs/userguide/saml-client-adapter/html/ch07.html

我现在的问题是,经过身份验证后,我无法获取用户数据(例如电子邮件、用户名、角色...)

userPrinciple 似乎是 SamlPrinciple 类型,而不是 KeycloakPrinciple。

属性和友好属性均为空。

import org.keycloak.adapters.saml.SamlPrincipal;
import org.keycloak.KeycloakPrincipal;

@Path("/user")
public class User {

    @GET
    @Path("/")
    @Produces({ MediaType.APPLICATION_JSON })
    @Consumes({ MediaType.APPLICATION_JSON })
    public Object info(@Context SecurityContext context) {

        SamlPrincipal userPrincipal = (SamlPrincipal) context
                .getUserPrincipal();

        Set<String> attributeNames = userPrincipal.getAttributeNames();
        Set<String> friendlyNames = userPrincipal.getFriendlyNames();
        ...
    }

attributeNames 和FriendlyNames 都是空集(大小=0)。

当我查看 KeyCloak SP 和 OpenAP IDP 之间的通信时,我看到一个发送用户数据的请求-响应:

OpenAM --> KeyCloak user information

如何/在哪里可以从 SamlPrinciple 对象获取此用户信息?

最佳答案

在客户端设置下的 keycloak 中,转到映射器选项卡。创建新属性,名称为用户名,映射器类型为用户属性,就像电子邮件等一样。之后您就可以在java过滤器中获取用户详细信息。 java 中的示例

SamlPrincipal samlPrincipal = (SamlPrincipal) principal;
userName = samlPrincipal.getFriendlyAttribute("username");
Principal principal = httpReq.getUserPrincipal();

关于java - 使用Java Servlet Filter Adapter时如何使用KeyCloak获取用户数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37563062/

相关文章:

java - springboot应用程序编译错误

java - Jackson 中的(反)序列化嵌套泛型

node.js - Passport-SAML:读取用户信息

android - 选择一个带有 intent get_content 的文件

single-sign-on - 单点登录 SAML 拦截

ruby-on-rails - Saml_IDP gem 不提取元数据或accepted_hosts

java - Spring @Value 不加载属性

java - 1L serialVersionUID 是什么意思?我什么时候可以使用这个默认值 1L?

binding - 在各种过滤选项之间切换的正确 emberjs 方式是什么?

sorting - 在 Google 表格中的过滤器 View 中排序时,ArrayFormula 列消失