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.web.servlet.FilterRegistrationBean;
028import org.springframework.context.annotation.Bean;
029import org.springframework.context.annotation.Configuration;
030
031import java.util.List;
032
033/**
034 * @since 1.4.0
035 */
036@Configuration
037@ConditionalOnProperty(name = "shiro.web.enabled", matchIfMissing = true)
038public class ShiroWebFilterConfiguration extends AbstractShiroWebFilterConfiguration {
039
040    public static final String REGISTRATION_BEAN_NAME = "filterShiroFilterRegistrationBean";
041    public static final String FILTER_NAME = "shiroFilter";
042
043    @Bean
044    @ConditionalOnMissingBean
045    @Override
046    protected ShiroFilterFactoryBean shiroFilterFactoryBean() {
047        return super.shiroFilterFactoryBean();
048    }
049
050    @Bean(name = REGISTRATION_BEAN_NAME)
051    @ConditionalOnMissingBean(name = REGISTRATION_BEAN_NAME)
052    protected FilterRegistrationBean<AbstractShiroFilter> filterShiroFilterRegistrationBean() throws Exception {
053
054        FilterRegistrationBean<AbstractShiroFilter> filterRegistrationBean = new FilterRegistrationBean<>();
055        filterRegistrationBean.setDispatcherTypes(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.INCLUDE, DispatcherType.ERROR);
056        filterRegistrationBean.setFilter((AbstractShiroFilter) shiroFilterFactoryBean().getObject());
057        filterRegistrationBean.setName(FILTER_NAME);
058        filterRegistrationBean.setOrder(1);
059
060        return filterRegistrationBean;
061    }
062
063    @Bean(name = "globalFilters")
064    @ConditionalOnMissingBean
065    protected List<String> globalFilters() {
066        return super.globalFilters();
067    }
068}