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.config.web.autoconfigure;
020
021import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
022import org.apache.shiro.spring.web.config.AbstractShiroWebFilterConfiguration;
023import org.apache.shiro.web.servlet.AbstractShiroFilter;
024import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
025import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
026import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
027import org.springframework.boot.web.servlet.FilterRegistrationBean;
028import org.springframework.context.annotation.Bean;
029import org.springframework.context.annotation.Configuration;
030
031import javax.servlet.DispatcherType;
032import java.util.List;
033
034/**
035 * @since 1.4.0
036 */
037@Configuration
038@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
039@ConditionalOnProperty(name = "shiro.web.enabled", matchIfMissing = true)
040public class ShiroWebFilterConfiguration extends AbstractShiroWebFilterConfiguration {
041
042    /**
043     * registration bean name key.
044     */
045    public static final String REGISTRATION_BEAN_NAME = "filterShiroFilterRegistrationBean";
046    /**
047     * shiro filter name key.
048     */
049    public static final String FILTER_NAME = "shiroFilter";
050
051    @Bean
052    @ConditionalOnMissingBean
053    @Override
054    protected ShiroFilterFactoryBean shiroFilterFactoryBean() {
055        return super.shiroFilterFactoryBean();
056    }
057
058    @Bean(name = REGISTRATION_BEAN_NAME)
059    @ConditionalOnMissingBean(name = REGISTRATION_BEAN_NAME)
060    protected FilterRegistrationBean<AbstractShiroFilter> filterShiroFilterRegistrationBean() throws Exception {
061
062        FilterRegistrationBean<AbstractShiroFilter> filterRegistrationBean = new FilterRegistrationBean<>();
063        filterRegistrationBean.setDispatcherTypes(DispatcherType.REQUEST, DispatcherType.FORWARD,
064                                                  DispatcherType.INCLUDE, DispatcherType.ERROR);
065        filterRegistrationBean.setFilter((AbstractShiroFilter) shiroFilterFactoryBean().getObject());
066        filterRegistrationBean.setName(FILTER_NAME);
067        filterRegistrationBean.setOrder(1);
068
069        return filterRegistrationBean;
070    }
071
072    @Bean(name = "globalFilters")
073    @ConditionalOnMissingBean
074    protected List<String> globalFilters() {
075        return super.globalFilters();
076    }
077}