Reputation: 727
Implementing a custom rest endpoint in keycloak I used these example:
After embedding the provider to keycloak it's loaded during keycloak startup. Guess that's fine. In server info I can see the the endpoint as well.
Problem:
How may I call that endpoint?
Do I need to registrate the endpoint or mount it on a client?
(If so which settings does the client need (admin rights etc...)
What is the URL for calling the endpoint?
Upvotes: 10
Views: 7412
Reputation: 533
Extending from Patrick Hesse's answer
For the latest versions of Keycloak (24.0.2 as of July 2024)
prefix auth has been dropped, it now looks like this
Format
{{keycloakUrl}}/realms/{{reamlName}}/{{id in providerFactory}}/...
Example
http://localhost/realms/master/otp-validator/validate
Sample Realm Resource Provider Factory
public class GoodRealmResourceProviderFactory implements RealmResourceProviderFactory, RealmResourceProvider {
public static final String ID = "otp-validator";
private KeycloakSession session;
public RealmResourceProvider create(KeycloakSession session) {
this.session = session;
return this;
}
public void init(Config.Scope config) {
// NOOP
}
public void postInit(KeycloakSessionFactory factory) {
// NOOP
}
public void close() {
// NOOP
}
public String getId() {
return ID;
}
public Object getResource() {
return new GoodRealmResourceProvider(session);
}
}
Resource Provider
public class GoodRealmResourceProvider implements RealmResourceProvider {
private final KeycloakSession session;
public GoodRealmResourceProvider(KeycloakSession session) {
this.session = session;
}
@Override
public Object getResource() {
return this;
}
@GET
@Path("/validate")
@Produces(MediaType.APPLICATION_JSON)
public String getCustomMessage() {
System.out.println("something amaziiing {}: "+session.getContext().getUri());
return "{\"message\": \"Hello from custom endpoint!\"}";
}
@Override
public void close() {
// Cleanup resources if necessary
}
Upvotes: 0
Reputation: 174
Your don't need register or mount your endpoint. the effective url is calculated with the given id in your ProviderFactory.
{{keycloakUrl}}/auth/realms/{{reamlName}}/{{id in providerFactory}}/...
For your example the url is
{{keycloakUrl}}/auth/realms/{{realmName}}/example/companies
Upvotes: 14