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

**URL:** <https://forum.keycloak.org/t/not-able-to-get-keycloakprincipal-when-using-keycloak-spring-boot-integrated-with-keycloak-spring-security-adapter/2204>\
**Category:** Securing applications\
**Tags:** authentication\
**Created:** [April 17, 2020, 6:10am UTC](https://forum.keycloak.org/t/not-able-to-get-keycloakprincipal-when-using-keycloak-spring-boot-integrated-with-keycloak-spring-security-adapter/2204 "2020-04-17T06:10:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![shreya-shah](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/shreya-shah/32/674_2.png) [@shreya-shah](https://forum.keycloak.org/u/shreya-shah)\
**Post date:** [April 17, 2020, 6:10am UTC](https://forum.keycloak.org/t/not-able-to-get-keycloakprincipal-when-using-keycloak-spring-boot-integrated-with-keycloak-spring-security-adapter/2204/1 "2020-04-17T06:10:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![zonaut](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/zonaut/32/666_2.png) [@zonaut](https://forum.keycloak.org/u/zonaut)\
**Post date:** [April 19, 2020, 3:51pm UTC](https://forum.keycloak.org/t/not-able-to-get-keycloakprincipal-when-using-keycloak-spring-boot-integrated-with-keycloak-spring-security-adapter/2204/2 "2020-04-19T15:51:39Z")

</div>

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.

```auto
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
    .currentRequestAttributes()).getRequest();
KeycloakPrincipal keycloakPrincipal = (KeycloakPrincipal) request.getUserPrincipal();

```

---

<div class="post-metadata">

**Author:** ![MikeTheBiker](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/mikethebiker/32/223_2.png) [@MikeTheBiker](https://forum.keycloak.org/u/MikeTheBiker)\
**Post date:** [May 27, 2020, 7:32am UTC](https://forum.keycloak.org/t/not-able-to-get-keycloakprincipal-when-using-keycloak-spring-boot-integrated-with-keycloak-spring-security-adapter/2204/3 "2020-05-27T07:32:53Z")

</div>

I use it like this:

```auto
@RestController
@RequestMapping("/protected")
class ProtectedController {
    var logger: Logger = LoggerFactory.getLogger(ProtectedController::class.java)

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

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

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

```

(In Kotlin but you get the idea…)
