Search in sources :

Example 11 with BasicAuthenticator

use of org.eclipse.jetty.security.authentication.BasicAuthenticator in project symmetric-ds by JumpMind.

the class SymmetricWebServer method setupBasicAuthIfNeeded.

protected void setupBasicAuthIfNeeded(Server server) {
    if (StringUtils.isNotBlank(basicAuthUsername)) {
        ConstraintSecurityHandler sh = new ConstraintSecurityHandler();
        Constraint constraint = new Constraint();
        constraint.setName(Constraint.__BASIC_AUTH);
        constraint.setRoles(new String[] { SecurityConstants.EMBEDDED_WEBSERVER_DEFAULT_ROLE });
        constraint.setAuthenticate(true);
        ConstraintMapping cm = new ConstraintMapping();
        cm.setConstraint(constraint);
        cm.setPathSpec("/*");
        // sh.setConstraintMappings(new ConstraintMapping[] {cm});
        sh.addConstraintMapping(cm);
        sh.setAuthenticator(new BasicAuthenticator());
        HashLoginService loginService = new HashLoginService();
        loginService.putUser(basicAuthUsername, new Password(basicAuthPassword), null);
        sh.setLoginService(loginService);
        server.setHandler(sh);
    }
}
Also used : ConstraintMapping(org.eclipse.jetty.security.ConstraintMapping) HashLoginService(org.eclipse.jetty.security.HashLoginService) BasicAuthenticator(org.eclipse.jetty.security.authentication.BasicAuthenticator) Constraint(org.eclipse.jetty.util.security.Constraint) ConstraintSecurityHandler(org.eclipse.jetty.security.ConstraintSecurityHandler) Password(org.eclipse.jetty.util.security.Password)

Example 12 with BasicAuthenticator

use of org.eclipse.jetty.security.authentication.BasicAuthenticator in project opennms by OpenNMS.

the class JUnitServer method initializeServerWithConfig.

