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 javax.servlet.DispatcherType;
022import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
023import org.apache.shiro.spring.web.config.AbstractShiroWebFilterConfiguration;
024import org.apache.shiro.web.servlet.AbstractShiroFilter;
025import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
026import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
027import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
028import org.springframework.boot.web.servlet.FilterRegistrationBean;
029import org.springframework.context.annotation.Bean;
030import org.springframework.context.annotation.Configuration;
031
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    public static final String REGISTRATION_BEAN_NAME = "filterShiroFilterRegistrationBean";
043    public static final String FILTER_NAME = "shiroFilter";
044
045    @Bean
046    @ConditionalOnMissingBean
047    @Override
048    protected ShiroFilterFactoryBean shiroFilterFactoryBean() {
049        return super.shiroFilterFactoryBean();
050    }
051
052    @Bean(name = REGISTRATION_BEAN_NAME)
053    @ConditionalOnMissingBean(name = REGISTRATION_BEAN_NAME)
054    protected FilterRegistrationBean<AbstractShiroFilter> filterShiroFilterRegistrationBean() throws Exception {
055
056        FilterRegistrationBean<AbstractShiroFilter> filterRegistrationBean = new FilterRegistrationBean<>();
057        filterRegistrationBean.setDispatcherTypes(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.INCLUDE, DispatcherType.ERROR);
058        filterRegistrationBean.setFilter((AbstractShiroFilter) shiroFilterFactoryBean().getObject());
059        filterRegistrationBean.setName(FILTER_NAME);
060        filterRegistrationBean.setOrder(1);
061
062        return filterRegistrationBean;
063    }
064
065    @Bean(name = "globalFilters")
066    @ConditionalOnMissingBean
067    protected List<String> globalFilters() {
068        return super.globalFilters();
069    }
070}