use of org.eclipse.jetty.security.ConstraintMapping 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.ConstraintMapping in project camel by apache.
the class JettyTestServer method basicAuth.
private SecurityHandler basicAuth(String username, String password, String realm) {
HashLoginService l = new HashLoginService();
l.putUser(username, Credential.getCredential(password), new String[] { "user" });
l.setName(realm);
Constraint constraint = new Constraint();
constraint.setName(Constraint.__BASIC_AUTH);
constraint.setRoles(new String[] { "user" });
constraint.setAuthenticate(true);
ConstraintMapping cm = new ConstraintMapping();
cm.setConstraint(constraint);
cm.setPathSpec("/*");
ConstraintSecurityHandler csh = new ConstraintSecurityHandler();
csh.setAuthenticator(new BasicAuthenticator());
csh.setRealmName("myrealm");
csh.addConstraintMapping(cm);
csh.setLoginService(l);
return csh;
}
use of org.eclipse.jetty.security.ConstraintMapping in project calcite-avatica by apache.
the class HttpServer method configureCommonAuthentication.
protected ConstraintSecurityHandler configureCommonAuthentication(Server server, ServerConnector connector, AvaticaServerConfiguration config, String constraintName, String[] allowedRoles, Authenticator authenticator, String realm, LoginService loginService) {
Constraint constraint = new Constraint();
constraint.setName(constraintName);
constraint.setRoles(allowedRoles);
// This is telling Jetty to not allow unauthenticated requests through (very important!)
constraint.setAuthenticate(true);
ConstraintMapping cm = new ConstraintMapping();
cm.setConstraint(constraint);
cm.setPathSpec("/*");
ConstraintSecurityHandler sh = new ConstraintSecurityHandler();
sh.setAuthenticator(authenticator);
sh.setLoginService(loginService);
sh.setConstraintMappings(new ConstraintMapping[] { cm });
sh.setRealmName(realm);
return sh;
}
use of org.eclipse.jetty.security.ConstraintMapping in project bnd by bndtools.
the class HttpConnectorTest method startJetty.
private static Server startJetty() throws Exception {
Server server = new Server();
// Create the login service
String REQUIRED_ROLE = "users";
HashLoginService loginSvc = new HashLoginService(REQUIRED_ROLE, USER_ROLE_FILE);
server.addBean(loginSvc);
// Start HTTP and HTTPS connectors
SelectChannelConnector httpConnector = new SelectChannelConnector();
httpConnector.setPort(0);
httpConnector.setHost(LOCALHOST);
server.addConnector(httpConnector);
SslSelectChannelConnector sslConnector = new SslSelectChannelConnector();
sslConnector.setPort(0);
sslConnector.setHost(LOCALHOST);
SslContextFactory contextFactory = sslConnector.getSslContextFactory();
contextFactory.setKeyStorePath(KEYSTORE_PATH);
contextFactory.setKeyStorePassword(KEYSTORE_PASS);
server.addConnector(sslConnector);
// Create the resource handler to serve files
ResourceHandler resourceHandler = new ETaggingResourceHandler();
resourceHandler.setResourceBase(RESOURCE_BASE);
resourceHandler.setDirectoriesListed(true);
// Setup user role constraints
Constraint constraint = new Constraint();
constraint.setName(Constraint.__BASIC_AUTH);
constraint.setRoles(new String[] { REQUIRED_ROLE });
constraint.setAuthenticate(true);
// Map constraints to the secured directory
ConstraintMapping cm = new ConstraintMapping();
cm.setConstraint(constraint);
cm.setPathSpec(SECURED_PATH);
// Setup the constraint handler
ConstraintSecurityHandler securityHandler = new ConstraintSecurityHandler();
securityHandler.setAuthMethod("BASIC");
securityHandler.setHandler(resourceHandler);
securityHandler.setLoginService(loginSvc);
securityHandler.setConstraintMappings(new ConstraintMapping[] { cm });
// Finally!! Start the server
server.setHandler(securityHandler);
server.start();
while (!server.isRunning()) {
Thread.sleep(10);
}
HTTP_PORT = httpConnector.getLocalPort();
HTTPS_PORT = sslConnector.getLocalPort();
assertNotSame(Integer.valueOf(0), Integer.valueOf(HTTP_PORT));
assertNotSame(Integer.valueOf(-1), Integer.valueOf(HTTP_PORT));
assertNotSame(Integer.valueOf(0), Integer.valueOf(HTTPS_PORT));
assertNotSame(Integer.valueOf(-1), Integer.valueOf(HTTPS_PORT));
assertNotSame(Integer.valueOf(HTTP_PORT), Integer.valueOf(HTTPS_PORT));
return server;
}
use of org.eclipse.jetty.security.ConstraintMapping in project camel by apache.
the class HttpBasicAuthComponentConfiguredTest method getSecurityHandler.
private SecurityHandler getSecurityHandler() throws IOException {
Constraint constraint = new Constraint(Constraint.__BASIC_AUTH, "user");
constraint.setAuthenticate(true);
ConstraintMapping cm = new ConstraintMapping();
cm.setPathSpec("/*");
cm.setConstraint(constraint);
ConstraintSecurityHandler sh = new ConstraintSecurityHandler();
sh.setAuthenticator(new BasicAuthenticator());
sh.setConstraintMappings(Arrays.asList(new ConstraintMapping[] { cm }));
HashLoginService loginService = new HashLoginService("MyRealm", "src/test/resources/myRealm.properties");
sh.setLoginService(loginService);
sh.setConstraintMappings(Arrays.asList(new ConstraintMapping[] { cm }));
return sh;
}
Aggregations