Search in sources :

Example 61 with Filter

use of javax.servlet.Filter in project springBoot-learn-demo by nbfujx.

the class ShiroConfig method shiroFilterFactoryBean.

/**
 * ShiroFilterFactoryBean,是个factorybean,为了生成ShiroFilter。
 * 它主要保持了三项数据,securityManager,filters,filterChainDefinitionManager。
 */
@Bean(name = "shiroFilter")
public ShiroFilterFactoryBean shiroFilterFactoryBean() {
    ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
    shiroFilterFactoryBean.setSecurityManager(securityManager());
    Map<String, Filter> filters = new LinkedHashMap<String, Filter>();
    shiroFilterFactoryBean.setFilters(filters);
    Map<String, String> filterChainDefinitionManager = new LinkedHashMap<String, String>();
    filterChainDefinitionManager.put("/login", "anon");
    filterChainDefinitionManager.put("/logout", "anon");
    // "authc,perms[sysUser:*]");
    filterChainDefinitionManager.put("/test/*", "authc,perms");
    // filterChainDefinitionManager.put("/sysMenu/*", "authc,perms");//"authc,perms[sysUser:*]");
    // filterChainDefinitionManager.put("/*", "anon");
    shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionManager);
    shiroFilterFactoryBean.setLoginUrl("/notAuthc");
    // shiroFilterFactoryBean.setUnauthorizedUrl("/notAuthz");
    return shiroFilterFactoryBean;
}
Also used : ShiroFilterFactoryBean(org.apache.shiro.spring.web.ShiroFilterFactoryBean) Filter(javax.servlet.Filter) LinkedHashMap(java.util.LinkedHashMap) ShiroFilterFactoryBean(org.apache.shiro.spring.web.ShiroFilterFactoryBean) Bean(org.springframework.context.annotation.Bean)

Example 62 with Filter

use of javax.servlet.Filter 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 63 with Filter

use of javax.servlet.Filter in project shiro by apache.

the class DefaultFilterChainManager method addToChain.

public void addToChain(String chainName, String filterName, String chainSpecificFilterConfig) {
    if (!StringUtils.hasText(chainName)) {
        throw new IllegalArgumentException("chainName cannot be null or empty.");
    }
    Filter filter = getFilter(filterName);
    if (filter == null) {
        throw new IllegalArgumentException("There is no filter with name '" + filterName + "' to apply to chain [" + chainName + "] in the pool of available Filters.  Ensure a " + "filter with that name/path has first been registered with the addFilter method(s).");
    }
    applyChainConfig(chainName, filter, chainSpecificFilterConfig);
    NamedFilterList chain = ensureChain(chainName);
    chain.add(filter);
}
Also used : Filter(javax.servlet.Filter)

Example 64 with Filter

use of javax.servlet.Filter in project shiro by apache.

the class IniFilterChainResolverFactory method registerFilters.

protected void registerFilters(Map<String, Filter> filters, FilterChainManager manager) {
    if (!CollectionUtils.isEmpty(filters)) {
        // only call filter.init if there is a FilterConfig available
        boolean init = getFilterConfig() != null;
        for (Map.Entry<String, Filter> entry : filters.entrySet()) {
            String name = entry.getKey();
            Filter filter = entry.getValue();
            manager.addFilter(name, filter, init);
        }
    }
}
Also used : Filter(javax.servlet.Filter) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 65 with Filter

use of javax.servlet.Filter in project shiro by apache.

the class WebIniSecurityManagerFactory method createDefaults.

@SuppressWarnings({ "unchecked" })
@Override
protected Map<String, ?> createDefaults(Ini ini, Ini.Section mainSection) {
    Map defaults = super.createDefaults(ini, mainSection);
    // add the default filters:
    Map<String, Filter> defaultFilters = DefaultFilter.createInstanceMap(null);
    defaults.putAll(defaultFilters);
    return defaults;
}
Also used : DefaultFilter(org.apache.shiro.web.filter.mgt.DefaultFilter) Filter(javax.servlet.Filter) Map(java.util.Map)

Aggregations

Filter (javax.servlet.Filter)185 FilterChain (javax.servlet.FilterChain)67 Test (org.junit.Test)57 HttpServletRequest (javax.servlet.http.HttpServletRequest)53 HttpServletResponse (javax.servlet.http.HttpServletResponse)44 ServletRequest (javax.servlet.ServletRequest)43 ServletResponse (javax.servlet.ServletResponse)43 ServletException (javax.servlet.ServletException)28 FilterConfig (javax.servlet.FilterConfig)25 IOException (java.io.IOException)23 ServletContext (javax.servlet.ServletContext)20 Injector (com.google.inject.Injector)17 FilterHolder (org.eclipse.jetty.servlet.FilterHolder)16 OncePerRequestFilter (org.springframework.web.filter.OncePerRequestFilter)13 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)12 Map (java.util.Map)9 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)9 ArrayList (java.util.ArrayList)8 Hashtable (java.util.Hashtable)8 DispatcherType (javax.servlet.DispatcherType)8