title: Obtaining user roles in servlet application using keycloak
body: <p>I'm using keycloak to protect my servlet. I have to add new roles and assign them to users dynamically. It works in keycloak using admin API, but I can't figure out how to obtain the roles for specific user in a servlet. </p>
  
  <p>I tried this solution, but I get empty set:</p>
  
  <pre><code>protected void doPost(HttpServletRequest request, HttpServletResponse response) {
  ...
  
  KeycloakSecurityContext context = (KeycloakSecurityContext)request.getAttribute(KeycloakSecurityContext.class.getName());
      Set&lt;String&gt; roles = AdapterUtils.getRolesFromSecurityContext((RefreshableKeycloakSecurityContext) context);
  ...
  }
  </code></pre>