【问题标题】:Restrict tomcat in spring boot to certain ip addresses将spring boot中的tomcat限制为某些ip地址
【发布时间】:2015-01-02 14:38:09
【问题描述】:

我需要将基于 Spring Boot 的应用程序的嵌入式 tomcat 限制为某些 IP 地址。我只想允许来自两个 IP 地址的传入连接,而不是全部。 我知道如何在未运行嵌入式但不知道如何在 Spring Boot 中配置它的 tomcat 中执行此操作。各种server.tomcat.* 属性似乎并不为此提供支持。 server.address 属性可以让我绑定到本地 IP 地址,但这不是我需要的。

【问题讨论】:

  • 如果您知道如何在独立容器中执行此操作,那么在 Tomcat 中找到 API 应该不难(可能是 ValveConnector 属性 - 从您的服务器.xml)。不过,客户很容易欺骗他们的 IP 地址,因此如果您的目标是安全,最好还是坚持其他方式。

标签: java spring security tomcat spring-boot


【解决方案1】:

找到此答案以寻找相同的解决方案。这是在 Spring Boot 中执行此操作的更准确的方法。

@Bean
public FilterRegistrationBean remoteAddressFilter() {

    FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
    RemoteAddrFilter filter = new RemoteAddrFilter();

    filter.setAllow("192.168.0.2");
    filter.setDenyStatus(404);

    filterRegistrationBean.setFilter(filter);
    filterRegistrationBean.addUrlPatterns("/*");

    return filterRegistrationBean;

}

默认响应是 403。要将其更改为 404,请添加 filter.setDenyStatus(404);

您也可以使用filter.setDeny("192\\.168\\.0\\.2"); 设置拒绝地址

RemoteAddressFilter Docs for Tomcat

【讨论】:

    【解决方案2】:

    如果你想添加多个 IP 地址,那么你可以通过使用带有自定义身份验证提供程序的 Spring Security 来做到这一点。自定义 Authentication Provider 配置如下:

    @Component
    public class CustomIpAuthenticationProvider implements AuthenticationProvider {
    
       Set<String> whitelist = new HashSet<String>();
    
        public CustomIpAuthenticationProvider() {
            whitelist.add("103.219.56.22");
            whitelist.add("192.168.2.33");
        }
    
        @Override
        public Authentication authenticate(Authentication auth) throws AuthenticationException {
    
            WebAuthenticationDetails details = (WebAuthenticationDetails) auth.getDetails();
            String userIp = details.getRemoteAddress();
    
            if(! whitelist.contains(userIp)) {
                throw new BadCredentialsException("Invalid IP Address");
            }
        }
    }
    

    以及 Spring Security 配置:

    @Configuration
    @EnableWebSecurity
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
    
        @Autowired
        private CustomIpAuthenticationProvider authenticationProvider;
    
        @Override
        protected void configure(AuthenticationManagerBuilder auth) throws Exception {
           auth.authenticationProvider(authenticationProvider);
        }
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
    
            http.authorizeRequests()
              .antMatchers("/login").permitAll()
              .anyRequest().authenticated()
              .and().formLogin().permitAll()
              .and().csrf().disable();
        }
    
    }
    

    或者,如果您只想从某些特定 IP 地址访问某些特定映射,那么 Spring Security 配置如下:

    @Configuration
    @EnableWebSecurity
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
    
            http.authorizeRequests()
              .antMatchers("/login").permitAll()
              .antMatchers("/rockstar/**").hasIpAddress("103.219.55.22")
              .anyRequest().authenticated()
              .and()
              .formLogin().permitAll()
              .and()
              .csrf().disable();
        }
    }
    

    【讨论】:

    • 这是一个很好的答案,但它是一个应用程序 IP 过滤器,上面的答案是一个容器 IP 过滤器,这是一个更好的解决方案
    猜你喜欢
    • 2020-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-17
    相关资源
    最近更新 更多