// Trying to create a basic authentication k/v scheme for a resource handler
HashLoginService loginService = new HashLoginService("MyRealm");
loginService.putUser("username1", new Password("password1"), new String[]{"user1"});
loginService.putUser("username2", new Password("password2"), new String[]{"user2"});
loginService.putUser("username3", new Password("password3"), new String[]{"user3"});
server.addBean(loginService);
ConstraintSecurityHandler security = new ConstraintSecurityHandler();
server.setHandler(security);
Constraint constraint = new Constraint();
constraint.setName(Constraint.__BASIC_AUTH);
constraint.setAuthenticate(true);
constraint.setRoles(new String[]{"user", "admin"});
ConstraintMapping mapping = new ConstraintMapping();
mapping.setPathSpec("/*");
mapping.setConstraint(constraint);
security.setConstraintMappings(Collections.singletonList(mapping));
security.setAuthenticator(new BasicAuthenticator());
security.setLoginService(loginService);
ResourceHandler resource = new ResourceHandler();
resource.setDirectoriesListed(true);
resource.setResourceBase("/root");
ContextHandler contextHandler = new ContextHandler();
contextHandler.setContextPath("/root");
contextHandler.setHandler(resource);
server.setHandler(contextHandler);
// This did not work ...
security.setHandler(contextHandler);