Search in sources :

Example 1 with Constraint

use of org.mortbay.jetty.security.Constraint 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 Constraint

use of org.mortbay.jetty.security.Constraint 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 3 with Constraint

use of org.mortbay.jetty.security.Constraint 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

Constraint (org.mortbay.jetty.security.Constraint)3 ConstraintMapping (org.mortbay.jetty.security.ConstraintMapping)3 HashUserRealm (org.mortbay.jetty.security.HashUserRealm)3 SecurityHandler (org.mortbay.jetty.security.SecurityHandler)3 ArrayList (java.util.ArrayList)1 Connector (org.mortbay.jetty.Connector)1 Handler (org.mortbay.jetty.Handler)1 Server (org.mortbay.jetty.Server)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 WebAppContext (org.mortbay.jetty.webapp.WebAppContext)1