use of jakarta.servlet.ServletContainerInitializer in project tomcat by apache.
the class TestStandardContext method testUncoveredMethods.
@Test
public void testUncoveredMethods() throws Exception {
// Setup Tomcat instance
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("/test", null);
ctx.setDenyUncoveredHttpMethods(true);
ServletContainerInitializer sci = new SCI();
ctx.addServletContainerInitializer(sci, null);
tomcat.start();
ByteChunk bc = new ByteChunk();
int rc;
rc = getUrl("http://localhost:" + getPort() + "/test/foo", bc, false);
Assert.assertEquals(403, rc);
}
use of jakarta.servlet.ServletContainerInitializer in project tomcat by apache.
the class TestStandardWrapper method doTestSecurityAnnotationsAddServlet.
private void doTestSecurityAnnotationsAddServlet(boolean useCreateServlet) throws Exception {
// Setup Tomcat instance
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
Servlet s = new DenyAllServlet();
ServletContainerInitializer sci = new SCI(s, useCreateServlet);
ctx.addServletContainerInitializer(sci, null);
tomcat.start();
ByteChunk bc = new ByteChunk();
int rc;
rc = getUrl("http://localhost:" + getPort() + "/", bc, null, null);
if (useCreateServlet) {
Assert.assertTrue(bc.getLength() > 0);
Assert.assertEquals(403, rc);
} else {
Assert.assertEquals("OK", bc.toString());
Assert.assertEquals(200, rc);
}
}
use of jakarta.servlet.ServletContainerInitializer in project tomcat by apache.
the class TestStandardContext method doTestDenyUncoveredHttpMethodsSCI.
private void doTestDenyUncoveredHttpMethodsSCI(boolean enableDeny) throws Exception {
// Test that denying uncovered HTTP methods when adding servlet security
// constraints programmatically does work.
// Set up a container
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
ctx.setDenyUncoveredHttpMethods(enableDeny);
// Setup realm
TesterMapRealm realm = new TesterMapRealm();
realm.addUser("tomcat", "tomcat");
realm.addUserRole("tomcat", "tomcat");
ctx.setRealm(realm);
// Configure app for BASIC auth
LoginConfig lc = new LoginConfig();
lc.setAuthMethod("BASIC");
ctx.setLoginConfig(lc);
ctx.getPipeline().addValve(new BasicAuthenticator());
// Add ServletContainerInitializer
ServletContainerInitializer sci = new DenyUncoveredHttpMethodsSCI();
ctx.addServletContainerInitializer(sci, null);
// Start the context
tomcat.start();
// Request the first servlet
ByteChunk bc = new ByteChunk();
int rc = getUrl("http://localhost:" + getPort() + "/test", bc, null);
// Check for a 401
if (enableDeny) {
// Should be default error page
Assert.assertTrue(bc.toString().contains("403"));
Assert.assertEquals(403, rc);
} else {
Assert.assertEquals("OK", bc.toString());
Assert.assertEquals(200, rc);
}
}
use of jakarta.servlet.ServletContainerInitializer in project tomcat by apache.
the class TestServletSecurityMappings method doTestSecurityAnnotationsAddServlet.
@Test
public void doTestSecurityAnnotationsAddServlet() throws Exception {
// Setup Tomcat instance
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("/test", null);
ctx.setMapperContextRootRedirectEnabled(redirectContextRoot);
ServletContainerInitializer sci = new SCI(secureRoot, secureDefault, secureFoo);
ctx.addServletContainerInitializer(sci, null);
tomcat.start();
ByteChunk bc = new ByteChunk();
int rc;
// Foo
rc = getUrl("http://localhost:" + getPort() + "/test/foo", bc, false);
if (secureFoo || secureDefault) {
Assert.assertEquals(403, rc);
} else {
Assert.assertEquals(200, rc);
}
bc.recycle();
// Default
rc = getUrl("http://localhost:" + getPort() + "/test/something", bc, false);
if (secureDefault) {
Assert.assertEquals(403, rc);
} else {
Assert.assertEquals(200, rc);
}
bc.recycle();
// Root
rc = getUrl("http://localhost:" + getPort() + "/test", bc, false);
if (redirectContextRoot) {
Assert.assertEquals(302, rc);
} else {
if (secureRoot || secureDefault) {
Assert.assertEquals(403, rc);
} else {
Assert.assertEquals(200, rc);
}
}
}
Aggregations