protected void initializeServerWithConfig(final JUnitHttpServer config) {
    Server server = null;
    if (config.https()) {
        server = new Server();
        // SSL context configuration
        SslContextFactory sslContextFactory = new SslContextFactory();
        sslContextFactory.setKeyStorePath(config.keystore());
        sslContextFactory.setKeyStorePassword(config.keystorePassword());
        sslContextFactory.setKeyManagerPassword(config.keyPassword());
        sslContextFactory.setTrustStorePath(config.keystore());
        sslContextFactory.setTrustStorePassword(config.keystorePassword());
        // HTTP Configuration
        HttpConfiguration http_config = new HttpConfiguration();
        http_config.setSecureScheme("https");
        http_config.setSecurePort(config.port());
        http_config.setOutputBufferSize(32768);
        http_config.setRequestHeaderSize(8192);
        http_config.setResponseHeaderSize(8192);
        http_config.setSendServerVersion(true);
        http_config.setSendDateHeader(false);
        // SSL HTTP Configuration
        HttpConfiguration https_config = new HttpConfiguration(http_config);
        https_config.addCustomizer(new SecureRequestCustomizer());
        // SSL Connector
        ServerConnector sslConnector = new ServerConnector(server, new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()), new HttpConnectionFactory(https_config));
        sslConnector.setPort(config.port());
        server.addConnector(sslConnector);
    } else {
        server = new Server(config.port());
    }
    m_server = server;
    final ContextHandler context1 = new ContextHandler();
    context1.setContextPath("/");
    context1.setWelcomeFiles(new String[] { "index.html" });
    context1.setResourceBase(config.resource());
    context1.setClassLoader(Thread.currentThread().getContextClassLoader());
    context1.setVirtualHosts(config.vhosts());
    final ContextHandler context = context1;
    Handler topLevelHandler = null;
    final HandlerList handlers = new HandlerList();
    if (config.basicAuth()) {
        // check for basic auth if we're configured to do so
        LOG.debug("configuring basic auth");
        final HashLoginService loginService = new HashLoginService("MyRealm", config.basicAuthFile());
        loginService.setHotReload(true);
        m_server.addBean(loginService);
        final ConstraintSecurityHandler security = new ConstraintSecurityHandler();
        final Set<String> knownRoles = new HashSet<String>();
        knownRoles.add("user");
        knownRoles.add("admin");
        knownRoles.add("moderator");
        final Constraint constraint = new Constraint();
        constraint.setName("auth");
        constraint.setAuthenticate(true);
        constraint.setRoles(knownRoles.toArray(new String[0]));
        final ConstraintMapping mapping = new ConstraintMapping();
        mapping.setPathSpec("/*");
        mapping.setConstraint(constraint);
        security.setConstraintMappings(Collections.singletonList(mapping), knownRoles);
        security.setAuthenticator(new BasicAuthenticator());
        security.setLoginService(loginService);
        security.setRealmName("MyRealm");
        security.setHandler(context);
        topLevelHandler = security;
    } else {
        topLevelHandler = context;
    }
    final Webapp[] webapps = config.webapps();
    if (webapps != null) {
        for (final Webapp webapp : webapps) {
            final WebAppContext wac = new WebAppContext();
            String path = null;
            if (!"".equals(webapp.pathSystemProperty()) && System.getProperty(webapp.pathSystemProperty()) != null) {
                path = System.getProperty(webapp.pathSystemProperty());
            } else {
                path = webapp.path();
            }
            if (path == null || "".equals(path)) {
                throw new IllegalArgumentException("path or pathSystemProperty of @Webapp points to a null or blank value");
            }
            wac.setWar(path);
            wac.setContextPath(webapp.context());
            handlers.addHandler(wac);
        }
    }
    final ResourceHandler rh = new ResourceHandler();
    rh.setWelcomeFiles(new String[] { "index.html" });
    rh.setResourceBase(config.resource());
    handlers.addHandler(rh);
    // fall through to default
    handlers.addHandler(new DefaultHandler());
    context.setHandler(handlers);
    m_server.setHandler(topLevelHandler);
}
Also used : HandlerList(org.eclipse.jetty.server.handler.HandlerList) ConstraintMapping(org.eclipse.jetty.security.ConstraintMapping) SecureRequestCustomizer(org.eclipse.jetty.server.SecureRequestCustomizer) JUnitHttpServer(org.opennms.core.test.http.annotations.JUnitHttpServer) Server(org.eclipse.jetty.server.Server) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) Constraint(org.eclipse.jetty.util.security.Constraint) Handler(org.eclipse.jetty.server.Handler) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler) ConstraintSecurityHandler(org.eclipse.jetty.security.ConstraintSecurityHandler) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) SslConnectionFactory(org.eclipse.jetty.server.SslConnectionFactory) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler) ServerConnector(org.eclipse.jetty.server.ServerConnector) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) WebAppContext(org.eclipse.jetty.webapp.WebAppContext) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) HashLoginService(org.eclipse.jetty.security.HashLoginService) BasicAuthenticator(org.eclipse.jetty.security.authentication.BasicAuthenticator) ConstraintSecurityHandler(org.eclipse.jetty.security.ConstraintSecurityHandler) HashSet(java.util.HashSet) Webapp(org.opennms.core.test.http.annotations.Webapp)

Example 13 with BasicAuthenticator

use of org.eclipse.jetty.security.authentication.BasicAuthenticator in project jena by apache.

the class SPARQLServer method buildServer.

