Search code examples
javajbosswildflywildfly-8user-roles

Is it possible to remove role or flush cache in jboss at runtime?


In my webapp, running in Wildfly, there are several roles defined. User is given several tabs for each role he has (e.g. admin, support etc). User/admin can also enable/disable roles for himself or for other users in browser. But when the role is added/removed, tab should be added/removed as well. And that only happens if jboss cache is flushed manually from cli or even worse - restarted. Is it possible to remove the role or flush server cache at runtime (when user clicks the button)? Role authentication is done via 'request.isUserInRole()', but what I need is something like setUserInRole("admin")=false.


Solution

  • This is how I resolved it.

    public void flushAuthenticationCache(String userid) {
    
        final String domain = "mydomain";
        try {
            ObjectName jaasMgr = new ObjectName("jboss.as:subsystem=security,security-domain=" + domain);
            Object[] params = {userid};
            String[] signature = {"java.lang.String"};
            MBeanServer server = (MBeanServer) MBeanServerFactory.findMBeanServer(null).get(0);
            server.invoke(jaasMgr, "flushCache", params, signature);
    
            } catch (Throwable e) {
            e.printStackTrace();
          }
    }
    

    Note that the method above flushes cache for specific user only. The method below you flush cache for all users:

    public static final void flushJaasCache(String securityDomain){  
          try {  
               javax.management.MBeanServerConnection mbeanServerConnection = java.lang.management.ManagementFactory  
                         .getPlatformMBeanServer();  
               javax.management.ObjectName mbeanName = new javax.management.ObjectName("jboss.as:subsystem=security,security-domain="+securityDomain);  
               mbeanServerConnection.invoke(mbeanName, "flushCache", null, null);  
          } catch (Exception e) {  
               throw new SecurityException(e);  
          }  
    }