code: public static WebApplicationContext findRequiredWebApplicationContext(final ServletContext servletContext) {
  
          WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(servletContext);
          if (wac == null) {
              final Enumeration<String> attrNames = servletContext.getAttributeNames();
              while (attrNames.hasMoreElements()) {
                  final String attrName = attrNames.nextElement();
                  final Object attrValue = servletContext.getAttribute(attrName);
                  if (attrValue instanceof WebApplicationContext) {
                      if (wac != null) {
                          throw new IllegalStateException("No unique WebApplicationContext found: more than one " +
                                  "DispatcherServlet registered with publishContext=true?");
                      }
                      wac = (WebApplicationContext) attrValue;
                  }
              }
          }
  
          if (wac == null) {
              throw new IllegalStateException("No WebApplicationContext found: no ContextLoaderListener registered?");
          }
  
          return wac;
  
      }
docs: /*
  This method mimics the behaviour in
  org.springframework.security.web.context.support.SecurityWebApplicationContextUtils#findRequiredWebApplicationContext(sc),
  which provides a default mechanism for looking for the WebApplicationContext as an attribute of the
  ServletContext that might have been declared with a non-standard name (if it had, it would be
  detected by WebApplicationContextUtils.getWebApplicationContext(sc). This is a behaviour directly
  supported in Spring Framework >= 4.2 thanks to
  org.springframework.web.context.support.WebApplicationContextUtils#findWebApplicationContext(sc).
  
  Unfortunately, the org.springframework.security.web.context.support.SecurityWebApplicationContextUtils class is
  only available since Spring Security 4.1, so we cannot simply call it if we want to support Spring Security 4.0.
  That's why this method basically mimics its behaviour.