javaspring-securityweb.xmlspring-java-configjava-melody

How to convert web.xml code to java config for java melody


Is it possible to completely eliminate web.xml from a project and convert it into Java configuration?

How to convert the following web.xml to java configuration?

I have gone through few links for understanding this

Some of them are : How to replace web.xml with application context config files?

But could not find any tutorial/blog how to replace each member of web.xml to correspnding java config..It would be really helpful if anything is available..

For example some of the filters come via libraries and we just need to declare in web.xml for functionality..How can the same be achieved in java config (replacing entire web.xml with java config)

  <filter>
            <filter-name>monitoring</filter-name>
            <filter-class>net.bull.javamelody.MonitoringFilter</filter-class>
            <init-param>
                <param-name>allowed-addr-pattern</param-name>
                <param-value>127\.0\..*\..*</param-value>
            </init-param>
        </filter>
        <filter-mapping>
            <filter-name>monitoring</filter-name>
            <url-pattern>/monitoring</url-pattern>
        </filter-mapping>
        <listener>
            <listener-class>net.bull.javamelody.SessionListener</listener-class>
        </listener>


        <login-config>
            <auth-method>BASIC</auth-method>
            <realm-name>Monitoring</realm-name>
        </login-config>

        <security-constraint>
            <web-resource-collection>
                <web-resource-name>Monitoring</web-resource-name>
                <url-pattern>/monitoring</url-pattern>
            </web-resource-collection>
            <auth-constraint>
                <role-name>jmon-admin</role-name>
            </auth-constraint>
            <!-- if SSL enabled (SSL and certificate must then be configured in the 
                server) <user-data-constraint> <transport-guarantee>CONFIDENTIAL</transport-guarantee> 
                </user-data-constraint> -->
        </security-constraint>

Solution

  • I guess I managed it finally

    For monitoring sql add this to your datasources config

      @Bean
      public SpringDataSourceBeanPostProcessor monitoringDataSourceBeanPostProcessor() {
        SpringDataSourceBeanPostProcessor processor = new SpringDataSourceBeanPostProcessor();
        processor.setExcludedDatasources(null);
        return processor;
      }
    

    add this to your securityConfig (you can restrict it for specific roles)

    .antMatchers("/monitoring/**")
    

    and optional step is edit this class (in order to override onStartup method) but I guess it's not needed by default

    import net.bull.javamelody.MonitoringFilter;
    import net.bull.javamelody.Parameter;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
    
    import javax.servlet.DispatcherType;
    import javax.servlet.FilterRegistration;
    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    import java.util.EnumSet;
    
    public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
    
      private static final Logger LOGGER = LoggerFactory.getLogger(AppInitializer.class);
      @Override
      protected Class<?>[] getRootConfigClasses() {
        return null;
      }
    
      @Override
      protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[]{SpringWebConfig.class};
      }
    
      @Override
      protected String[] getServletMappings() {
        return new String[]{"/"};
      }
    
      @Override
      public void onStartup(ServletContext servletContext) throws ServletException {
        super.onStartup(servletContext);
        FilterRegistration javaMelody = servletContext.getFilterRegistration("javamelody");
        if (javaMelody != null) {
          LOGGER.info("Java Melody Filter Registration found: {}", javaMelody);
          // Filter registered by Servlet Container via web-fragment in
          // javamelody.jar
          addFilter(javaMelody);
        } else {
          LOGGER.info("Java Melody Filter Registration not found. Registering dynamically");
          // Running in embedded server mode.
          FilterRegistration.Dynamic javaMelodyFilter = servletContext.addFilter("javamelody", new MonitoringFilter());
          addFilter(javaMelodyFilter);
        }
      }
    
      private void addFilter(FilterRegistration javaMelody) {
        javaMelody.addMappingForUrlPatterns(
                EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.ASYNC), true, "/*");
        javaMelody.setInitParameter(Parameter.LOG.getCode(), Boolean.toString(false));
        javaMelody.setInitParameter(Parameter.DISPLAYED_COUNTERS.getCode(), "http,sql,error,log");
      }
    
    }
    

    I found this sources here and here