Search in sources :

Example 1 with SecurityHandler

use of org.mortbay.jetty.security.SecurityHandler in project maven-plugins by apache.

the class ProjectInfoReportUtilsTest method startJetty.

private void startJetty(boolean isSSL, boolean withAuth) throws Exception {
    jettyServer = new Server();
    jettyServer.setStopAtShutdown(true);
    Connector connector = (isSSL ? getSSLConnector() : getDefaultConnector());
    jettyServer.setConnectors(new Connector[] { connector });
    WebAppContext webapp = new WebAppContext();
    webapp.setContextPath("/");
    webapp.setResourceBase(getBasedir() + "/target/classes/");
    webapp.setServer(jettyServer);
    if (withAuth) {
        Constraint constraint = new Constraint();
        constraint.setName(Constraint.__BASIC_AUTH);
        constraint.setRoles(new String[] { "user", "admin" });
        constraint.setAuthenticate(true);
        ConstraintMapping cm = new ConstraintMapping();
        cm.setConstraint(constraint);
        cm.setPathSpec("/*");
        SecurityHandler sh = new SecurityHandler();
        sh.setUserRealm(new HashUserRealm("MyRealm", getBasedir() + "/src/test/resources/realm.properties"));
        sh.setConstraintMappings(new ConstraintMapping[] { cm });
        webapp.addHandler(sh);
    }
    DefaultHandler defaultHandler = new DefaultHandler();
    defaultHandler.setServer(jettyServer);
    Handler[] handlers = new Handler[2];
    handlers[0] = webapp;
    handlers[1] = defaultHandler;
    jettyServer.setHandlers(handlers);
    jettyServer.start();
    port = connector.getLocalPort();
}
Also used : Connector(org.mortbay.jetty.Connector) SelectChannelConnector(org.mortbay.jetty.nio.SelectChannelConnector) SslSocketConnector(org.mortbay.jetty.security.SslSocketConnector) WebAppContext(org.mortbay.jetty.webapp.WebAppContext) SecurityHandler(org.mortbay.jetty.security.SecurityHandler) ConstraintMapping(org.mortbay.jetty.security.ConstraintMapping) Server(org.mortbay.jetty.Server) Constraint(org.mortbay.jetty.security.Constraint) Handler(org.mortbay.jetty.Handler) SecurityHandler(org.mortbay.jetty.security.SecurityHandler) DefaultHandler(org.mortbay.jetty.handler.DefaultHandler) HashUserRealm(org.mortbay.jetty.security.HashUserRealm) DefaultHandler(org.mortbay.jetty.handler.DefaultHandler)

Example 2 with SecurityHandler

use of org.mortbay.jetty.security.SecurityHandler in project exhibitor by soabase.

the class ExhibitorMain method addSecurityFile.

private void addSecurityFile(HashUserRealm realm, String securityFile, Context root) throws Exception {
    // create a temp Jetty context to parse the security portion of the web.xml file
    /*
            TODO

            This code assumes far too much internal knowledge of Jetty. I don't know
            of simple way to parse the web.xml though and don't want to write it myself.
         */
    final URL url = new URL("file", null, securityFile);
    final WebXmlConfiguration webXmlConfiguration = new WebXmlConfiguration();
    WebAppContext context = new WebAppContext();
    context.setServer(server);
    webXmlConfiguration.setWebAppContext(context);
    ContextHandler contextHandler = new ContextHandler("/") {

        @Override
        protected void startContext() throws Exception {
            super.startContext();
            setServer(server);
            webXmlConfiguration.configure(url.toString());
        }
    };
    try {
        SecurityHandler securityHandler = webXmlConfiguration.getWebAppContext().getSecurityHandler();
        if (realm != null) {
            securityHandler.setUserRealm(realm);
        }
        root.setSecurityHandler(securityHandler);
        contextHandler.start();
    } finally {
        contextHandler.stop();
    }
}
Also used : ContextHandler(org.mortbay.jetty.handler.ContextHandler) WebAppContext(org.mortbay.jetty.webapp.WebAppContext) SecurityHandler(org.mortbay.jetty.security.SecurityHandler) WebXmlConfiguration(org.mortbay.jetty.webapp.WebXmlConfiguration) URL(java.net.URL)

Example 3 with SecurityHandler

use of org.mortbay.jetty.security.SecurityHandler in project exhibitor by soabase.

the class ExhibitorCreator method makeSecurityHandler.

