final Constraint requireAuthentication = new Constraint();
requireAuthentication.setAuthenticate(true);
requireAuthentication.setRoles(new String[] { "user" });
final ConstraintMapping authenticateAll = new ConstraintMapping();
authenticateAll.setPathSpec("/*");
authenticateAll.setConstraint(requireAuthentication);
final Constraint passThrough = new Constraint();
passThrough.setAuthenticate(false);
final ConstraintMapping healthCheckExemption = new ConstraintMapping();
healthCheckExemption.setPathSpec("/health/");
healthCheckExemption.setConstraint(passThrough);
final ConstraintMapping statsExemption = new ConstraintMapping();
statsExemption.setPathSpec("/stats/*");
statsExemption.setConstraint(passThrough);
securityHandler.setConstraintMappings(new ConstraintMapping[] { authenticateAll, healthCheckExemption, statsExemption });
context.setSecurityHandler(securityHandler);
}