Search in sources :

Example 1 with DefaultFilter

use of org.apache.shiro.web.filter.mgt.DefaultFilter in project shiro by apache.

the class WebIniSecurityManagerFactoryTest method testDefaultFiltersPresent.

/**
 * Test that ensures the WebIniSecurityManagerFactory will automatically add the default
 * filters to the pool of beans before the INI configuration is interpreted.
 */
@Test
public void testDefaultFiltersPresent() {
    Ini ini = new Ini();
    // just a normal configuration line in the MAIN section for any of the default filtes should work
    // out of the box.  So, create the main section and just config one of them:
    Ini.Section section = ini.addSection(IniSecurityManagerFactory.MAIN_SECTION_NAME);
    section.put("authc.loginUrl", "/login.jsp");
    WebIniSecurityManagerFactory factory = new WebIniSecurityManagerFactory(ini);
    org.apache.shiro.mgt.SecurityManager sm = factory.getInstance();
    assertNotNull(sm);
    assertTrue(sm instanceof DefaultWebSecurityManager);
    // now assert that all of the default filters exist:
    Map<String, ?> beans = factory.getBeans();
    for (DefaultFilter defaultFilter : DefaultFilter.values()) {
        Filter filter = (Filter) beans.get(defaultFilter.name());
        assertNotNull(filter);
        assertTrue(defaultFilter.getFilterClass().isAssignableFrom(filter.getClass()));
    }
}
Also used : DefaultFilter(org.apache.shiro.web.filter.mgt.DefaultFilter) Ini(org.apache.shiro.config.Ini) DefaultFilter(org.apache.shiro.web.filter.mgt.DefaultFilter) Filter(javax.servlet.Filter) DefaultWebSecurityManager(org.apache.shiro.web.mgt.DefaultWebSecurityManager) Test(org.junit.Test)

Example 2 with DefaultFilter

use of org.apache.shiro.web.filter.mgt.DefaultFilter in project shiro by apache.

the class DefaultFiltersTest method checkDefaultFilters.

@Test
public void checkDefaultFilters() throws Exception {
    EnumSet<DefaultFilter> defaultFilters = EnumSet.allOf(DefaultFilter.class);
    for (Field field : ShiroWebModule.class.getFields()) {
        if (Modifier.isStatic(field.getModifiers()) && Key.class.isAssignableFrom(field.getType())) {
            Class<? extends Filter> filterType = ((Key) field.get(null)).getTypeLiteral().getRawType();
            boolean found = false;
            for (DefaultFilter filter : defaultFilters) {
                if (filterType.equals(filter.getFilterClass())) {
                    found = true;
                    defaultFilters.remove(filter);
                    break;
                }
            }
            if (!found) {
                fail("Guice ShiroWebModule containts a default filter that Shiro proper does not. (" + filterType.getName() + ")");
            }
        }
    }
    if (!defaultFilters.isEmpty()) {
        fail("Guice ShiroWebModule is missing one or more filters. " + defaultFilters);
    }
}
Also used : Field(java.lang.reflect.Field) DefaultFilter(org.apache.shiro.web.filter.mgt.DefaultFilter) Key(com.google.inject.Key) Test(org.junit.Test)

Aggregations

DefaultFilter (org.apache.shiro.web.filter.mgt.DefaultFilter)2 Test (org.junit.Test)2 Key (com.google.inject.Key)1 Field (java.lang.reflect.Field)1 Filter (javax.servlet.Filter)1 Ini (org.apache.shiro.config.Ini)1 DefaultWebSecurityManager (org.apache.shiro.web.mgt.DefaultWebSecurityManager)1