【问题标题】:How to secure anonymous access restricted by ip address in Spring Security?如何保护 Spring Security 中受 IP 地址限制的匿名访问?
【发布时间】:2019-01-01 06:25:55
【问题描述】:

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

网址是:

http://10.102.34.98:880/auth/tokens/revoke/blabla 其中 auth 是网络应用的上下文根。

访问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();

【问题讨论】:

  • 你叫什么网址?
  • /tokens/revoke/blabla

标签: spring spring-security


【解决方案1】:

您应该通过启用 Spring Security 日志来获取更多信息:

-Dlogging.level.org.springframework.security=TRACE

最有可能发生的情况是,一个hasIpAddress 匹配,角色ROLE_ANONYMOUS 被赋予发出请求的用户。除非在您的安全配置中启用了 anonymous(),否则不会启用该角色。

查看其他问题:

【讨论】:

    猜你喜欢
    • 2014-05-28
    • 2015-03-21
    • 2020-06-11
    • 1970-01-01
    • 1970-01-01
    • 2011-01-01
    • 2015-10-09
    • 1970-01-01
    • 2015-04-25
    相关资源
    最近更新 更多