Search in sources :

Example 1 with DefaultFilterChainManager

use of org.apache.shiro.web.filter.mgt.DefaultFilterChainManager in project dq-easy-cloud by dq-open-cloud.

the class EcAuthorityManager method doUpdateFilterChains.

/**
 * <p>
 * 执行更新过滤器链
 * </p>
 *
 * @param filterChainDefinitionMap
 * @return void
 * @author daiqi
 * @date 2018/6/27 13:00
 */
private void doUpdateFilterChains(Map<String, String> filterChainDefinitionMap) {
    AbstractShiroFilter shiroFilter;
    try {
        shiroFilter = (AbstractShiroFilter) shiroFilterFactoryBean.getObject();
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage(), e);
    }
    PathMatchingFilterChainResolver filterChainResolver = (PathMatchingFilterChainResolver) shiroFilter.getFilterChainResolver();
    DefaultFilterChainManager manager = (DefaultFilterChainManager) filterChainResolver.getFilterChainManager();
    // 清空老的过滤器链
    manager.getFilterChains().clear();
    shiroFilterFactoryBean.getFilterChainDefinitionMap().clear();
    shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
    // 重新构建生成
    Map<String, String> chains = shiroFilterFactoryBean.getFilterChainDefinitionMap();
    for (Map.Entry<String, String> entry : chains.entrySet()) {
        String url = entry.getKey();
        String chainDefinition = entry.getValue().trim().replace(" ", "");
        manager.createChain(url, chainDefinition);
    }
}
Also used : DefaultFilterChainManager(org.apache.shiro.web.filter.mgt.DefaultFilterChainManager) AbstractShiroFilter(org.apache.shiro.web.servlet.AbstractShiroFilter) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) PathMatchingFilterChainResolver(org.apache.shiro.web.filter.mgt.PathMatchingFilterChainResolver)

Example 2 with DefaultFilterChainManager

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

the class ShiroFilterFactoryBean method createFilterChainManager.

protected FilterChainManager createFilterChainManager() {
    DefaultFilterChainManager manager = new DefaultFilterChainManager();
    Map<String, Filter> defaultFilters = manager.getFilters();
    // apply global settings if necessary:
    for (Filter filter : defaultFilters.values()) {
        applyGlobalPropertiesIfNecessary(filter);
    }
    // Apply the acquired and/or configured filters:
    Map<String, Filter> filters = getFilters();
    if (!CollectionUtils.isEmpty(filters)) {
        for (Map.Entry<String, Filter> entry : filters.entrySet()) {
            String name = entry.getKey();
            Filter filter = entry.getValue();
            applyGlobalPropertiesIfNecessary(filter);
            if (filter instanceof Nameable) {
                ((Nameable) filter).setName(name);
            }
            // 'init' argument is false, since Spring-configured filters should be initialized
            // in Spring (i.e. 'init-method=blah') or implement InitializingBean:
            manager.addFilter(name, filter, false);
        }
    }
    // build up the chains:
    Map<String, String> chains = getFilterChainDefinitionMap();
    if (!CollectionUtils.isEmpty(chains)) {
        for (Map.Entry<String, String> entry : chains.entrySet()) {
            String url = entry.getKey();
            String chainDefinition = entry.getValue();
            manager.createChain(url, chainDefinition);
        }
    }
    return manager;
}
Also used : Nameable(org.apache.shiro.util.Nameable) DefaultFilterChainManager(org.apache.shiro.web.filter.mgt.DefaultFilterChainManager) AuthenticationFilter(org.apache.shiro.web.filter.authc.AuthenticationFilter) AuthorizationFilter(org.apache.shiro.web.filter.authz.AuthorizationFilter) AccessControlFilter(org.apache.shiro.web.filter.AccessControlFilter) AbstractShiroFilter(org.apache.shiro.web.servlet.AbstractShiroFilter) Filter(javax.servlet.Filter) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 3 with DefaultFilterChainManager

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

the class ShiroFilterFactoryBeanTest method testFilterDefinition.

@Test
public void testFilterDefinition() {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("org/apache/shiro/spring/web/ShiroFilterFactoryBeanTest.xml");
    AbstractShiroFilter shiroFilter = (AbstractShiroFilter) context.getBean("shiroFilter");
    PathMatchingFilterChainResolver resolver = (PathMatchingFilterChainResolver) shiroFilter.getFilterChainResolver();
    DefaultFilterChainManager fcManager = (DefaultFilterChainManager) resolver.getFilterChainManager();
    NamedFilterList chain = fcManager.getChain("/test");
    assertNotNull(chain);
    assertEquals(chain.size(), 2);
    Filter[] filters = new Filter[chain.size()];
    filters = chain.toArray(filters);
    assertTrue(filters[0] instanceof DummyFilter);
    assertTrue(filters[1] instanceof FormAuthenticationFilter);
}
Also used : DefaultFilterChainManager(org.apache.shiro.web.filter.mgt.DefaultFilterChainManager) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) FormAuthenticationFilter(org.apache.shiro.web.filter.authc.FormAuthenticationFilter) AbstractShiroFilter(org.apache.shiro.web.servlet.AbstractShiroFilter) FormAuthenticationFilter(org.apache.shiro.web.filter.authc.FormAuthenticationFilter) AbstractShiroFilter(org.apache.shiro.web.servlet.AbstractShiroFilter) NamedFilterList(org.apache.shiro.web.filter.mgt.NamedFilterList) PathMatchingFilterChainResolver(org.apache.shiro.web.filter.mgt.PathMatchingFilterChainResolver) Test(org.junit.Test)

Aggregations

DefaultFilterChainManager (org.apache.shiro.web.filter.mgt.DefaultFilterChainManager)3 AbstractShiroFilter (org.apache.shiro.web.servlet.AbstractShiroFilter)3 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 PathMatchingFilterChainResolver (org.apache.shiro.web.filter.mgt.PathMatchingFilterChainResolver)2 Filter (javax.servlet.Filter)1 Nameable (org.apache.shiro.util.Nameable)1 AccessControlFilter (org.apache.shiro.web.filter.AccessControlFilter)1 AuthenticationFilter (org.apache.shiro.web.filter.authc.AuthenticationFilter)1 FormAuthenticationFilter (org.apache.shiro.web.filter.authc.FormAuthenticationFilter)1 AuthorizationFilter (org.apache.shiro.web.filter.authz.AuthorizationFilter)1 NamedFilterList (org.apache.shiro.web.filter.mgt.NamedFilterList)1 Test (org.junit.Test)1 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)1