Tomcat server IP address restriction methods?

半世苍凉 提交于 2019-12-12 06:24:29

问题


I knew that I can add a Valve in context.xml in tomcat server to allow or deny some IP address :

<Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="127\.0\.0\.1" denyStatus="403" />

Except above configuration, Are there any other method that I can config IP restriction?

For example, can I use text file or database to store IP addresses for IP restriction propose?

Thank you very much!!!


回答1:


You can dynamically register Tomcat's Remote Address Filter.

http://tomcat.apache.org/tomcat-7.0-doc/config/filter.html#Remote_Address_Filter

It looks like this:

@WebListener
public class MyServletContextListener implements ServletContextListener {
  @Override
  public void contextInitialized(ServletContextEvent sce) {
    // Get IP addresses from the DB or text file.
    ...

    ServletContext sc = sce.getServletContext();
    FilterRegistration fr;
    fr = sc.addFilter("RemoteAddrFilter", "org.apache.catalina.filters.RemoteAddrFilter");
    fr.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, "/*");
    fr.setInitParameter("allow", "127\\.0\\.0\\.1");
    fr.setInitParameter("denyStatus", "403");
  }
}


来源:https://stackoverflow.com/questions/17518200/tomcat-server-ip-address-restriction-methods

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!