* The {@link Server} for which the security handler is created.
* @return
*/
private ConstraintSecurityHandler createSecurityHandler(Server server,
ServletDefinition servletDefinition) {
ConstraintSecurityHandler securityHandler = new ConstraintSecurityHandler();
securityHandler.setServer(server);
Constraint constraint = new Constraint();
constraint.setName("security" + servletDefinition.hashCode());
if (servletDefinition.isRequireBasicAuth()) {
// add basic authentication and role-based authorization based on
// the credentials store (realm file)
LoginService loginService = new HashLoginService(
"elastisys:scale security realm",
servletDefinition.getRealmFile());
securityHandler.getServer().addBean(loginService);
securityHandler.setAuthenticator(new BasicAuthenticator());
securityHandler.setLoginService(loginService);
constraint.setAuthenticate(true);
constraint.setRoles(new String[] { servletDefinition
.getRequireRole() });
}
// require confidential transport: HTTP requests will be redirected to
// the secure (https) port.
if (servletDefinition.isRequireHttps()) {
constraint.setDataConstraint(Constraint.DC_CONFIDENTIAL);
}
// apply constraint to all pages/web resources
ConstraintMapping mapping = new ConstraintMapping();
mapping.setConstraint(constraint);
mapping.setPathSpec("/*");
securityHandler.setConstraintMappings(Lists.newArrayList(mapping));
return securityHandler;
}