'Keycloak with Tomcat: org.keycloak.KeycloakPrincipal cannot be cast to org.keycloak.KeycloakPrincipal
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.
Solution 1:[1]
Remoing the keycloak adapters fromWEB-INF directory solves this issue.
Solution 2:[2]
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>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | user2101 |
Solution 2 | JagadeeshaHP |