Search in sources :

Example 1 with CorsFilter

use of org.apache.catalina.filters.CorsFilter in project che by eclipse.

the class CheCorsFilter method init.

@Override
public void init(FilterConfig filterConfig) throws ServletException {
    corsFilter = new CorsFilter();
    corsFilter.init(new CodenvyCorsFilterConfig());
}
Also used : CorsFilter(org.apache.catalina.filters.CorsFilter)

Example 2 with CorsFilter

use of org.apache.catalina.filters.CorsFilter in project tomcat by apache.

the class TestAuthenticatorBaseCorsPreflight method test.

@Test
public void test() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    File appDir = new File("test/webapp");
    Context ctx = tomcat.addContext("", appDir.getAbsolutePath());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMappingDecoded("/", "default");
    LoginConfig loginConfig = new LoginConfig();
    loginConfig.setAuthMethod("BASIC");
    ctx.setLoginConfig(loginConfig);
    BasicAuthenticator basicAuth = new BasicAuthenticator();
    basicAuth.setAllowCorsPreflight(allowCorsPreflight.toString());
    ctx.getPipeline().addValve(basicAuth);
    Realm realm = new NullRealm();
    ctx.setRealm(realm);
    SecurityCollection securityCollection = new SecurityCollection();
    securityCollection.addPattern("/*");
    SecurityConstraint constraint = new SecurityConstraint();
    constraint.setAuthConstraint(true);
    constraint.addCollection(securityCollection);
    ctx.addConstraint(constraint);
    // For code coverage
    FilterDef otherFilter = new FilterDef();
    otherFilter.setFilterName("other");
    otherFilter.setFilterClass(AddDefaultCharsetFilter.class.getName());
    FilterMap otherMap = new FilterMap();
    otherMap.setFilterName("other");
    otherMap.addURLPatternDecoded("/other");
    ctx.addFilterDef(otherFilter);
    ctx.addFilterMap(otherMap);
    FilterDef corsFilter = new FilterDef();
    corsFilter.setFilterName("cors");
    corsFilter.setFilterClass(CorsFilter.class.getName());
    corsFilter.addInitParameter(CorsFilter.PARAM_CORS_ALLOWED_ORIGINS, ALLOWED_ORIGIN);
    corsFilter.addInitParameter(CorsFilter.PARAM_CORS_ALLOWED_METHODS, ALLOWED_METHOD);
    FilterMap corsFilterMap = new FilterMap();
    corsFilterMap.setFilterName("cors");
    corsFilterMap.addURLPatternDecoded(filterMapping);
    ctx.addFilterDef(corsFilter);
    ctx.addFilterMap(corsFilterMap);
    tomcat.start();
    Map<String, List<String>> reqHead = new HashMap<>();
    if (origin != null) {
        List<String> values = new ArrayList<>();
        if (SAME_ORIGIN.equals(origin)) {
            values.add(origin + ":" + getPort());
        } else {
            values.add(origin);
        }
        reqHead.put(CorsFilter.REQUEST_HEADER_ORIGIN, values);
    }
    if (accessControl != null) {
        List<String> values = new ArrayList<>();
        values.add(accessControl);
        reqHead.put(CorsFilter.REQUEST_HEADER_ACCESS_CONTROL_REQUEST_METHOD, values);
    }
    ByteChunk out = new ByteChunk();
    int rc = methodUrl("http://localhost:" + getPort() + "/target", out, 300000, reqHead, null, method, false);
    if (allow) {
        Assert.assertEquals(200, rc);
    } else {
        Assert.assertEquals(403, rc);
    }
}
Also used : Context(org.apache.catalina.Context) CorsFilter(org.apache.catalina.filters.CorsFilter) Tomcat(org.apache.catalina.startup.Tomcat) FilterDef(org.apache.tomcat.util.descriptor.web.FilterDef) HashMap(java.util.HashMap) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) ArrayList(java.util.ArrayList) NullRealm(org.apache.catalina.realm.NullRealm) AddDefaultCharsetFilter(org.apache.catalina.filters.AddDefaultCharsetFilter) FilterMap(org.apache.tomcat.util.descriptor.web.FilterMap) SecurityConstraint(org.apache.tomcat.util.descriptor.web.SecurityConstraint) SecurityConstraint(org.apache.tomcat.util.descriptor.web.SecurityConstraint) LoginConfig(org.apache.tomcat.util.descriptor.web.LoginConfig) ArrayList(java.util.ArrayList) List(java.util.List) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) File(java.io.File) NullRealm(org.apache.catalina.realm.NullRealm) Realm(org.apache.catalina.Realm) SecurityCollection(org.apache.tomcat.util.descriptor.web.SecurityCollection) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Aggregations

CorsFilter (org.apache.catalina.filters.CorsFilter)2 File (java.io.File)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Context (org.apache.catalina.Context)1 Realm (org.apache.catalina.Realm)1 AddDefaultCharsetFilter (org.apache.catalina.filters.AddDefaultCharsetFilter)1 NullRealm (org.apache.catalina.realm.NullRealm)1 DefaultServlet (org.apache.catalina.servlets.DefaultServlet)1 Tomcat (org.apache.catalina.startup.Tomcat)1 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)1 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)1 FilterDef (org.apache.tomcat.util.descriptor.web.FilterDef)1 FilterMap (org.apache.tomcat.util.descriptor.web.FilterMap)1 LoginConfig (org.apache.tomcat.util.descriptor.web.LoginConfig)1 SecurityCollection (org.apache.tomcat.util.descriptor.web.SecurityCollection)1 SecurityConstraint (org.apache.tomcat.util.descriptor.web.SecurityConstraint)1 Test (org.junit.Test)1