Not able to get KeycloakPrincipal when using keycloak spring boot integrated with keycloak spring security adapter

I am not able to get KeycloakPrincipal, when using Keycloak with Spring Security. How do I solve this?

Check if your user principal is an instance of KeycloakPrincipal and than cast it.
Without much information or code examples that’s the only thing I can give you.

HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
KeycloakPrincipal keycloakPrincipal = (KeycloakPrincipal) request.getUserPrincipal();

I use it like this:

class ProtectedController {
    var logger: Logger = LoggerFactory.getLogger(

    // @Autowired
    // private lateinit var auth: KeycloakSecurityContext

    fun saySomething(authToken: KeycloakAuthenticationToken, message: String): String {
        val principal = authToken.principal as KeycloakPrincipal<KeycloakSecurityContext>

        return "Psst: (wisper) $message -> ${}"

(In Kotlin but you get the idea…)