mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-07-15 01:07:30 +08:00
Limit Servlet Listener registration
Limit Servlet Listener registration to the specific supported types. Issue: #54112999
This commit is contained in:
parent
f25b9e109f
commit
d8eadfddff
@ -258,7 +258,8 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext
|
||||
for (Entry<String, EventListener> listenerBean : getOrderedBeansOfType(EventListener.class)) {
|
||||
String name = listenerBean.getKey();
|
||||
EventListener listener = listenerBean.getValue();
|
||||
if (!filterRegistrations.contains(listener)) {
|
||||
if (ServletListenerRegistrationBean.isSupportedType(listener)
|
||||
&& !filterRegistrations.contains(listener)) {
|
||||
ServletListenerRegistrationBean<EventListener> registration = new ServletListenerRegistrationBean<EventListener>(
|
||||
listener);
|
||||
registration.setName(name);
|
||||
|
@ -25,12 +25,12 @@ import org.springframework.core.Conventions;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Base class for {@link ServletRegistrationBean servlet} and
|
||||
* {@link FilterRegistrationBean filter} registration beans.
|
||||
* Base class for Servlet 3.0+ based registration beans.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @see ServletRegistrationBean
|
||||
* @see FilterRegistrationBean
|
||||
* @see ServletListenerRegistrationBean
|
||||
*/
|
||||
public abstract class RegistrationBean implements ServletContextInitializer {
|
||||
|
||||
|
@ -16,31 +16,82 @@
|
||||
|
||||
package org.springframework.boot.context.embedded;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.EventListener;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.ServletContextAttributeListener;
|
||||
import javax.servlet.ServletContextListener;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletRequestAttributeListener;
|
||||
import javax.servlet.ServletRequestListener;
|
||||
import javax.servlet.http.HttpSessionAttributeListener;
|
||||
import javax.servlet.http.HttpSessionListener;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
/**
|
||||
* A {@link ServletContextInitializer} to register {@link EventListener}s in a Servlet
|
||||
* 3.0+ container. Similar to the {@link ServletContext#addListener(EventListener)
|
||||
* registration} features provided by {@link ServletContext} but with a Spring Bean
|
||||
* friendly design.
|
||||
*
|
||||
* This bean can be used to register the following types of listener:
|
||||
* <ul>
|
||||
* <li>{@link ServletContextAttributeListener}</li>
|
||||
* <li>{@link ServletRequestListener}</li>
|
||||
* <li>{@link ServletRequestAttributeListener}</li>
|
||||
* <li>{@link HttpSessionAttributeListener}</li>
|
||||
* <li>{@link HttpSessionListener}</li>
|
||||
* <li>{@link ServletContextListener}</li>
|
||||
* </ul>
|
||||
* @author Dave Syer
|
||||
* @author Phillip Webb
|
||||
* @param <T> the type of listener
|
||||
*/
|
||||
public class ServletListenerRegistrationBean<T extends EventListener> extends
|
||||
RegistrationBean {
|
||||
|
||||
private static final Set<Class<?>> SUPPORTED_TYPES;
|
||||
static {
|
||||
Set<Class<?>> types = new HashSet<Class<?>>();
|
||||
types.add(ServletContextAttributeListener.class);
|
||||
types.add(ServletRequestListener.class);
|
||||
types.add(ServletRequestAttributeListener.class);
|
||||
types.add(HttpSessionAttributeListener.class);
|
||||
types.add(HttpSessionListener.class);
|
||||
types.add(ServletContextListener.class);
|
||||
SUPPORTED_TYPES = Collections.unmodifiableSet(types);
|
||||
}
|
||||
|
||||
private T listener;
|
||||
|
||||
/**
|
||||
* Create a new {@link ServletListenerRegistrationBean} instance.
|
||||
*/
|
||||
public ServletListenerRegistrationBean() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@link ServletListenerRegistrationBean} instance.
|
||||
* @param listener the listener to register
|
||||
*/
|
||||
public ServletListenerRegistrationBean(T listener) {
|
||||
super();
|
||||
Assert.notNull(listener, "Listener must not be null");
|
||||
Assert.isTrue(isSupportedType(listener), "EventLisener is not a supported type");
|
||||
this.listener = listener;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the listener that will be registered.
|
||||
* @param listener the listener to register
|
||||
*/
|
||||
public void setListener(T listener) {
|
||||
Assert.notNull(listener, "Listener must not be null");
|
||||
Assert.isTrue(isSupportedType(listener), "EventLisener is not a supported type");
|
||||
this.listener = listener;
|
||||
}
|
||||
|
||||
@ -53,4 +104,18 @@ public class ServletListenerRegistrationBean<T extends EventListener> extends
|
||||
return this.listener;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns {@code true} if the specified listener is one of the supported types.
|
||||
* @param listener the listener to test
|
||||
* @return if the listener is of a supported type
|
||||
*/
|
||||
public static boolean isSupportedType(EventListener listener) {
|
||||
for (Class<?> type : SUPPORTED_TYPES) {
|
||||
if (ClassUtils.isAssignableValue(type, listener)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -20,7 +20,6 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.boot.context.initializer.ConfigFileApplicationContextInitializer;
|
||||
import org.springframework.context.support.StaticApplicationContext;
|
||||
import org.springframework.core.env.MapPropertySource;
|
||||
import org.springframework.core.env.SimpleCommandLinePropertySource;
|
||||
@ -132,4 +131,9 @@ public class ConfigFileApplicationContextInitializerTests {
|
||||
assertThat(property, equalTo("fromspecificlocation"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultApplicationProperties() throws Exception {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user