Search in sources :

Example 11 with ConstraintSecurityHandler

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);
}
Also used : ConstraintMapping(org.eclipse.jetty.security.ConstraintMapping) Constraint(org.eclipse.jetty.util.security.Constraint) ConstraintSecurityHandler(org.eclipse.jetty.security.ConstraintSecurityHandler) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 12 with ConstraintSecurityHandler

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);
    }
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) HandlerList(org.eclipse.jetty.server.handler.HandlerList) Server(org.eclipse.jetty.server.Server) UnknownHostException(java.net.UnknownHostException) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) ConstraintSecurityHandler(org.eclipse.jetty.security.ConstraintSecurityHandler) Handler(org.eclipse.jetty.server.Handler) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler) ConstraintSecurityHandler(org.eclipse.jetty.security.ConstraintSecurityHandler) LoginException(javax.security.auth.login.LoginException) UnknownHostException(java.net.UnknownHostException) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler)

Example 13 with ConstraintSecurityHandler

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;
}
Also used : ConstraintSecurityHandler(org.eclipse.jetty.security.ConstraintSecurityHandler) FormAuthenticator(org.eclipse.jetty.security.authentication.FormAuthenticator) DrillRestLoginService(org.apache.drill.exec.server.rest.auth.DrillRestLoginService)

Example 14 with ConstraintSecurityHandler

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);
}
Also used : ConstraintMapping(org.eclipse.jetty.security.ConstraintMapping) Constraint(org.eclipse.jetty.util.security.Constraint) ConstraintSecurityHandler(org.eclipse.jetty.security.ConstraintSecurityHandler)

Example 15 with ConstraintSecurityHandler

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);
}
Also used : ConstraintMapping(org.eclipse.jetty.security.ConstraintMapping) BasicAuthenticator(org.eclipse.jetty.security.authentication.BasicAuthenticator) Constraint(org.eclipse.jetty.util.security.Constraint) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) ConstraintSecurityHandler(org.eclipse.jetty.security.ConstraintSecurityHandler) HandlerCollection(org.eclipse.jetty.server.handler.HandlerCollection) DefaultServlet(org.eclipse.jetty.servlet.DefaultServlet) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) HashSet(java.util.HashSet)

Aggregations

ConstraintSecurityHandler (org.eclipse.jetty.security.ConstraintSecurityHandler)24 ConstraintMapping (org.eclipse.jetty.security.ConstraintMapping)19 Constraint (org.eclipse.jetty.util.security.Constraint)19 HashLoginService (org.eclipse.jetty.security.HashLoginService)12 BasicAuthenticator (org.eclipse.jetty.security.authentication.BasicAuthenticator)11 Server (org.eclipse.jetty.server.Server)10 ServerConnector (org.eclipse.jetty.server.ServerConnector)5 WebAppContext (org.eclipse.jetty.webapp.WebAppContext)5 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)4 DefaultHandler (org.eclipse.jetty.server.handler.DefaultHandler)4 Password (org.eclipse.jetty.util.security.Password)4 IOException (java.io.IOException)3 HashSet (java.util.HashSet)3 Handler (org.eclipse.jetty.server.Handler)3 ContextHandlerCollection (org.eclipse.jetty.server.handler.ContextHandlerCollection)3 HandlerCollection (org.eclipse.jetty.server.handler.HandlerCollection)3 HandlerList (org.eclipse.jetty.server.handler.HandlerList)3 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)3 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)3 File (java.io.File)2