Search in sources :

Example 1 with PathMatchingFilterChainResolver

use of org.apache.shiro.web.filter.mgt.PathMatchingFilterChainResolver 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 PathMatchingFilterChainResolver

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

the class ShiroFilterFactoryBean method createInstance.

/**
 * This implementation:
 * <ol>
 * <li>Ensures the required {@link #setSecurityManager(org.apache.shiro.mgt.SecurityManager) securityManager}
 * property has been set</li>
 * <li>{@link #createFilterChainManager() Creates} a {@link FilterChainManager} instance that reflects the
 * configured {@link #setFilters(java.util.Map) filters} and
 * {@link #setFilterChainDefinitionMap(java.util.Map) filter chain definitions}</li>
 * <li>Wraps the FilterChainManager with a suitable
 * {@link org.apache.shiro.web.filter.mgt.FilterChainResolver FilterChainResolver} since the Shiro Filter
 * implementations do not know of {@code FilterChainManager}s</li>
 * <li>Sets both the {@code SecurityManager} and {@code FilterChainResolver} instances on a new Shiro Filter
 * instance and returns that filter instance.</li>
 * </ol>
 *
 * @return a new Shiro Filter reflecting any configured filters and filter chain definitions.
 * @throws Exception if there is a problem creating the AbstractShiroFilter instance.
 */
protected AbstractShiroFilter createInstance() throws Exception {
    log.debug("Creating Shiro Filter instance.");
    SecurityManager securityManager = getSecurityManager();
    if (securityManager == null) {
        String msg = "SecurityManager property must be set.";
        throw new BeanInitializationException(msg);
    }
    if (!(securityManager instanceof WebSecurityManager)) {
        String msg = "The security manager does not implement the WebSecurityManager interface.";
        throw new BeanInitializationException(msg);
    }
    FilterChainManager manager = createFilterChainManager();
    // Expose the constructed FilterChainManager by first wrapping it in a
    // FilterChainResolver implementation. The AbstractShiroFilter implementations
    // do not know about FilterChainManagers - only resolvers:
    PathMatchingFilterChainResolver chainResolver = new PathMatchingFilterChainResolver();
    chainResolver.setFilterChainManager(manager);
    // injection of the SecurityManager and FilterChainResolver:
    return new SpringShiroFilter((WebSecurityManager) securityManager, chainResolver);
}
Also used : BeanInitializationException(org.springframework.beans.factory.BeanInitializationException) WebSecurityManager(org.apache.shiro.web.mgt.WebSecurityManager) WebSecurityManager(org.apache.shiro.web.mgt.WebSecurityManager) SecurityManager(org.apache.shiro.mgt.SecurityManager) DefaultFilterChainManager(org.apache.shiro.web.filter.mgt.DefaultFilterChainManager) FilterChainManager(org.apache.shiro.web.filter.mgt.FilterChainManager) PathMatchingFilterChainResolver(org.apache.shiro.web.filter.mgt.PathMatchingFilterChainResolver)

Example 3 with PathMatchingFilterChainResolver

use of org.apache.shiro.web.filter.mgt.PathMatchingFilterChainResolver 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)

Example 4 with PathMatchingFilterChainResolver

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

the class IniFilterChainResolverFactory method createInstance.

protected FilterChainResolver createInstance(Ini ini) {
    FilterChainResolver filterChainResolver = createDefaultInstance();
    if (filterChainResolver instanceof PathMatchingFilterChainResolver) {
        PathMatchingFilterChainResolver resolver = (PathMatchingFilterChainResolver) filterChainResolver;
        FilterChainManager manager = resolver.getFilterChainManager();
        buildChains(manager, ini);
    }
    return filterChainResolver;
}
Also used : FilterChainResolver(org.apache.shiro.web.filter.mgt.FilterChainResolver) PathMatchingFilterChainResolver(org.apache.shiro.web.filter.mgt.PathMatchingFilterChainResolver) FilterChainManager(org.apache.shiro.web.filter.mgt.FilterChainManager) PathMatchingFilterChainResolver(org.apache.shiro.web.filter.mgt.PathMatchingFilterChainResolver)

Aggregations

PathMatchingFilterChainResolver (org.apache.shiro.web.filter.mgt.PathMatchingFilterChainResolver)4 DefaultFilterChainManager (org.apache.shiro.web.filter.mgt.DefaultFilterChainManager)3 FilterChainManager (org.apache.shiro.web.filter.mgt.FilterChainManager)2 AbstractShiroFilter (org.apache.shiro.web.servlet.AbstractShiroFilter)2 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 SecurityManager (org.apache.shiro.mgt.SecurityManager)1 FormAuthenticationFilter (org.apache.shiro.web.filter.authc.FormAuthenticationFilter)1 FilterChainResolver (org.apache.shiro.web.filter.mgt.FilterChainResolver)1 NamedFilterList (org.apache.shiro.web.filter.mgt.NamedFilterList)1 WebSecurityManager (org.apache.shiro.web.mgt.WebSecurityManager)1 Test (org.junit.Test)1 BeanInitializationException (org.springframework.beans.factory.BeanInitializationException)1 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)1