Example usage for org.apache.shiro.web.filter.mgt DefaultFilterChainManager createChain

List of usage examples for org.apache.shiro.web.filter.mgt DefaultFilterChainManager createChain

Introduction

In this page you can find the example usage for org.apache.shiro.web.filter.mgt DefaultFilterChainManager createChain.

Prototype

public void createChain(String chainName, String chainDefinition) 

Source Link

Usage

From source file:com.centfor.frame.shiro.FrameShiroFilterFactoryBean.java

License:Apache License

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);
    }//  w w w . j  a va 2  s .c o m

    //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;
}

From source file:com.webarch.common.shiro.dynamic.DynamicPermissionServiceImpl.java

License:Apache License

private void addToChain(DefaultFilterChainManager manager, Map<String, String> definitions) throws Exception {
    if (definitions == null || CollectionUtils.isEmpty(definitions)) {
        return;//from  w ww  . ja va 2 s  . co m
    }
    ///**???
    manager.getFilterChains().remove("/**");
    for (Map.Entry<String, String> entry : definitions.entrySet()) {
        String url = entry.getKey();
        String chainDefinition = entry.getValue().trim().replace(" ", "");
        manager.createChain(url, chainDefinition);
    }
}

From source file:io.bootique.shiro.web.MappedShiroFilterFactory.java

License:Apache License

protected FilterChainResolver createChainResolver(Map<String, Filter> chainFilters) {
    DefaultFilterChainManager chainManager = new DefaultFilterChainManager();

    // load filters
    chainFilters.forEach((name, filter) -> chainManager.addFilter(name, filter));

    if (urls != null) {
        urls.forEach((url, value) -> {
            LOGGER.info("Loading url chain {} -> {}", url, value);
            chainManager.createChain(url, value);
        });//  w w  w  .  j a v  a  2  s. c o  m
    }

    PathMatchingFilterChainResolver resolver = new PathMatchingFilterChainResolver();
    resolver.setFilterChainManager(chainManager);
    return resolver;
}

From source file:org.tolven.shiro.web.filter.mgt.TolvenFilterChainResolver.java

License:Open Source License

@Override
public FilterChain getChain(ServletRequest servletRequest, ServletResponse servletResponse,
        FilterChain originalChain) {/*from   www  . j a va  2s  . co  m*/
    String requestURI = getPathWithinApplication(servletRequest);
    HttpServletRequest request = (HttpServletRequest) servletRequest;
    String urlMethod = request.getMethod();
    if (logger.isDebugEnabled()) {
        logger.debug("requestURI=" + urlMethod + " " + requestURI);
    }
    TolvenAuthorization authz = getAuthBean().getAuthorization(urlMethod, request.getContextPath(), requestURI);
    if (authz == null) {
        throw new RuntimeException(
                "authorization url cannot be found for request: " + urlMethod + " " + requestURI);
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Matched TolvenAuthorization=" + authz);
    }
    String authorizationURI = authz.getUrl();
    if (!StringUtils.hasText(authorizationURI)) {
        throw new RuntimeException(
                "authorization url cannot be null or empty for request: " + urlMethod + " " + requestURI);
    }
    String filterString = authz.getFilters();
    if (filterString == null || filterString.trim().length() == 0) {
        return originalChain;
    } else {
        DefaultFilterChainManager temporaryManager = new DefaultFilterChainManager(getFilterConfig());
        boolean init = getFilterConfig() != null; //only call filter.init if there is a FilterConfig available
        if (logger.isDebugEnabled()) {
            logger.debug("Adding filters to temporary manager");
        }
        Map<String, Filter> filters = null;
        try {
            filters = getFilters(filterString);
        } catch (Exception ex) {
            throw new RuntimeException("Failed to get chain filters for: " + request.getContextPath(), ex);
        }
        for (Entry<String, Filter> entry : filters.entrySet()) {
            temporaryManager.addFilter(entry.getKey(), entry.getValue(), init);
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Added filters to temporary manager");
        }
        try {
            temporaryManager.createChain(authorizationURI, filterString);
        } catch (Exception ex) {
            throw new RuntimeException("Could not create chain: " + authorizationURI + " for filters: "
                    + filterString + " in context: " + getFilterConfig().getServletContext().getContextPath(),
                    ex);
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Created filter chain: " + authorizationURI + " with " + filterString);
        }
        NamedFilterList chain = temporaryManager.getChain(authorizationURI);
        return chain.proxy(originalChain);
    }
}

From source file:org.workin.security.shiro.filter.authz.ShiroAuthorizationFilterChainManager.java

License:Apache License

@Override
public void reloadFilterChainDefinitions() {
    AbstractShiroFilter shiroFilter = null;
    try {/* w  ww. j a  va2 s .co  m*/
        shiroFilter = (AbstractShiroFilter) shiroFilterFactoryBean.getObject();
    } catch (Exception e) {
        logger.error("getShiroFilter from shiroFilterFactoryBean error!", e);
        throw new RuntimeException("get ShiroFilter from shiroFilterFactoryBean error!");
    }

    PathMatchingFilterChainResolver filterChainResolver = (PathMatchingFilterChainResolver) shiroFilter
            .getFilterChainResolver();
    DefaultFilterChainManager manager = (DefaultFilterChainManager) filterChainResolver.getFilterChainManager();

    synchronized (lock) {
        /* ??/???? */
        manager.getFilterChains().clear();
        shiroFilterFactoryBean.getFilterChainDefinitionMap().clear();
        shiroFilterFactoryBean.setFilterChainDefinitions(this.loadFilterChainDefinitions());

        // ??
        Map<String, String> chains = shiroFilterFactoryBean.getFilterChainDefinitionMap();
        for (Map.Entry<String, String> entry : chains.entrySet()) {
            String url = entry.getKey();
            String chainDefinition = StringUtils.trimAll(entry.getValue());
            manager.createChain(url, chainDefinition);
        }
    }
}

From source file:zcu.xutil.misc.ShiroFilterFactory.java

License:Apache License

public AbstractShiroFilter getShiroFilter() {
    DefaultFilterChainManager manager = new DefaultFilterChainManager();
    for (Filter filter : manager.getFilters().values())
        applyGlobalPropertiesIfNecessary(filter);
    for (Map.Entry<String, Filter> entry : filters.entrySet()) {
        applyGlobalPropertiesIfNecessary(entry.getValue());
        manager.addFilter(entry.getKey(), entry.getValue());
    }//www.  ja v a  2s  .c om
    Ini ini = new Ini();
    ini.load(definitions);
    Ini.Section section = ini.getSection(IniFilterChainResolverFactory.URLS);
    if (CollectionUtils.isEmpty(section))
        section = ini.getSection(Ini.DEFAULT_SECTION_NAME);
    for (Map.Entry<String, String> entry : section.entrySet())
        manager.createChain(entry.getKey(), entry.getValue());
    PathMatchingFilterChainResolver chainResolver = new PathMatchingFilterChainResolver();
    chainResolver.setFilterChainManager(manager);
    return new XSFilter((WebSecurityManager) securityManager, chainResolver);
}