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

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

Introduction

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

Prototype

public void addFilter(String name, Filter filter, boolean init) 

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);
    }//from   w w  w . ja  v  a  2 s.  c  om

    //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:org.tolven.shiro.web.filter.mgt.TolvenFilterChainResolver.java

License:Open Source License

@Override
public FilterChain getChain(ServletRequest servletRequest, ServletResponse servletResponse,
        FilterChain originalChain) {//from w  w  w .ja v  a  2s  .  c o 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);
    }
}