Search in sources :

Example 6 with ServletContainerInitializer

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);
}
Also used : ServletContext(jakarta.servlet.ServletContext) Context(org.apache.catalina.Context) ServletContainerInitializer(jakarta.servlet.ServletContainerInitializer) Tomcat(org.apache.catalina.startup.Tomcat) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) HttpMethodConstraint(jakarta.servlet.annotation.HttpMethodConstraint) HttpConstraint(jakarta.servlet.annotation.HttpConstraint) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 7 with ServletContainerInitializer

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);
    }
}
Also used : Context(org.apache.catalina.Context) ServletContext(jakarta.servlet.ServletContext) ServletContainerInitializer(jakarta.servlet.ServletContainerInitializer) Tomcat(org.apache.catalina.startup.Tomcat) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) HttpServlet(jakarta.servlet.http.HttpServlet) Servlet(jakarta.servlet.Servlet) HttpConstraint(jakarta.servlet.annotation.HttpConstraint) HttpMethodConstraint(jakarta.servlet.annotation.HttpMethodConstraint)

Example 8 with ServletContainerInitializer

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);
    }
}
Also used : ServletContext(jakarta.servlet.ServletContext) Context(org.apache.catalina.Context) ServletContainerInitializer(jakarta.servlet.ServletContainerInitializer) Tomcat(org.apache.catalina.startup.Tomcat) TesterMapRealm(org.apache.catalina.startup.TesterMapRealm) BasicAuthenticator(org.apache.catalina.authenticator.BasicAuthenticator) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) LoginConfig(org.apache.tomcat.util.descriptor.web.LoginConfig) HttpMethodConstraint(jakarta.servlet.annotation.HttpMethodConstraint) HttpConstraint(jakarta.servlet.annotation.HttpConstraint)

Example 9 with ServletContainerInitializer

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);
        }
    }
}
Also used : Context(org.apache.catalina.Context) ServletContext(jakarta.servlet.ServletContext) ServletContainerInitializer(jakarta.servlet.ServletContainerInitializer) Tomcat(org.apache.catalina.startup.Tomcat) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Aggregations

ServletContainerInitializer (jakarta.servlet.ServletContainerInitializer)9 ServletContext (jakarta.servlet.ServletContext)6 Context (org.apache.catalina.Context)5 Tomcat (org.apache.catalina.startup.Tomcat)5 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)5 HttpConstraint (jakarta.servlet.annotation.HttpConstraint)4 HttpMethodConstraint (jakarta.servlet.annotation.HttpMethodConstraint)4 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 LinkedHashMap (java.util.LinkedHashMap)3 Map (java.util.Map)3 Set (java.util.Set)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)3 FilterMap (org.apache.tomcat.util.descriptor.web.FilterMap)3 Test (org.junit.Test)3 IOException (java.io.IOException)2 MalformedURLException (java.net.MalformedURLException)2 LinkedHashSet (java.util.LinkedHashSet)2 BasicAuthenticator (org.apache.catalina.authenticator.BasicAuthenticator)2