private SecurityHandler makeSecurityHandler(String realm, String consoleUser, String consolePassword, String curatorUser, String curatorPassword) {
    HashUserRealm userRealm = new HashUserRealm(realm);
    userRealm.put(consoleUser, Credential.getCredential(consolePassword));
    userRealm.addUserToRole(consoleUser, "console");
    userRealm.put(curatorUser, Credential.getCredential(curatorPassword));
    userRealm.addUserToRole(curatorUser, "curator");
    Constraint console = new Constraint();
    console.setName("consoleauth");
    console.setRoles(new String[] { "console" });
    console.setAuthenticate(true);
    Constraint curator = new Constraint();
    curator.setName("curatorauth");
    curator.setRoles(new String[] { "curator", "console" });
    curator.setAuthenticate(true);
    ConstraintMapping consoleMapping = new ConstraintMapping();
    consoleMapping.setConstraint(console);
    consoleMapping.setPathSpec("/*");
    ConstraintMapping curatorMapping = new ConstraintMapping();
    curatorMapping.setConstraint(curator);
    curatorMapping.setPathSpec("/exhibitor/v1/cluster/list");
    SecurityHandler handler = new SecurityHandler();
    handler.setUserRealm(userRealm);
    handler.setConstraintMappings(new ConstraintMapping[] { consoleMapping, curatorMapping });
    handler.setAuthenticator(new BasicAuthenticator());
    return handler;
}
Also used : SecurityHandler(org.mortbay.jetty.security.SecurityHandler) ConstraintMapping(org.mortbay.jetty.security.ConstraintMapping) BasicAuthenticator(org.mortbay.jetty.security.BasicAuthenticator) Constraint(org.mortbay.jetty.security.Constraint) HashUserRealm(org.mortbay.jetty.security.HashUserRealm)

Example 4 with SecurityHandler

use of org.mortbay.jetty.security.SecurityHandler in project sonatype-aether by sonatype.

the class HttpServer method newSecurityHandler.

protected Handler newSecurityHandler() {
    List<ConstraintMapping> mappings = new ArrayList<ConstraintMapping>();
    for (String pathSpec : securedRealms.keySet()) {
        String[] roles = securedRealms.get(pathSpec);
        Constraint constraint = new Constraint();
        constraint.setName(Constraint.__BASIC_AUTH);
        constraint.setRoles(roles);
        constraint.setAuthenticate(true);
        ConstraintMapping constraintMapping = new ConstraintMapping();
        constraintMapping.setConstraint(constraint);
        constraintMapping.setPathSpec(pathSpec);
        mappings.add(constraintMapping);
    }
    HashUserRealm userRealm = new HashUserRealm("TestRealm");
    for (String username : userPasswords.keySet()) {
        String password = userPasswords.get(username);
        String[] roles = userRoles.get(username);
        userRealm.put(username, password);
        if (roles != null) {
            for (String role : roles) {
                userRealm.addUserToRole(username, role);
            }
        }
    }
    SecurityHandler securityHandler = new SecurityHandler();
    securityHandler.setUserRealm(userRealm);
    securityHandler.setConstraintMappings(mappings.toArray(new ConstraintMapping[mappings.size()]));
    return securityHandler;
}
Also used : SecurityHandler(org.mortbay.jetty.security.SecurityHandler) ConstraintMapping(org.mortbay.jetty.security.ConstraintMapping) Constraint(org.mortbay.jetty.security.Constraint) ArrayList(java.util.ArrayList) HashUserRealm(org.mortbay.jetty.security.HashUserRealm)

Aggregations

SecurityHandler (org.mortbay.jetty.security.SecurityHandler)4 Constraint (org.mortbay.jetty.security.Constraint)3 ConstraintMapping (org.mortbay.jetty.security.ConstraintMapping)3 HashUserRealm (org.mortbay.jetty.security.HashUserRealm)3 WebAppContext (org.mortbay.jetty.webapp.WebAppContext)2 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 Connector (org.mortbay.jetty.Connector)1 Handler (org.mortbay.jetty.Handler)1 Server (org.mortbay.jetty.Server)1 ContextHandler (org.mortbay.jetty.handler.ContextHandler)1 DefaultHandler (org.mortbay.jetty.handler.DefaultHandler)1 SelectChannelConnector (org.mortbay.jetty.nio.SelectChannelConnector)1 BasicAuthenticator (org.mortbay.jetty.security.BasicAuthenticator)1 SslSocketConnector (org.mortbay.jetty.security.SslSocketConnector)1 WebXmlConfiguration (org.mortbay.jetty.webapp.WebXmlConfiguration)1