简体   繁体   中英

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.

从WEB-INF 目录移除keycloak 适配器解决了这个问题。

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

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM