如何在Spring Security中保护受ip地址限制的匿名访问?

如何在Spring Security中保护受ip地址限制的匿名访问?,spring,spring-security,Spring,Spring Security,我希望允许匿名访问URL,仅限于特定IP地址子网 网址是: 其中auth是web应用程序的上下文根 访问IP地址为10.102.34.98 访问IP地址的子网掩码为255.255.255.0 trusted.client.subnet属性设置为10.102.34.0/24 匿名访问工作正常: protected void configure(HttpSecurity http) throws Exception { http .csrf().disable()

我希望允许匿名访问URL,仅限于特定IP地址子网

网址是:

其中auth是web应用程序的上下文根

访问IP地址为10.102.34.98 访问IP地址的子网掩码为255.255.255.0 trusted.client.subnet属性设置为10.102.34.0/24

匿名访问工作正常:

protected void configure(HttpSecurity http) throws Exception {
    http
        .csrf().disable()
        .requestMatchers()
            .antMatchers("/login", "/oauth/authorize","/tokens/revoke/**")
            .and()
        .authorizeRequests()
            .antMatchers("/tokens/revoke/**").permitAll()
            .and()
        .authorizeRequests()
            .anyRequest().authenticated()
            .and()
        .formLogin()
            .loginPage("/login")
            .permitAll();
}
但是,一旦我将
permitAll()
替换为
hasIpAddress()
我就会被重定向到我的登录页面

如何允许受IP地址子网限制的匿名访问

protected void configure(HttpSecurity http) throws Exception {
    http
        .csrf().disable()
        .requestMatchers()
            .antMatchers("/login", "/oauth/authorize","/tokens/revoke/**")
            .and()
        .authorizeRequests() 
            .antMatchers("/tokens/revoke/**").hasIpAddress(environment.getProperty("trusted.client.subnet"))
            .and()
        .authorizeRequests()
            .anyRequest().authenticated()
            .and()
        .formLogin()
            .loginPage("/login")
            .permitAll();
}
更新1:

最后的代码强制显示登录表单。我希望它不会考虑到这一点,因为它已经通过了IP地址白名单

    .and()
    .authorizeRequests()
        .anyRequest().authenticated()
        .and()
    .formLogin()
        .loginPage("/login")
        .permitAll();

您应该通过启用Spring安全日志获得更多信息:

-Dlogging.level.org.springframework.security=TRACE
最可能发生的情况是,一个
hasIpAddress
匹配,角色
role\u ANONYMOUS
被提供给发出请求的用户。除非在安全配置中启用了
anonymous()
,否则不会启用该角色

请参见以下其他问题:


你叫什么URL?/tokens/revoke/blabla