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.spring.web.config;
020
021import org.apache.shiro.mgt.SecurityManager;
022import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
023import org.apache.shiro.web.config.ShiroFilterConfiguration;
024import org.apache.shiro.web.filter.mgt.DefaultFilter;
025import org.springframework.beans.factory.annotation.Autowired;
026import org.springframework.beans.factory.annotation.Value;
027
028import javax.servlet.Filter;
029import java.util.Collections;
030import java.util.List;
031import java.util.Map;
032
033/**
034 * @since 1.4.0
035 */
036public class AbstractShiroWebFilterConfiguration {
037
038    @Autowired
039    protected SecurityManager securityManager;
040
041    @Autowired
042    protected ShiroFilterChainDefinition shiroFilterChainDefinition;
043
044    @Autowired(required = false)
045    protected ShiroFilterConfiguration shiroFilterConfiguration;
046
047    @Autowired(required = false)
048    protected Map<String, Filter> filterMap;
049
050    @Value("#{ @environment['shiro.loginUrl'] ?: '/login.jsp' }")
051    protected String loginUrl;
052
053    @Value("#{ @environment['shiro.successUrl'] ?: '/' }")
054    protected String successUrl;
055
056    @Value("#{ @environment['shiro.unauthorizedUrl'] ?: null }")
057    protected String unauthorizedUrl;
058
059    @Value("#{ @environment['shiro.caseInsensitive'] ?: false }")
060    protected boolean caseInsensitive;
061
062    protected List<String> globalFilters() {
063        return Collections.singletonList(DefaultFilter.invalidRequest.name());
064    }
065
066    protected ShiroFilterConfiguration shiroFilterConfiguration() {
067        return shiroFilterConfiguration != null
068                ? shiroFilterConfiguration
069                : new ShiroFilterConfiguration();
070    }
071
072    protected ShiroFilterFactoryBean shiroFilterFactoryBean() {
073        ShiroFilterFactoryBean filterFactoryBean = new ShiroFilterFactoryBean();
074
075        filterFactoryBean.setLoginUrl(loginUrl);
076        filterFactoryBean.setSuccessUrl(successUrl);
077        filterFactoryBean.setUnauthorizedUrl(unauthorizedUrl);
078        filterFactoryBean.setCaseInsensitive(caseInsensitive);
079
080        filterFactoryBean.setSecurityManager(securityManager);
081        filterFactoryBean.setShiroFilterConfiguration(shiroFilterConfiguration());
082        filterFactoryBean.setGlobalFilters(globalFilters());
083        filterFactoryBean.setFilterChainDefinitionMap(shiroFilterChainDefinition.getFilterChainMap());
084
085        if (filterMap != null) {
086            filterFactoryBean.setFilters(filterMap);
087        }
088
089        return filterFactoryBean;
090    }
091}