当前位置: 首页 > 知识库问答 >
问题:

Keycloak spring security客户端凭据授予

齐高阳
2023-03-14

我可以使用KeycloakRestTemplate,其中一个keycloak客户机与另一个keycloak客户机通信。然而,只有当我登录到第一个keycloak客户机时,它才起作用,即它向keycloak服务器发送客户机ID、客户机机密、用户名和密码。如果我没有在第一个客户机上使用用户和密码进行身份验证,我会得到“无法设置授权头,因为没有身份验证原则”。但是我已经将keycloak配置为对第一个客户机使用服务帐户(客户机凭据授权),因此我不应该使用用户/密码,而应该只依赖于客户机ID/Secret。这是OAuth2规范的错误/偏差吗?

共有1个答案

濮阳旺
2023-03-14

KeyCloakRestTemplate向Keycloak服务器发送客户端ID、客户端机密、用户名和密码。我只想发送客户ID和机密。为此,我创建了OAuth2RestTemplateKeycloakClientCredentialsRestTemplate子类。它使用Spring Boot中的OAuth2支持来授予客户机凭据。它还从application.properties获取Keycloak属性。

import org.springframework.security.oauth2.client.OAuth2ClientContext;
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails;

public class KeycloakClientCredentialsRestTemplate extends OAuth2RestTemplate {

    public KeycloakClientCredentialsRestTemplate(OAuth2ProtectedResourceDetails resource,
            OAuth2ClientContext context) {
        super(resource, context);
    }

}

还有:

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.security.oauth2.client.DefaultOAuth2ClientContext;
import org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails;
import org.springframework.security.oauth2.common.AuthenticationScheme;
import org.springframework.stereotype.Service;

@Service
public class KeycloakClientCredentialsConfig {

    @Value("${keycloak.realm}")
    private String realm;

    @Value("${keycloak.auth-server-url}")
    private String authServerUrl;

    @Value("${keycloak.resource}")
    private String clientId;

    @Value("${keycloak.credentials.secret}")
    private String clientSecret;

    @Bean
    public KeycloakClientCredentialsRestTemplate createRestTemplate() {
        return new KeycloakClientCredentialsRestTemplate(getClientCredentialsResourceDetails(),
                new DefaultOAuth2ClientContext());
    }

    private ClientCredentialsResourceDetails getClientCredentialsResourceDetails() {
        String accessTokenUri = String.format("%s/realms/%s/protocol/openid-connect/token",
            authServerUrl, realm);
        List<String> scopes = new ArrayList<String>(0); // TODO introduce scopes

        ClientCredentialsResourceDetails clientCredentialsResourceDetails = 
                new ClientCredentialsResourceDetails();

        clientCredentialsResourceDetails.setAccessTokenUri(accessTokenUri);
        clientCredentialsResourceDetails.setAuthenticationScheme(AuthenticationScheme.header);
        clientCredentialsResourceDetails.setClientId(clientId);
        clientCredentialsResourceDetails.setClientSecret(clientSecret);
        clientCredentialsResourceDetails.setScope(scopes);

        return clientCredentialsResourceDetails;
    }

}
 类似资料:
  • 当授权范围限于客户端控制下的受保护资源或事先与授权服务器商定的受保护资源时客户端凭据可以被用作为一种授权许可。典型的当客户端代表自己表演(客户端也是资源所有者)或者基于与授权服务器事先商定的授权请求对受保护资源的访问权限时,客户端凭据被用作为授权许可。

  • 直截了当的问题,希望有一个直截了当的答案。我试图使用request通过Node.js实现客户端凭证流。这是我的代码 无论我做什么,响应体总是 我尝试过使用 curl 提出请求,结果相同。 这意味着这是凭据的问题。我肯定为我的应用程序使用了正确的客户端ID和客户端秘密,这让我相信是编码导致了问题。 我的编码正确吗?如果是这样,还有什么原因呢?

  • 有人用这种方法吗?https://laravel.com/docs/5.4/passport#client-凭证授予代币 我试图使注册API只包含client_id和client_secret,我希望返回作为访问令牌、刷新令牌、过期日期,但返回www.url。com/oauth/token这是什么 有人能帮我吗?提前谢谢

  • 当客户端是资源所有者时,或者当授权范围限于受客户端控制的受保护资源时,客户端凭证可以用作授权授权。 客户端仅在客户端凭据的帮助下请求访问令牌。 客户端凭证授权流用于获取访问令牌以授权API请求。 使用客户端凭据授权,获取的访问令牌仅授予客户端应用程序搜索和获取目录文档的权限。 下图描绘了客户端凭据流。 上图所示的流程包括以下步骤 - Step 1 - 客户端使用授权服务器进行身份验证,并从令牌端点

  • 我开发Spring Cloud(使用Netflix OSS堆栈)微服务架构已经有一段时间了。正如您所料,我已经将授权服务器分离为一个独立的微服务。我的前端应用程序使用“密码”授权类型用于用户登录目的。但是,对于从前端服务到其他后端服务的rest调用,我使用的是“Client-Credentials”授权类型。客户机凭据授予类型也在其他后端服务中使用。通过这样做,我无法确定谁是请求的实际调用者(当前

  • 顺便说一句,这些微服务只会通过中间件层互相交谈,我的意思是不需要用户凭据来允许授权(用户登录过程如Facebook)。 我在Internet上寻找了一些示例,展示了如何创建一个授权和资源服务器来管理这种通信。然而,我只是找到了一些例子,解释了如何使用用户凭据(三条腿)来实现它。 有没有人有在Spring Boot和Oauth2中如何做的样例?如果有可能提供更多关于所用范围的详细信息,令牌交换将不胜