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
}