use of org.eclipse.jetty.security.ConstraintSecurityHandler in project async-http-client by AsyncHttpClient.
the class TestUtils method addAuthHandler.
private static void addAuthHandler(Server server, String auth, LoginAuthenticator authenticator, Handler handler) {
server.addBean(LOGIN_SERVICE);
Constraint constraint = new Constraint();
constraint.setName(auth);
constraint.setRoles(new String[] { USER, ADMIN });
constraint.setAuthenticate(true);
ConstraintMapping mapping = new ConstraintMapping();
mapping.setConstraint(constraint);
mapping.setPathSpec("/*");
Set<String> knownRoles = new HashSet<>();
knownRoles.add(USER);
knownRoles.add(ADMIN);
List<ConstraintMapping> cm = new ArrayList<>();
cm.add(mapping);
ConstraintSecurityHandler security = new ConstraintSecurityHandler();
security.setConstraintMappings(cm, knownRoles);
security.setAuthenticator(authenticator);
security.setLoginService(LOGIN_SERVICE);
security.setHandler(handler);
server.setHandler(security);
}
use of org.eclipse.jetty.security.ConstraintSecurityHandler in project calcite-avatica by apache.
the class HttpServer method internalStart.
protected void internalStart() {
if (server != null) {
throw new RuntimeException("Server is already started");
}
final QueuedThreadPool threadPool = new QueuedThreadPool();
threadPool.setDaemon(true);
server = new Server(threadPool);
server.manage(threadPool);
final ServerConnector connector = configureConnector(getConnector(), port);
ConstraintSecurityHandler securityHandler = null;
if (null != this.config) {
switch(config.getAuthenticationType()) {
case SPNEGO:
// Get the Handler for SPNEGO authentication
securityHandler = configureSpnego(server, connector, this.config);
break;
case BASIC:
securityHandler = configureBasicAuthentication(server, connector, config);
break;
case DIGEST:
securityHandler = configureDigestAuthentication(server, connector, config);
break;
default:
// Pass
break;
}
}
server.setConnectors(new Connector[] { connector });
// Default to using the handler that was passed in
final HandlerList handlerList = new HandlerList();
Handler avaticaHandler = handler;
// Wrap the provided handler for security if we made one
if (null != securityHandler) {
securityHandler.setHandler(handler);
avaticaHandler = securityHandler;
}
handlerList.setHandlers(new Handler[] { avaticaHandler, new DefaultHandler() });
server.setHandler(handlerList);
try {
server.start();
} catch (Exception e) {
throw new RuntimeException(e);
}
port = connector.getLocalPort();
LOG.info("Service listening on port {}.", getPort());
// Set the information about the address for this server
try {
this.handler.setServerRpcMetadata(createRpcServerMetadata(connector));
} catch (UnknownHostException e) {
// Failed to do the DNS lookup, bail out.
throw new RuntimeException(e);
}
}
use of org.eclipse.jetty.security.ConstraintSecurityHandler in project drill by apache.
the class WebServer method createSecurityHandler.
/**
* @return {@link SecurityHandler} with appropriate {@link LoginService}, {@link Authenticator} and constraints.
*/
private ConstraintSecurityHandler createSecurityHandler() {
ConstraintSecurityHandler security = new ConstraintSecurityHandler();
Set<String> knownRoles = ImmutableSet.of(AUTHENTICATED_ROLE, ADMIN_ROLE);
security.setConstraintMappings(Collections.<ConstraintMapping>emptyList(), knownRoles);
security.setAuthenticator(new FormAuthenticator("/login", "/login", true));
security.setLoginService(new DrillRestLoginService(workManager.getContext()));
return security;
}
use of org.eclipse.jetty.security.ConstraintSecurityHandler in project hbase by apache.
the class HttpServerUtil method constrainHttpMethods.
/**
* Add constraints to a Jetty Context to disallow undesirable Http methods.
* @param ctxHandler The context to modify
*/
public static void constrainHttpMethods(ServletContextHandler ctxHandler) {
Constraint c = new Constraint();
c.setAuthenticate(true);
ConstraintMapping cmt = new ConstraintMapping();
cmt.setConstraint(c);
cmt.setMethod("TRACE");
cmt.setPathSpec("/*");
ConstraintMapping cmo = new ConstraintMapping();
cmo.setConstraint(c);
cmo.setMethod("OPTIONS");
cmo.setPathSpec("/*");
ConstraintSecurityHandler securityHandler = new ConstraintSecurityHandler();
securityHandler.setConstraintMappings(new ConstraintMapping[] { cmt, cmo });
ctxHandler.setSecurityHandler(securityHandler);
}
use of org.eclipse.jetty.security.ConstraintSecurityHandler in project jetty.project by eclipse.
the class DatabaseLoginServiceTestServer method configureServer.
protected void configureServer() throws Exception {
_protocol = "http";
_server.addBean(_loginService);
ConstraintSecurityHandler security = new ConstraintSecurityHandler();
_server.setHandler(security);
Constraint constraint = new Constraint();
constraint.setName("auth");
constraint.setAuthenticate(true);
constraint.setRoles(new String[] { "user", "admin" });
ConstraintMapping mapping = new ConstraintMapping();
mapping.setPathSpec("/*");
mapping.setConstraint(constraint);
Set<String> knownRoles = new HashSet<>();
knownRoles.add("user");
knownRoles.add("admin");
security.setConstraintMappings(Collections.singletonList(mapping), knownRoles);
security.setAuthenticator(new BasicAuthenticator());
security.setLoginService(_loginService);
ServletContextHandler root = new ServletContextHandler();
root.setContextPath("/");
root.setResourceBase(_resourceBase);
ServletHolder servletHolder = new ServletHolder(new DefaultServlet());
servletHolder.setInitParameter("gzip", "true");
root.addServlet(servletHolder, "/*");
_handler = new TestHandler(_resourceBase);
HandlerCollection handlers = new HandlerCollection();
handlers.setHandlers(new Handler[] { _handler, root });
security.setHandler(handlers);
}
Aggregations