// Later : private and in constructor.
private ServletContextHandler buildServer(String jettyConfig, boolean enableCompression) {
    if (jettyConfig != null) {
        // --jetty-config=jetty-fuseki.xml
        // for detailed configuration of the server using Jetty features.
        server = configServer(jettyConfig);
    } else
        server = defaultServerConfig(serverConfig.port, serverConfig.loopback);
    // Keep the server to a maximum number of threads.
    // server.setThreadPool(new QueuedThreadPool(ThreadPoolSize)) ;
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setErrorHandler(new FusekiErrorHandler());
    context.addEventListener(new FusekiServletContextListener(this));
    // Increase form size.
    context.getServletContext().getContextHandler().setMaxFormContentSize(10 * 1000 * 1000);
    // Wire up authentication if appropriate
    if (jettyConfig == null && serverConfig.authConfigFile != null) {
        Constraint constraint = new Constraint();
        constraint.setName(Constraint.__BASIC_AUTH);
        constraint.setRoles(new String[] { "fuseki" });
        constraint.setAuthenticate(true);
        ConstraintMapping mapping = new ConstraintMapping();
        mapping.setConstraint(constraint);
        mapping.setPathSpec("/*");
        IdentityService identService = new DefaultIdentityService();
        ConstraintSecurityHandler securityHandler = new ConstraintSecurityHandler();
        securityHandler.addConstraintMapping(mapping);
        securityHandler.setIdentityService(identService);
        HashLoginService loginService = new HashLoginService("Fuseki Authentication", serverConfig.authConfigFile);
        loginService.setIdentityService(identService);
        securityHandler.setLoginService(loginService);
        securityHandler.setAuthenticator(new BasicAuthenticator());
        context.setSecurityHandler(securityHandler);
        serverLog.debug("Basic Auth Configuration = " + serverConfig.authConfigFile);
    }
    // Wire up context handler to server
    server.setHandler(context);
    // Constants. Add RDF types.
    MimeTypes mt = new MimeTypes();
    mt.addMimeMapping("rdf", WebContent.contentTypeRDFXML + ";charset=utf-8");
    mt.addMimeMapping("ttl", WebContent.contentTypeTurtle + ";charset=utf-8");
    mt.addMimeMapping("nt", WebContent.contentTypeNTriples + ";charset=ascii");
    mt.addMimeMapping("nq", WebContent.contentTypeNQuads + ";charset=ascii");
    mt.addMimeMapping("trig", WebContent.contentTypeTriG + ";charset=utf-8");
    // mt.addMimeMapping("tpl", "text/html;charset=utf-8") ;
    context.setMimeTypes(mt);
    server.setHandler(context);
    serverLog.debug("Pages = " + serverConfig.pages);
    boolean installManager = true;
    boolean installServices = true;
    String validationRoot = "/validate";
    if (installManager || installServices) {
        // TODO Respect port.
        if (serverConfig.pagesPort != serverConfig.port)
            serverLog.warn("Not supported yet - pages on a different port to services");
        if (serverConfig.pages != null) {
            if (!FileOps.exists(serverConfig.pages))
                serverLog.warn("No pages directory - " + serverConfig.pages);
            String base = serverConfig.pages;
            Map<String, Object> data = new HashMap<>();
            data.put("mgt", new MgtFunctions());
            SimpleVelocityServlet templateEngine = new SimpleVelocityServlet(base, data);
            addServlet(context, templateEngine, "*.tpl", false);
        }
    }
    if (installManager) {
        // Action when control panel selects a dataset.
        HttpServlet datasetChooser = new ActionDataset();
        addServlet(context, datasetChooser, PageNames.actionDatasetNames, false);
    }
    if (installServices) {
        // Validators
        HttpServlet validateQuery = new QueryValidator();
        HttpServlet validateUpdate = new UpdateValidator();
        HttpServlet validateData = new DataValidator();
        HttpServlet validateIRI = new IRIValidator();
        HttpServlet dumpService = new DumpServlet();
        HttpServlet generalQueryService = new SPARQL_QueryGeneral();
        addServlet(context, validateQuery, validationRoot + "/query", false);
        addServlet(context, validateUpdate, validationRoot + "/update", false);
        addServlet(context, validateData, validationRoot + "/data", false);
        addServlet(context, validateIRI, validationRoot + "/iri", false);
        // general query processor.
        addServlet(context, generalQueryService, HttpNames.ServiceGeneralQuery, enableCompression);
    }
    if (installManager || installServices) {
        String[] files = { "fuseki.html", "index.html" };
        context.setWelcomeFiles(files);
        addContent(context, "/", serverConfig.pages);
    }
    return context;
}
Also used : Constraint(org.eclipse.jetty.util.security.Constraint) MgtFunctions(org.apache.jena.fuseki.mgt.MgtFunctions) BasicAuthenticator(org.eclipse.jetty.security.authentication.BasicAuthenticator) IRIValidator(org.apache.jena.fuseki.validation.IRIValidator) ActionDataset(org.apache.jena.fuseki.mgt.ActionDataset) HttpServlet(javax.servlet.http.HttpServlet) MimeTypes(org.eclipse.jetty.http.MimeTypes) UpdateValidator(org.apache.jena.fuseki.validation.UpdateValidator) QueryValidator(org.apache.jena.fuseki.validation.QueryValidator) DataValidator(org.apache.jena.fuseki.validation.DataValidator) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler)

