protected WebEnvironment createEnvironment(ServletContext sc) {

        WebEnvironment webEnvironment = determineWebEnvironment(sc);
        if (!MutableWebEnvironment.class.isInstance(webEnvironment)) {
            throw new ConfigurationException("Custom WebEnvironment class [" + webEnvironment.getClass().getName()
                    + "] is not of required type [" + MutableWebEnvironment.class.getName() + "]");
        }

        String configLocations = sc.getInitParameter(CONFIG_LOCATIONS_PARAM);
        boolean configSpecified = StringUtils.hasText(configLocations);

        if (configSpecified && !(ResourceConfigurable.class.isInstance(webEnvironment))) {
            String msg = "WebEnvironment class [" + webEnvironment.getClass().getName() + "] does not implement the "
                    + ResourceConfigurable.class.getName() + "interface.  This is required to accept any "
                    + "configured " + CONFIG_LOCATIONS_PARAM + "value(s).";
            throw new ConfigurationException(msg);
        }

        MutableWebEnvironment environment = (MutableWebEnvironment) webEnvironment;

        environment.setServletContext(sc);

        if (configSpecified && (environment instanceof ResourceConfigurable)) {
            ((ResourceConfigurable) environment).setConfigLocations(configLocations);
        }

        customizeEnvironment(environment);

        LifecycleUtils.init(environment);

        return environment;
    }
Instantiates a {@link WebEnvironment} based on the specified ServletContext.
<p/>
This implementation {@link #determineWebEnvironmentClass(javax.servlet.ServletContext) determines} a
{@link WebEnvironment} implementation class to use.  That class is instantiated, configured, and returned.
<p/>
This allows custom {@code WebEnvironment} implementations to be specified via a ServletContext init-param if
desired.  If not specified, the default {@link IniWebEnvironment} implementation will be used.

@param sc current servlet context
@return the constructed Shiro WebEnvironment instance
@see MutableWebEnvironment
@see ResourceConfigurable
web/src/main/java/org/apache/shiro/web/env/EnvironmentLoader.java