【发布时间】: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