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    protected List<String> globalFilters() {
060        return Collections.singletonList(DefaultFilter.invalidRequest.name());
061    }
062
063    protected ShiroFilterConfiguration shiroFilterConfiguration() {
064        return shiroFilterConfiguration != null
065                ? shiroFilterConfiguration
066                : new ShiroFilterConfiguration();
067    }
068
069    protected ShiroFilterFactoryBean shiroFilterFactoryBean() {
070        ShiroFilterFactoryBean filterFactoryBean = new ShiroFilterFactoryBean();
071
072        filterFactoryBean.setLoginUrl(loginUrl);
073        filterFactoryBean.setSuccessUrl(successUrl);
074        filterFactoryBean.setUnauthorizedUrl(unauthorizedUrl);
075
076        filterFactoryBean.setSecurityManager(securityManager);
077        filterFactoryBean.setShiroFilterConfiguration(shiroFilterConfiguration());
078        filterFactoryBean.setGlobalFilters(globalFilters());
079        filterFactoryBean.setFilterChainDefinitionMap(shiroFilterChainDefinition.getFilterChainMap());
080
081        if (filterMap != null) {
082            filterFactoryBean.setFilters(filterMap);
083        }
084
085        return filterFactoryBean;
086    }
087}