001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.shiro.web.config;
020
021import org.apache.shiro.config.Ini;
022import org.apache.shiro.config.IniSecurityManagerFactory;
023import org.apache.shiro.mgt.SecurityManager;
024import org.apache.shiro.web.filter.mgt.DefaultFilter;
025import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
026
027import javax.servlet.Filter;
028import java.util.Map;
029
030/**
031 * Differs from the parent class only in the {@link #createDefaultInstance()} method, to
032 * ensure a web-capable {@code SecurityManager} instance is created by default.
033 *
034 * @since 1.0
035 */
036public class WebIniSecurityManagerFactory extends IniSecurityManagerFactory {
037
038    /**
039     * Creates a new {@code WebIniSecurityManagerFactory} instance which will construct web-capable
040     * {@code SecurityManager} instances.
041     */
042    public WebIniSecurityManagerFactory() {
043        super();
044    }
045
046    /**
047     * Creates a new {@code WebIniSecurityManagerFactory} instance which will construct web-capable
048     * {@code SecurityManager} instances.  Uses the given {@link Ini} instance to construct the instance.
049     *
050     * @param config the Ini configuration that will be used to construct new web-capable {@code SecurityManager}
051     *               instances.
052     */
053    public WebIniSecurityManagerFactory(Ini config) {
054        super(config);
055    }
056
057    /**
058     * Simply returns <code>new {@link DefaultWebSecurityManager}();</code> to ensure a web-capable
059     * {@code SecurityManager} is available by default.
060     *
061     * @return a new web-capable {@code SecurityManager} instance.
062     */
063    @Override
064    protected SecurityManager createDefaultInstance() {
065        return new DefaultWebSecurityManager();
066    }
067
068    @SuppressWarnings({"unchecked"})
069    @Override
070    protected Map<String, ?> createDefaults(Ini ini, Ini.Section mainSection) {
071        Map defaults = super.createDefaults(ini, mainSection);
072        //add the default filters:
073        Map<String, Filter> defaultFilters = DefaultFilter.createInstanceMap(null);
074        defaults.putAll(defaultFilters);
075        return defaults;
076    }
077}