FileSecurityInitializer implements ServletContainerInitializer { @Override public void onStartup(Set<Class<?>> classes, ServletContext servletContext) throws ServletException { WebApplication webApplication = (WebApplication) servletContext; DefaultSecurityManager securityManager = new DefaultSecurityManager(); webApplication.setSecurityManager(securityManager); File userFile = new File("user.properties"); if (!userFile.exists()) { userFile = new File("etc/user.properties"); } if (userFile.exists()) { Properties userProperties = new Properties(); try (FileInputStream fileInput = new FileInputStream(userFile)) { userProperties.load(fileInput); } catch (IOException ioe) { } userProperties.entrySet().forEach(entry -> { String username = (String) entry.getKey(); String password = (String) entry.getValue(); securityManager.addUser(username, password); }); } File userRoleFile = new File("userrole.properties"); if (!userRoleFile.exists()) { userRoleFile = new File("etc/userrole.properties"); } if (userRoleFile.exists()) { Properties userRoleProperties = new Properties(); try (FileInputStream fileInput = new FileInputStream(userRoleFile)) { userRoleProperties.load(fileInput); } catch (IOException ioe) { } userRoleProperties.entrySet().forEach(entry -> { String username = (String) entry.getKey(); String roles = (String) entry.getValue(); securityManager.addUserRole(username, roles.split(",")); }); } }  @Override void onStartup(Set<Class<?>> classes, ServletContext servletContext);  }
@Test public void testOnStartup() throws Exception { DefaultWebApplication webApplication = new DefaultWebApplication(); webApplication.addInitializer(new FileSecurityInitializer()); webApplication.initialize(); DefaultSecurityManager manager = (DefaultSecurityManager) webApplication.getSecurityManager(); DefaultWebApplicationRequest request = new DefaultWebApplicationRequest(); manager.login(request, "j2ee", "j2ee"); assertTrue(manager.isUserInRole(request, "Administrator")); }