简体   繁体   English

实施 SPI 以扩展 Keycloak API - 找不到资源

[英]Implementing a SPI to extend Keycloak API - Could not find resource

I'm trying to create a custom resource on my Keycloak server to extend its rest API.我正在尝试在我的 Keycloak 服务器上创建自定义资源以扩展其 rest API。 So i'm implementing a SPI.所以我正在实施一个SPI。 Starting with a hello world.从一个你好世界开始。

At the moment my goal is to obtain a "hello" + name through a GET on http://localhost:8080/auth/admin/realms/myRealm/hello目前我的目标是通过http://localhost:8080/auth/admin/realms/myRealm/hello上的 GET 获取“hello”+名称

I use Postman to requests the server.我使用 Postman 来请求服务器。 I'm able to get a user token on myRealm.我能够在 myRealm 上获得用户令牌。 I choose a user for which i have assigned the role View-users in the realm-managment Mapper.我选择了一个用户,我在realm-managment映射器中为其分配了角色View-users

So the builtin Keycloak Admin API works.所以内置的 Keycloak Admin API 可以工作。 eg: http://localhost:8080/auth/admin/realms/myRealm/users/count returns the expected user count.例如: http://localhost:8080/auth/admin/realms/myRealm/users/count返回预期的用户数。

But the problem is i get an "error": "RESTEASY003210: Could not find resource for full path: http://localhost:8080/auth/admin/realms/myRealm/hello/ " when requesting this endpoint.但问题是我在请求此端点时收到“错误”:“RESTEASY003210:找不到完整路径的资源: http://localhost:8080/auth/admin/realms/myRealm/hello/ ”。

Here's my setup (i read several guides):这是我的设置(我阅读了几个指南):

The module project's pom.xml include dependency for keycloak-core keycloak-server-spi keycloak-server-spi-private org.jboss.spec.javax.ws.rs模块项目的 pom.xml 包括对keycloak-core keycloak-server-spi keycloak-server-spi-private org.jboss.spec.javax.ws.rs的依赖项

RealmResourceProvider implementation: RealmResourceProvider 实现:

public class HelloWorldProvider implements RealmResourceProvider {

    private KeycloakSession session;

    public HelloWorldProvider(KeycloakSession session) {
        this.session = session;
    }

    @Override
    public Object getResource() {
        return this;
    }

    @GET
    @Path("/hello")
    @Produces("text/plain; charset=utf-8")
    public String get() {
        String name = session.getContext().getRealm().getDisplayName();
        if (name == null) {
            name = session.getContext().getRealm().getName();
        }
        return "Hello" + name;
    }

    @Override
    public void close() {
    }
}

Factory implementation:工厂实施:

public class HelloWorldProviderFactory implements RealmResourceProviderFactory {

    public static final String ID = "hello";

    @Override
    public String getId() {
        return ID;
    }

    @Override
    public int order() {
        return 0;
    }

    @Override
    public RealmResourceProvider create(KeycloakSession keycloakSession) {
        return new HelloWorldProvider(keycloakSession);
    }

    @Override
    public void init(Config.Scope scope) {
    }

    @Override
    public void postInit(KeycloakSessionFactory keycloakSessionFactory) {
    }

    @Override
    public void close() {
    }
}

I also created the file src\main\resources\META-INF\org.keycloak.services.resource.RealmResourceProviderFactory it contains a reference to my HelloWorldProviderFactory我还创建了文件src\main\resources\META-INF\org.keycloak.services.resource.RealmResourceProviderFactory它包含对我的HelloWorldProviderFactory的引用

After packaging the jar, i put a copy of it in keycloak-9.0.3\standalone\deployments and after running standalone.bat the file keycloak-spi-rest-hello-1.0.jar.deployed is created.打包 jar 后,我将它的副本放在keycloak-9.0.3\standalone\deployments中,并在运行standalone.bat后创建文件keycloak-spi-rest-hello-1.0.jar.deployed

Try this:尝试这个:

http://localhost:8080/auth/admin/realms/myRealm/hello/hello http://localhost:8080/auth/admin/realms/myRealm/hello/hello

because first 'hello' is the name of your factory ID, instead the second 'hello' is the path you set on the service.因为第一个“hello”是您的工厂 ID 的名称,而第二个“hello”是您在服务上设置的路径。

alexb83's answer does not work for me, I had to omit the /admin part from the url alexb83 的回答对我不起作用,我不得不从 url 中省略/admin部分

http://localhost:8080/auth/realms/myRealm/hello/hello

(sorry, I don't have not enough reputation to comment under said answer) (对不起,我没有足够的声誉来评论所说的答案)

So as alexb83 said, i missed the second /hello in the url.正如 alexb83 所说,我错过了 url 中的第二个 /hello。

As PotatoesMaster said, i had to omit /admin in the url.正如 PotatoesMaster 所说,我不得不在 url 中省略 /admin。

In Addition the file org.keycloak.services.resource.RealmResourceProviderFactory was in aa wrong folder: i missed \services .此外,文件org.keycloak.services.resource.RealmResourceProviderFactory位于错误的文件夹中:我错过了\services

The working path is:工作路径是:

src\main\resources\META-INF\services\org.keycloak.services.resource.RealmResourceProviderFactory

In my case I had mixed up the order of the sub-paths in the url when accessing the end-point, ie instead of在我的情况下,我在访问端点时混淆了 url 中子路径的顺序,即

/user/customuser /用户/自定义用户

I had given我给了

/customuser/user /customuser/用户

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM