Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tan9/fcc312caf520bc39fea5fb1da00aedbd to your computer and use it in GitHub Desktop.
Save tan9/fcc312caf520bc39fea5fb1da00aedbd to your computer and use it in GitHub Desktop.
Spring Boot app on WebLogic workaround.
package com.cht.compost.context.web;
import java.util.EnumSet;
import javax.servlet.DispatcherType;
import javax.servlet.Filter;
import javax.servlet.FilterRegistration;
import javax.servlet.Registration;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import lombok.AllArgsConstructor;
import lombok.experimental.Delegate;
/**
* WebLogic filter registration implementation violated to the Servlet specfication, here comes the dirty workaround.
* <p>
* In WebLogic, overriding the call to {@link FilterRegistration.Dynamic#addMappingForUrlPatterns(EnumSet, boolean, String...)}
* and {@link FilterRegistration.Dynamic#addMappingForServletNames(EnumSet, boolean, String...)} with
* {@code boolean} = {@code true}。
* <p>
* <strong>NOTE:</strong> Only tested on WebLogic Server 12.1.3.0.0, you should test on WebLogics other than this version yourself.
*
* @see <a href="https://github.com/spring-projects/spring-boot/issues/2862#issuecomment-99461807">Spring Boot issue</a>
* and <a href="http://192.168.1.4/issues/143909">Redmine Issue #143909</a>
*/
public abstract class WebLogicFilterOrderingWorkaroundServletInitializer extends SpringBootServletInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
if (isInWebLogic()) {
servletContext = new ServletContextWrapper(servletContext);
}
super.onStartup(servletContext);
}
public boolean isInWebLogic() {
// TODO look for more robust test?
return System.getProperty("weblogic.Name") != null && System.getProperty("weblogic.home") != null;
}
@AllArgsConstructor
static class ServletContextWrapper implements ServletContext, ServletContextOverrider {
@Delegate(excludes = ServletContextOverrider.class)
private final ServletContext delegate;
@Override
public FilterRegistration.Dynamic addFilter(String filterName, String className) {
FilterRegistration.Dynamic dynamic = delegate.addFilter(filterName, className);
return new FilterRegistrationDynamicWrapper(dynamic);
}
@Override
public FilterRegistration.Dynamic addFilter(String filterName, Filter filter) {
FilterRegistration.Dynamic dynamic = delegate.addFilter(filterName, filter);
return new FilterRegistrationDynamicWrapper(dynamic);
}
@Override
public FilterRegistration.Dynamic addFilter(String filterName, Class<? extends Filter> filterClass) {
FilterRegistration.Dynamic dynamic = delegate.addFilter(filterName, filterClass);
return new FilterRegistrationDynamicWrapper(dynamic);
}
}
interface ServletContextOverrider {
FilterRegistration.Dynamic addFilter(String filterName, String className);
FilterRegistration.Dynamic addFilter(String filterName, Filter filter);
FilterRegistration.Dynamic addFilter(String filterName, Class<? extends Filter> filterClass);
}
@AllArgsConstructor
static class FilterRegistrationDynamicWrapper implements FilterRegistration.Dynamic, FilterRegistrationDynamicOverrider {
@Delegate(types = {FilterRegistration.Dynamic.class, Registration.Dynamic.class},
excludes = FilterRegistrationDynamicOverrider.class)
private final FilterRegistration.Dynamic delegate;
@Override
public void addMappingForServletNames(EnumSet<DispatcherType> dispatcherTypes, boolean isMatchAfter, String... servletNames) {
// IMPORTANT! Overridding the call to WebLogic registration.
delegate.addMappingForServletNames(dispatcherTypes, true, servletNames);
}
@Override
public void addMappingForUrlPatterns(EnumSet<DispatcherType> dispatcherTypes, boolean isMatchAfter, String... urlPatterns) {
// IMPORTANT! Overridding the call to WebLogic registration.
delegate.addMappingForUrlPatterns(dispatcherTypes, true, urlPatterns);
}
}
interface FilterRegistrationDynamicOverrider {
void addMappingForServletNames(
EnumSet<DispatcherType> dispatcherTypes,
boolean isMatchAfter, String... servletNames);
void addMappingForUrlPatterns(
EnumSet<DispatcherType> dispatcherTypes,
boolean isMatchAfter, String... urlPatterns);
}
}
@tan9
Copy link
Author

tan9 commented Jul 27, 2016

If you are going to deploy your Spring Boot application to WebLogic server, extending this class instead of SpringBootServletInitializer.

@tan9
Copy link
Author

tan9 commented Jul 27, 2016

Copy link

ghost commented Apr 18, 2017

This code is not even compiling now. A lot has changed in springboot since then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment