如何在 Spring Cloud Gateway 中获取 Keycloak 领域和用户属性
How to get Keycloak realm and user attributes in Spring Cloud Gateway
在 Spring 使用 MVC 启动时,可以通过在控制器方法中注入 Principal
来获取有关 Keycloak 用户领域和定义属性的信息,该方法的类型为 KeycloakAuthenticationToken
,它提供此信息。
但在 Spring 具有依赖项的云网关中
implementation 'org.springframework.cloud:spring-cloud-security'
implementation 'org.springframework.boot:spring-boot-starter-oauth2-client'
和通过
定义的安全性
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http,
ReactiveClientRegistrationRepository clientRegistrationRepository) {
// Authenticate through configured OpenID Provider
http.oauth2Login();
...
}
安全有效,我设法通过过滤器检索主体
.filter((exchange, chain) -> {
return exchange.getPrincipal().flatMap(p -> {
System.out.println("Pricnipal class: " + p.getClass());
OAuth2AuthenticationToken token = (OAuth2AuthenticationToken) p;
System.out.println("Token: " + token.getAuthorizedClientRegistrationId() + "|"
+ token.getPrincipal().getAttributes() + "|" + token.getDetails() + "|"+ token.getPrincipal().getClass());
System.out.println("Exchange Attr: " + exchange.getAttributes());
OidcUser oicdUser = (OidcUser)token.getPrincipal();
System.out.println("OicdUser: "+oicdUser.getClaims()+ "|" + oicdUser.getIdToken().getClaims()+"|"+oicdUser.getAttributes());
Authentication aut = (Authentication) p;
ServerHttpRequest request = exchange.getRequest().mutate().header("username1", p.getName())
.header("roles1", aut.getAuthorities().toString()).build();
return chain.filter(exchange.mutate().request(request).build());
});
})
但是它的类型是OAuth2AuthenticationToken
。其中有关于经过身份验证的用户的基本信息,但没有通过 Keycloak 管理控制台定义的 Keycloak 领域或用户属性数据。
实际上,它有效。发布的代码没有任何问题。我没有正确设置 Keycloak Mappers,这是向令牌添加属性所必需的。
配置后,上述方法足以从令牌中检索用户属性。
他们将在 token.getPrincipal().getAttributes()
属性 中 OAuth2AuthenticationToken token
。
在 Spring 使用 MVC 启动时,可以通过在控制器方法中注入 Principal
来获取有关 Keycloak 用户领域和定义属性的信息,该方法的类型为 KeycloakAuthenticationToken
,它提供此信息。
但在 Spring 具有依赖项的云网关中
implementation 'org.springframework.cloud:spring-cloud-security'
implementation 'org.springframework.boot:spring-boot-starter-oauth2-client'
和通过
定义的安全性public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http,
ReactiveClientRegistrationRepository clientRegistrationRepository) {
// Authenticate through configured OpenID Provider
http.oauth2Login();
...
}
安全有效,我设法通过过滤器检索主体
.filter((exchange, chain) -> {
return exchange.getPrincipal().flatMap(p -> {
System.out.println("Pricnipal class: " + p.getClass());
OAuth2AuthenticationToken token = (OAuth2AuthenticationToken) p;
System.out.println("Token: " + token.getAuthorizedClientRegistrationId() + "|"
+ token.getPrincipal().getAttributes() + "|" + token.getDetails() + "|"+ token.getPrincipal().getClass());
System.out.println("Exchange Attr: " + exchange.getAttributes());
OidcUser oicdUser = (OidcUser)token.getPrincipal();
System.out.println("OicdUser: "+oicdUser.getClaims()+ "|" + oicdUser.getIdToken().getClaims()+"|"+oicdUser.getAttributes());
Authentication aut = (Authentication) p;
ServerHttpRequest request = exchange.getRequest().mutate().header("username1", p.getName())
.header("roles1", aut.getAuthorities().toString()).build();
return chain.filter(exchange.mutate().request(request).build());
});
})
但是它的类型是OAuth2AuthenticationToken
。其中有关于经过身份验证的用户的基本信息,但没有通过 Keycloak 管理控制台定义的 Keycloak 领域或用户属性数据。
实际上,它有效。发布的代码没有任何问题。我没有正确设置 Keycloak Mappers,这是向令牌添加属性所必需的。
配置后,上述方法足以从令牌中检索用户属性。
他们将在 token.getPrincipal().getAttributes()
属性 中 OAuth2AuthenticationToken token
。