【问题标题】:Spring Security hasIpAddress causes errorsSpring Security hasIpAddress 导致错误
【发布时间】:2016-10-06 13:20:43
【问题描述】:

在我的配置中,我已经定义了:

@EnableGlobalMethodSecurity(prePostEnabled = true, proxyTargetClass = true)

并使用以下代码:

    http.exceptionHandling()
            .authenticationEntryPoint(authEntryPoint)
            .and()
            .authorizeRequests()
            .antMatchers("/api/payment").permitAll()
            //.antMatchers("/api/payment").hasIpAddress("XXX.XXX.X.XX")
...

现在,当我允许所有流量流向“/api/payment”时,一切都会执行。

但是,如果我尝试启用 IP 验证:

    http.exceptionHandling()
            .authenticationEntryPoint(authEntryPoint)
            .and()
            .authorizeRequests()
            //.antMatchers("/api/payment").permitAll()
            .antMatchers("/api/payment").hasIpAddress("XXX.XXX.X.XX")
...

从给定 IP 地址发送的请求会收到以下响应:

{"errorMessage":"Not Found","errorId":"6ae34aa3-9195-42d6-8906-996906988ce0","errorDetails":null} 

我做错了什么?

编辑

这是我正在调用的方法:

@RequestMapping(value = "/payment", method = POST)
public String saveOrder(@ModelAttribute PaymentDto paymentDto) {
    paymentService.savePayment(paymentDto);
    return "OK";
}

由于我总是发送请求数据(例如帖子在以下网址:api/payment?id=123&whatever

我已添加:

.antMatchers("/api/payment?**").access("hasIpAddress('XXX.XXX.X.XX')")

但这也没有用。

更新 2

我已经像这样改变了我的 antMatcher 的位置:

http.exceptionHandling()
        .authenticationEntryPoint(authEntryPoint)
        .and()
        .authorizeRequests()
        .antMatchers("/", "/app/**", "/assets/**", "/fonts/**", "/images/**").permitAll()
        .antMatchers("/authentication/login", "/login.html").anonymous()
        .antMatchers("/api/products/**").permitAll()
        .antMatchers("/api/cities/**").permitAll()
        .antMatchers("/api/order/**").permitAll()
        .antMatchers("/api/payment").hasIpAddress("XXX.XXX.X.XX")
        .anyRequest().authenticated()
        .and()
        .csrf().requireCsrfProtectionMatcher(new CsrfRequestMatcher())
        .and()
        .formLogin()
        .loginPage("/login.html")
        .loginProcessingUrl("/authentication/login")
        .successHandler(authSuccessHandler)
        .failureHandler(authFailureHandler)
        .and()
        .logout()
        .logoutUrl("/authentication/logout")
        .logoutSuccessHandler(logoutSuccessHandler)
        .deleteCookies(JSESSIONID_COOKIE, CSRF_COOKIE)
        .invalidateHttpSession(true)
        .permitAll();

当我尝试从给定的 IP 地址发帖时,它现在只是将我重定向到登录页面

【问题讨论】:

  • 可能你得到一个空指针异常
  • @reos,没有空指针异常。该方法根本没有执行。
  • 可能IP为空,spring抛出空指针
  • IP地址怎么可以为空?
  • 不知道spring是如何获取IP地址的。有没有办法知道Spring拿到的IP?

标签: spring spring-security spring-boot


【解决方案1】:

结果我发现,我通过代理从 nginx 接收请求,它没有传递请求者的原始 IP 地址。我不得不添加一些额外的 nginx 配置,现在,我可以像这样验证 IP 地址:

@RequestMapping(value = "/payment", method = POST)
public String saveOrder(PaymentDto paymentDto, HttpServletRequest request) {
    if (request.getHeader("X-Forwarded-For").equals("XXX.XXX.X.XX")){
      DO STUFF
    }
}

【讨论】:

    猜你喜欢
    • 2016-03-02
    • 2019-05-04
    • 2015-10-21
    • 2019-06-24
    • 2020-11-30
    • 2015-04-02
    • 1970-01-01
    • 2018-04-15
    • 1970-01-01
    相关资源
    最近更新 更多