有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

spring security如何转换web。java melody的java配置的xml代码

是否有可能完全消除网络。从项目中提取xml并将其转换为Java配置

如何转换以下网页。xml到java配置

为了理解这一点,我浏览了几个链接

其中包括: How to replace web.xml with application context config files?

但找不到任何教程/博客如何替换web的每个成员。xml到对应的java配置。。如果有什么可以提供的话,那将非常有帮助

例如,一些过滤器是通过库来的,我们只需要在web中声明。用于功能的xml。。如何在java配置中实现同样的功能(用java配置替换整个web.xml)

  <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>

共 (1) 个答案

  1. # 1 楼答案

    我想我终于成功了

    要监视sql,请将其添加到数据源配置中

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

    将其添加到您的securityConfig(您可以针对特定角色对其进行限制)

    .antMatchers("/monitoring/**")
    

    可选的步骤是编辑这个类(为了重写onStartup方法),但我想默认情况下不需要它

    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");
      }
    
    }
    

    我发现这个来源是herehere