# Scopes configuration based on roles,

**URL:** <https://forum.keycloak.org/t/scopes-configuration-based-on-roles/6505>\
**Category:** Securing applications\
**Tags:** authentication, client-configuration\
**Created:** [December 21, 2020, 8:12pm UTC](https://forum.keycloak.org/t/scopes-configuration-based-on-roles/6505 "2020-12-21T20:12:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![peletomi](https://avatars.discourse-cdn.com/v4/letter/p/ba9def/32.png) [@peletomi](https://forum.keycloak.org/u/peletomi)\
**Post date:** [December 21, 2020, 8:12pm UTC](https://forum.keycloak.org/t/scopes-configuration-based-on-roles/6505/1 "2020-12-21T20:12:58Z")

</div>

Hello,

I am using Keycloak with two React frontends and a backend. The two frontends use two separate clients. The scopes are currently assigned to the clients. This has the drawback that the user for frontend A is able to log in into frontend B, receiving the scopes of client B.

- Is there a way to configure scopes based on the users role?
- Is there a recommended way to fix the above situation?

---

<div class="post-metadata">

**Author:** ![Robinyo](https://avatars.discourse-cdn.com/v4/letter/r/dec6dc/32.png) [@Robinyo](https://forum.keycloak.org/u/Robinyo)\
**Post date:** [December 21, 2020, 8:22pm UTC](https://forum.keycloak.org/t/scopes-configuration-based-on-roles/6505/2 "2020-12-21T20:22:31Z")

</div>

See: [Angular, OAuth 2.0 and Keycloak](https://robferguson.org/blog/2019/12/31/angular-oauth2-keycloak/)

---

<div class="post-metadata">

**Author:** ![peletomi](https://avatars.discourse-cdn.com/v4/letter/p/ba9def/32.png) [@peletomi](https://forum.keycloak.org/u/peletomi)\
**Post date:** [December 21, 2020, 8:35pm UTC](https://forum.keycloak.org/t/scopes-configuration-based-on-roles/6505/3 "2020-12-21T20:35:46Z")

</div>

What a pity.

I came up with the following:

- the user has a roles assigned
- the client has a real role mapping, which puts the role into the access token next to the client scopes
- in the backend I would validate for role from the user + scope from the client

Would this make sense?

---

<div class="post-metadata">

**Author:** ![peletomi](https://avatars.discourse-cdn.com/v4/letter/p/ba9def/32.png) [@peletomi](https://forum.keycloak.org/u/peletomi)\
**Post date:** [December 22, 2020, 10:07pm UTC](https://forum.keycloak.org/t/scopes-configuration-based-on-roles/6505/4 "2020-12-22T22:07:22Z")

</div>

My solution is as follows.

In Keycloak I add a User Realm Role Mapper to the client.

 ![image](https://global.discourse-cdn.com/free1/uploads/keycloak/original/2X/c/c92a9594cbcb67e290bdb1e6049317dcdd0cb2b9.png)

This puts the roles of the user into the access token.

Then in Spring I have created an interceptor, which checks the roles in the token against roles defined in an annotation on the controller.

```auto
@Component
class RoleCheckInterceptor(private val mapper: ObjectMapper) : HandlerInterceptor {

    override fun preHandle(request: HttpServletRequest, response: HttpServletResponse, handler: Any): Boolean {
        if (handler !is HandlerMethod) {
            return true
        }
        val rolesFromAnnotation = mutableSetOf<String>()
        handler.bean::class.findAnnotation<AuthorizeRoles>()?.value?.forEach { rolesFromAnnotation.add(it) }
        handler.method.getAnnotation(AuthorizeRoles::class.java)?.value?.forEach { rolesFromAnnotation.add(it) }
        if (rolesFromAnnotation.isEmpty()) {
            return true
        }

        val roles = request.getHeader("Authorization")?.let { headerValue ->
            val jwt = JWTParser.parse(headerValue.split(" ")[1])
            jwt.jwtClaimsSet?.getClaim("roles")?.let { r ->
                val roles = r as JSONArray
                HashSet(roles)
            }
        } ?: emptySet<String>()
        val allowed = rolesFromAnnotation.all { roles.contains(it) }
        return if (allowed) {
            true
        } else {
            val unauthorized = Unauthorized(request.requestURI).toFE()
            response.status = unauthorized.status
            response.setHeader(HttpHeaders.CONTENT_TYPE, problemContentType)
            mapper.writeValue(response.writer, unauthorized)
            response.writer.flush()
            response.writer.close()
            false
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![gsepehr](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/gsepehr/32/7144_2.png) [@gsepehr](https://forum.keycloak.org/u/gsepehr)\
**Post date:** [December 1, 2022, 11:54am UTC](https://forum.keycloak.org/t/scopes-configuration-based-on-roles/6505/5 "2022-12-01T11:54:40Z")

</div>

**Life saver**. This is exactly what I expected. Keycloak client `authorization` explains well how to set scopes for users if they have certain roles, but it wasn’t at all obvious how to do it without `authorization`. Thank you.
