Java SpringMVC:如何过滤客户端&x27;s IP?

Java SpringMVC:如何过滤客户端&x27;s IP?,java,spring,spring-mvc,spring-security,spring-boot,Java,Spring,Spring Mvc,Spring Security,Spring Boot,我使用弹簧靴,我有一个@控制器。为了安全起见,我只想接受127.0.0.1的请求(@RequestMapping)。我该怎么做呢?您可以使用Spring Security的。例如: <http use-expressions="true"> <intercept-url pattern="/admin*" access="hasIpAddress('127.0.0.1/24')"/> ... </http> ... 选项1。

我使用
弹簧靴
,我有一个
@控制器
。为了安全起见,我只想接受
127.0.0.1
的请求(
@RequestMapping
)。我该怎么做呢?

您可以使用Spring Security的。例如:

<http use-expressions="true">
    <intercept-url pattern="/admin*"
        access="hasIpAddress('127.0.0.1/24')"/>
    ...
</http>

...
选项1。 由于您使用的是spring boot,我假设您更喜欢使用spring的自动配置类。请使用WebSecurityConfigureAdapter并在那里配置您的访问规则

@EnableWebSecurity
@Configuration
public class CustomWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {

      @Override
      protected void configure(HttpSecurity http) throws Exception {
        http
          .authorizeUrls()
            .antMatchers("/admin/**").hasRole("ADMIN")
            .antMatchers("/**").access("hasIpAddress('127.0.0.1/24')")
            .anyRequest().authenticated();

      }
    }

选择2 如果您使用的是tomcat,则可以在application.properties上自定义tomcat的代理配置。参考资料


server.tomcat.internal proxies=192\\.168\\\\\\\d{1,3}\\\\\d{1,3}

您的答案更好