我正在使用keycloak来保护我的servlet.我必须添加新角色并动态地将它们分配给用户.它使用admin API在keycloak中工作,但我无法弄清楚如何在servlet中获取特定用户的角色.
我尝试了这个解决方案,但我得到了空集:
protected void doPost(HttpServletRequest request, HttpServletResponse response) {
...
KeycloakSecurityContext context = (KeycloakSecurityContext)request.getAttribute(KeycloakSecurityContext.class.getName());
Set<String> roles = AdapterUtils.getRolesFromSecurityContext((RefreshableKeycloakSecurityContext) context);
...
}
Run Code Online (Sandbox Code Playgroud)