Keycloak 21.1 REST API 端点

问题描述 投票:0回答:1

我一直在尝试在 keycloak 21.1 中实现这个自定义端点:

EndopintFactory.java 文件

package org.keycloak.extension.authenticator;

import org.keycloak.Config;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.KeycloakSessionFactory;
import org.keycloak.services.resource.RealmResourceProvider;
import org.keycloak.services.resource.RealmResourceProviderFactory;

public class HashRegistrationEndpointFactory implements RealmResourceProviderFactory {

    public static final String ID = "custom-registration-endpoint";

    @Override
    public RealmResourceProvider create(KeycloakSession session) {
        return new HashRegistrationEndpoint();
    }

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

    @Override
    public void postInit(KeycloakSessionFactory factory) {
    }

    @Override
    public void close() {
    }

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

端点java文件:

package org.keycloak.taxit.authenticator;

import lombok.RequiredArgsConstructor;
import org.jboss.logging.Logger;
import org.keycloak.models.KeycloakSession;
import org.keycloak.services.resource.RealmResourceProvider;

import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import java.util.HashMap;
import java.util.Map;

@RequiredArgsConstructor
public class HashRegistrationEndpoint implements RealmResourceProvider {

    private static final Logger logger = Logger.getLogger(HashRegistrationEndpoint.class);

    @Context
    private KeycloakSession session;

    @GET
    @Path("/hash-register")
    @Produces(MediaType.APPLICATION_JSON)
    public Response customRegister(
            @QueryParam("hash") String hash,
            @QueryParam("issuer") String issuer,
            @QueryParam("email") String email,
            @QueryParam("ruc") String ruc,
            @QueryParam("isValidId") String isValidId) {

        // Set parameters in the authentication session
        session.getContext().getAuthenticationSession().setAuthNote("hash", hash);
        session.getContext().getAuthenticationSession().setAuthNote("issuer", issuer);
        session.getContext().getAuthenticationSession().setAuthNote("email", email);
        session.getContext().getAuthenticationSession().setAuthNote("ruc", ruc);
        session.getContext().getAuthenticationSession().setAuthNote("isValidId", isValidId);

        // Create a map and put the query parameters into it
        Map<String, String> map = new HashMap<>();
        map.put("hash", hash);
        map.put("issuer", issuer);
        map.put("email", email);
        map.put("ruc", ruc);
        map.put("isValidId", isValidId);

        // Return the map as a JSON object
        return Response.ok().entity(map).build();
    }

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

    @Override
    public void close() {
    }
}

META-INF/org.keycloak.services.resource.RealmResourceProviderFactory

org.keycloak.taxit.authenticator.HashRegistrationEndpointFactory

编写代码后,我构建了 jar 文件并将其放入 keycloak 服务器文件夹的 /providers 文件夹中。

然后我运行命令:

kc.bat build
kc.bat start-dev

服务器运行正常,但是当我尝试访问端点时http://localhost:8080/auth/realms/realm_name/custom-registration-endpoint/hash-register?hash=a&issuer=b&email=c&ruc=d&isValidId=e在浏览器中的响应是 404。

URL 是否有问题,或者我在 keycloak 的编程/配置中遗漏了什么?

java rest keycloak quarkus
1个回答
0
投票

您是否尝试过不带

/auth
的 URL。自 Keycloak 17 起,
/auth
已从默认上下文路径中删除。

参考资料:

© www.soinside.com 2019 - 2024. All rights reserved.