Keycloak为现有领域创建用户?

时间:2020-11-06 17:06:23

标签: java keycloak http-status-code-401 keycloak-services

我正在尝试为现有领域和客户端创建用户。下面是我尝试过的。 URL,领域,用户名,密码,clientId和clientSecret是正确的。但是它在行NotAuthorization上引发了var response = keycloak.realm( realm ).users().create( user );异常,我也尝试使用授权令牌来获取它,但是它抛出了相同的错误。我在做什么错了?

        Keycloak keycloak = Keycloak.getInstance( serverUrl, realm, username, password, clientId, clientSecret );

        var user = new UserRepresentation();
        user.setEnabled( true );
        user.setUsername( "test" );
        user.setAttributes(Collections.singletonMap("origin", Collections.singletonList( "demo" ) ));

        CredentialRepresentation credentialRepresentation = new CredentialRepresentation();
        credentialRepresentation.setTemporary( false );
        credentialRepresentation.setType( CredentialRepresentation.PASSWORD );
        credentialRepresentation.setValue( "1234" );
        user.setCredentials( Collections.singletonList( credentialRepresentation ) );

        try {
            var response = keycloak.realm( realm ).users().create( user );
            System.out.println( "Response: " + response.getStatus() );
            System.out.println( "Response: " + response.getMetadata() );

        } catch ( Exception e ) {
            e.printStackTrace();
        }

错误堆栈跟踪:

javax.ws.rs.ProcessingException: javax.ws.rs.NotAuthorizedException: HTTP 401 Unauthorized
    at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.filterRequest(ClientInvocation.java:603)
    at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.invoke(ClientInvocation.java:440)
    at org.jboss.resteasy.client.jaxrs.internal.proxy.ClientInvoker.invokeSync(ClientInvoker.java:149)
    at org.jboss.resteasy.client.jaxrs.internal.proxy.ClientInvoker.invoke(ClientInvoker.java:112)
    at org.jboss.resteasy.client.jaxrs.internal.proxy.ClientProxy.invoke(ClientProxy.java:76)
    at com.sun.proxy.$Proxy26.create(Unknown Source)
    at io.heist.keycloak.user.generator.KeycloakGen.main(KeycloakGen.java:27)
Caused by: javax.ws.rs.NotAuthorizedException: HTTP 401 Unauthorized
    at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.handleErrorStatus(ClientInvocation.java:221)
    at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.extractResult(ClientInvocation.java:195)
    at org.jboss.resteasy.client.jaxrs.internal.proxy.extractors.BodyEntityExtractor.extractEntity(BodyEntityExtractor.java:62)
    at org.jboss.resteasy.client.jaxrs.internal.proxy.ClientInvoker.invokeSync(ClientInvoker.java:151)
    at org.jboss.resteasy.client.jaxrs.internal.proxy.ClientInvoker.invoke(ClientInvoker.java:112)
    at org.jboss.resteasy.client.jaxrs.internal.proxy.ClientProxy.invoke(ClientProxy.java:76)
    at com.sun.proxy.$Proxy16.grantToken(Unknown Source)
    at org.keycloak.admin.client.token.TokenManager.grantToken(TokenManager.java:90)
    at org.keycloak.admin.client.token.TokenManager.getAccessToken(TokenManager.java:70)
    at org.keycloak.admin.client.token.TokenManager.getAccessTokenString(TokenManager.java:65)
    at org.keycloak.admin.client.resource.BearerAuthFilter.filter(BearerAuthFilter.java:52)
    at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.filterRequest(ClientInvocation.java:590)
    ... 6 more

Process finished with exit code 0

相关依赖项:

        <dependency>
            <groupId>org.keycloak</groupId>
            <artifactId>keycloak-core</artifactId>
            <version>10.0.2</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.keycloak</groupId>
            <artifactId>keycloak-admin-client</artifactId>
            <version>11.0.1</version>
        </dependency>

1 个答案:

答案 0 :(得分:0)

显然,它不接受我的登录用户admin作为已认证用户。因此,我添加了一个新用户,并赋予其正确的管理员角色,如下所示:enter image description here

enter image description here

完成这些操作后,我便可以添加用户。