Spring Boot 应用程序中如何限制访问IP地址?

x33g5p2x  于2022-09-28 转载在 Spring  
字(1.2k)|赞(0)|评价(0)|浏览(2692)

您可以通过 IPAddressAccessControlHandlerSpring Boot 中配置IP 地址的访问控制列表。 让我们看看怎么做。

@Bean public UndertowServletWebServerFactory embeddedServletContainerFactory() {
  UndertowServletWebServerFactory factory = new UndertowServletWebServerFactory();
  factory.addDeploymentInfoCustomizers(new UndertowDeploymentInfoCustomizer() {
    @Override public void customize(DeploymentInfo deploymentInfo) {
      // Enable IPAddressAccessControlHandler
      deploymentInfo.addOuterHandlerChainWrapper(new HandlerWrapper() {
        @Override public HttpHandler wrap(HttpHandler handler) {
          return new IPAddressAccessControlHandler(handler).setDefaultAllow(false).addAllow("192.168.10.1").addAllow("192.168.10.2");
        }
      });
    }
  });
  return factory;
}

如果您愿意,可以使用 HandlerParser.parse() 方法来读取 IP ACL:

@Bean public UndertowServletWebServerFactory embeddedServletContainerFactory() {
UndertowServletWebServerFactory factory = new UndertowServletWebServerFactory();
factory.addDeploymentInfoCustomizers(new UndertowDeploymentInfoCustomizer() {
    @Override public void customize(DeploymentInfo deploymentInfo) {
      deploymentInfo.addOuterHandlerChainWrapper(HandlerParser.parse("ip-access-control(default-allow=false, acl={'192.168.10.1 allow', '192.168.10.2 allow'})", getClass().getClassLoader()));
    });
}
});
return factory;
}

相关文章