2

I am running Keycloak with the Tomcat adapter. But, when I try to get the KeycloakPrincipal, it errors out with this;

java.lang.ClassCastException: org.keycloak.KeycloakPrincipal cannot be cast to org.keycloak.KeycloakPrincipal

My code is very simple and my environment is setup exactly like the KeyCloak docs say it should be.

{
    HttpServletRequest request = (HttpServletRequest)getApp().getFromUserStorage(HttpServletRequest.class.getName());
    KeycloakPrincipal kcp = (KeycloakPrincipal) request.getUserPrincipal();
}

Any ideas as to what is wrong here? The only solutions I've found by googling have to do with Maven configuration, but Maven isn't being used here.

AbramD
  • 41
  • 3
  • Check if you have duplicate keycloak libraries in the classpath at runtime . – Arnaud Aug 20 '19 at 12:59
  • I thought about that too. The only reference to the Keycloak objects is through a reference to the Tomcat library. – AbramD Aug 20 '19 at 13:03
  • [This page](https://stackoverflow.com/questions/43865259/java-lang-classcastexception-with-the-same-class-object) may help you. – Kohei TAMURA Aug 21 '19 at 03:00

3 Answers3

1

Remoing the keycloak adapters fromWEB-INF directory solves this issue.

user2101
  • 145
  • 1
  • 11
  • 1
    Hello and welcome to Stack Overflow. Text only answers are usually not very useful unless they explain what they do. Please consider adding an explanation of what you changed or wrote. – Saud Khan Apr 06 '20 at 17:18
0

I found the issue. One of my libraries was causing a conflict.

AbramD
  • 41
  • 3
0

I also got the same issue.

solution :

a) Project/maven keycloak jar and tomcat/lib keycloak jars should have same version

b) Add scope as provided in pom.xml

<dependency>
        <groupId>org.keycloak</groupId>
        <artifactId>keycloak-core</artifactId>
        <version>17.0.0</version>
        <scope>provided</scope>
    </dependency>