[keycloak-user] Cannot invoke custom endpoint added to keycloak
Sachindra Dilhara
sachindragh at gmail.com
Thu Mar 21 02:53:14 EDT 2019
Hi all,
I want to add a custom endpoint to keycloak and I tried a sample given.
Following are the steps I took.
1. *pom.xml*
*<project xmlns="http://maven.apache.org/POM/4.0.0
<http://maven.apache.org/POM/4.0.0>"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance
<http://www.w3.org/2001/XMLSchema-instance>"*
* xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
<http://maven.apache.org/POM/4.0.0>
http://maven.apache.org/maven-v4_0_0.xsd
<http://maven.apache.org/maven-v4_0_0.xsd>">*
* <name>Keycloak Custom REST Endpoint Example</name>*
* <description/>*
* <modelVersion>4.0.0</modelVersion>*
* <artifactId>keycloak-custom-rest-endpoint</artifactId>*
* <packaging>jar</packaging>*
* <groupId>keycloak.example</groupId>*
* <version>1.0.0</version>*
* <properties>*
* <maven.compiler.source>11</maven.compiler.source>*
* <maven.compiler.target>11</maven.compiler.target>*
* </properties>*
* <dependencies>*
* <dependency>*
* <groupId>org.keycloak</groupId>*
* <artifactId>keycloak-core</artifactId>*
* <version>4.8.1.Final</version>*
* <scope>provided</scope>*
* </dependency>*
* <dependency>*
* <groupId>org.keycloak</groupId>*
* <artifactId>keycloak-server-spi</artifactId>*
* <version>4.8.1.Final</version>*
* <scope>provided</scope>*
* </dependency>*
* <dependency>*
* <groupId>org.keycloak</groupId>*
* <artifactId>keycloak-server-spi-private</artifactId>*
* <version>4.8.1.Final</version>*
* <scope>provided</scope>*
* </dependency>*
* <dependency>*
* <groupId>org.jboss.spec.javax.ws.rs
<http://org.jboss.spec.javax.ws.rs></groupId>*
* <artifactId>jboss-jaxrs-api_2.1_spec</artifactId>*
* <version>1.0.2.Final</version>*
* </dependency>*
* </dependencies>*
* <build>*
* <finalName>hello-rest-example</finalName>*
* <plugins>*
* <plugin>*
* <groupId>org.apache.maven.plugins</groupId>*
* <artifactId>maven-compiler-plugin</artifactId>*
* <configuration>*
* <source>6</source>*
* <target>6</target>*
* </configuration>*
* </plugin>*
* </plugins>*
* </build>*
*</project>*
2. *Provider class*
*package org.keycloak.examples.rest;*
*import org.keycloak.models.KeycloakSession;*
*import org.keycloak.services.resource.RealmResourceProvider;*
*import javax.ws.rs.GET;*
*import javax.ws.rs.Path;*
*import javax.ws.rs.Produces;*
*/***
* * @author <a href="mailto:sthorger at redhat.com
<sthorger at redhat.com>">Stian Thorgersen</a>*
* */*
*public class HelloResourceProvider implements RealmResourceProvider {*
* private KeycloakSession session;*
* public HelloResourceProvider(KeycloakSession session) {*
* this.session = session;*
* }*
* @Override*
* public Object getResource() {*
* return this;*
* }*
* @GET*
* @Path("/customMessage1")*
* @Produces("text/plain; charset=utf-8")*
* public String get1() {*
* String name = session.getContext().getRealm().getDisplayName();*
* if (name == null) {*
* name = session.getContext().getRealm().getName();*
* }*
* return "Hello " + name;*
* }*
* @GET*
* @Path("/customMessage2")*
* @Produces("text/plain; charset=utf-8")*
* public String get2() {*
* return "Hello custom message 2";*
* }*
* @Override*
* public void close() {*
* }*
*}*
3. *Provider factory class*
*package org.keycloak.examples.rest;*
*import org.keycloak.Config.Scope;*
*import org.keycloak.models.KeycloakSession;*
*import org.keycloak.models.KeycloakSessionFactory;*
*import org.keycloak.services.resource.RealmResourceProvider;*
*import org.keycloak.services.resource.RealmResourceProviderFactory;*
*/***
* * @author <a href="mailto:sthorger at redhat.com
<sthorger at redhat.com>">Stian Thorgersen</a>*
* */*
*public class HelloResourceProviderFactory implements
RealmResourceProviderFactory {*
* public static final String ID = "hello";*
* @Override*
* public String getId() {*
* return ID;*
* }*
* @Override*
* public RealmResourceProvider create(KeycloakSession session) {*
* return new HelloResourceProvider(session);*
* }*
* @Override*
* public void init(Scope config) {*
* }*
* @Override*
* public void postInit(KeycloakSessionFactory factory) {*
* }*
* @Override*
* public void close() {*
* }*
*}*
4. Added
*META-INF/services/org.keycloak.services.resource.RealmResourceProviderFactory*
file with below content.
*org.keycloak.examples.rest.HelloResourceProviderFactory*
5. Build a jar and dropped it in the
*{$Keycloak_home}/standalone/deployments* folder and started the server.
6. Using the management console I could see that the jar is deployed. (
http://localhost:9990/console/index.html#deployments)
7. Generated a token using *admin-cli* in *master* realm with *admin user*
credentials.
8. Used that token to invoke the get endpoints "
http://localhost:8080/auth/custommessage1" and "
http://localhost:8080/auth/custommessage2". But both the nedpoints returned
HTTP 404 Not Found.
I want to know the following.
1. Have I created and deployed the custom endpoint correctly?
2. Is the URLs used to invoke endpoints are correct or not?
3. Have I used the correct way to invoke the custom endpoint. (I mean using
the token generated from admin user in master) ?
--
Sachindra Dilhara
More information about the keycloak-user
mailing list