Example 14 with BasicAuthenticator

use of org.eclipse.jetty.security.authentication.BasicAuthenticator in project jena by apache.

the class JettyFuseki method security.

// This is now provided by Shiro.
private static void security(ServletContextHandler context, String authfile) {
    Constraint constraint = new Constraint();
    constraint.setName(Constraint.__BASIC_AUTH);
    constraint.setRoles(new String[] { "fuseki" });
    constraint.setAuthenticate(true);
    ConstraintMapping mapping = new ConstraintMapping();
    mapping.setConstraint(constraint);
    mapping.setPathSpec("/*");
    IdentityService identService = new DefaultIdentityService();
    ConstraintSecurityHandler securityHandler = new ConstraintSecurityHandler();
    securityHandler.addConstraintMapping(mapping);
    securityHandler.setIdentityService(identService);
    HashLoginService loginService = new HashLoginService("Fuseki Authentication", authfile);
    loginService.setIdentityService(identService);
    securityHandler.setLoginService(loginService);
    securityHandler.setAuthenticator(new BasicAuthenticator());
    context.setSecurityHandler(securityHandler);
    serverLog.debug("Basic Auth Configuration = " + authfile);
}
Also used : BasicAuthenticator(org.eclipse.jetty.security.authentication.BasicAuthenticator) Constraint(org.eclipse.jetty.util.security.Constraint)

Example 15 with BasicAuthenticator

use of org.eclipse.jetty.security.authentication.BasicAuthenticator 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

BasicAuthenticator (org.eclipse.jetty.security.authentication.BasicAuthenticator)30 Constraint (org.eclipse.jetty.util.security.Constraint)19 Test (org.junit.Test)12 ConstraintMapping (org.eclipse.jetty.security.ConstraintMapping)11 ConstraintSecurityHandler (org.eclipse.jetty.security.ConstraintSecurityHandler)11 HashLoginService (org.eclipse.jetty.security.HashLoginService)11 Server (org.eclipse.jetty.server.Server)4 HashSet (java.util.HashSet)3 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)3 ArrayList (java.util.ArrayList)2 ClientCertAuthenticator (org.eclipse.jetty.security.authentication.ClientCertAuthenticator)2 DigestAuthenticator (org.eclipse.jetty.security.authentication.DigestAuthenticator)2 FormAuthenticator (org.eclipse.jetty.security.authentication.FormAuthenticator)2 SpnegoAuthenticator (org.eclipse.jetty.security.authentication.SpnegoAuthenticator)2 Handler (org.eclipse.jetty.server.Handler)2 ServerConnector (org.eclipse.jetty.server.ServerConnector)2 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)2 DefaultHandler (org.eclipse.jetty.server.handler.DefaultHandler)2 HandlerCollection (org.eclipse.jetty.server.handler.HandlerCollection)2 HandlerList (org.eclipse.jetty.server.handler.HandlerList)2