Use Spring Session's defaults for its filter's dispatcher types

Closes gh-8288
pull/8300/head
Andy Wilkinson 8 years ago
parent fe6320db42
commit 57fe8b966e

@ -58,7 +58,8 @@ import org.springframework.util.Assert;
@AutoConfigureAfter({ DataSourceAutoConfiguration.class, HazelcastAutoConfiguration.class, @AutoConfigureAfter({ DataSourceAutoConfiguration.class, HazelcastAutoConfiguration.class,
JdbcTemplateAutoConfiguration.class, MongoAutoConfiguration.class, JdbcTemplateAutoConfiguration.class, MongoAutoConfiguration.class,
RedisAutoConfiguration.class }) RedisAutoConfiguration.class })
@Import({ SessionConfigurationImportSelector.class, SessionRepositoryValidator.class }) @Import({ SessionConfigurationImportSelector.class, SessionRepositoryValidator.class,
SessionRepositoryFilterConfiguration.class })
public class SessionAutoConfiguration { public class SessionAutoConfiguration {
/** /**

@ -0,0 +1,49 @@
/*
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.autoconfigure.session;
import java.util.EnumSet;
import javax.servlet.DispatcherType;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.session.web.http.SessionRepositoryFilter;
/**
* Configuration for customizing the registration of the {@link SessionRepositoryFilter}.
*
* @author Andy Wilkinson
*/
@Configuration
@ConditionalOnBean(SessionRepositoryFilter.class)
class SessionRepositoryFilterConfiguration {
@Bean
public FilterRegistrationBean<SessionRepositoryFilter<?>> sessionRepositoryFilterRegistration(
SessionRepositoryFilter<?> filter) {
FilterRegistrationBean<SessionRepositoryFilter<?>> registration = new FilterRegistrationBean<SessionRepositoryFilter<?>>(
filter);
registration.setDispatcherTypes(EnumSet.of(DispatcherType.ASYNC,
DispatcherType.ERROR, DispatcherType.REQUEST));
registration.setOrder(SessionRepositoryFilter.DEFAULT_ORDER);
return registration;
}
}

@ -18,6 +18,9 @@ package org.springframework.boot.autoconfigure.session;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.EnumSet;
import javax.servlet.DispatcherType;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
@ -28,12 +31,15 @@ import org.springframework.beans.factory.BeanCreationException;
import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration; import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration;
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration; import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
import org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration; import org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.session.ExpiringSession; import org.springframework.session.ExpiringSession;
import org.springframework.session.MapSessionRepository; import org.springframework.session.MapSessionRepository;
import org.springframework.session.SessionRepository; import org.springframework.session.SessionRepository;
import org.springframework.session.data.mongo.MongoOperationsSessionRepository; import org.springframework.session.data.mongo.MongoOperationsSessionRepository;
import org.springframework.session.web.http.SessionRepositoryFilter;
import org.springframework.test.util.ReflectionTestUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -124,6 +130,19 @@ public class SessionAutoConfigurationTests extends AbstractSessionAutoConfigurat
load("spring.session.store-type=jdbc"); load("spring.session.store-type=jdbc");
} }
@SuppressWarnings("unchecked")
@Test
public void filterIsRegisteredWithAsyncErrorAndRequestDispatcherTypes() {
load("spring.session.store-type=hash-map");
FilterRegistrationBean<?> registration = this.context
.getBean(FilterRegistrationBean.class);
assertThat(registration.getFilter())
.isSameAs(this.context.getBean(SessionRepositoryFilter.class));
assertThat((EnumSet<DispatcherType>) ReflectionTestUtils.getField(registration,
"dispatcherTypes")).containsOnly(DispatcherType.ASYNC,
DispatcherType.ERROR, DispatcherType.REQUEST);
}
@Configuration @Configuration
static class SessionRepositoryConfiguration { static class SessionRepositoryConfiguration {

Loading…
Cancel
Save