gpt4 book ai didi

spring - 无法在 PreAuthorize 中评估表达式 'hasIpAddress(..)'

转载 作者:行者123 更新时间:2023-12-04 18:28:34 26 4
gpt4 key购买 nike

为了寻找this的答案问题,我一直在研究基于 IP 地址过滤请求的方法。
我有以下方法:

@RequestMapping(value = "/payment", method = POST)
@PreAuthorize("hasIpAddress('XXX.XXX.X.XX')")
public String pay(PaymentDto paymentDto){
System.out.println("Payment received");
return "OK";
}

但是,在执行时,这会给我一个错误:
{"errorMessage":"Internal Server Error","errorId":"26b1a1ba-3ae8-4497-9f1c-7370ea5116ff","errorDetails":{"message":"Failed to evaluate expression 'hasIpAddress('XXX.XXX.X.XX')'","exception":"java.lang.IllegalArgumentException","errors":null}} 

到底是怎么回事?

这是Java错误:
org.springframework.expression.spel.SpelEvaluationException: EL1004E:(pos 0): Method call: Method hasIpAddress(java.lang.String) cannot be found on org.springframework.security.access.expression.method.MethodSecurityExpressionRoot type
at org.springframework.expression.spel.ast.MethodReference.findAccessorForMethod(MethodReference.java:211) ~[spring-expression-4.2.3.RELEASE.jar!/:4.2.3.RELEASE]
at org.springframework.expression.spel.ast.MethodReference.getValueInternal(MethodReference.java:125) ~[spring-expression-4.2.3.RELEASE.jar!/:4.2.3.RELEASE]
at org.springframework.expression.spel.ast.MethodReference.getValueInternal(MethodReference.java:85) ~[spring-expression-4.2.3.RELEASE.jar!/:4.2.3.RELEASE]
at org.springframework.expression.spel.ast.SpelNodeImpl.getTypedValue(SpelNodeImpl.java:131) ~[spring-expression-4.2.3.RELEASE.jar!/:4.2.3.RELEASE]

最佳答案

Doc, hasIpAddress is Web Security Expression and not available for @PreAuthorize. You can use like this


<http use-expressions="true">
<intercept-url pattern="/admin*"
access="hasRole('admin') and hasIpAddress('xxx.xx.xx.xxx')"/>
...
</http>

或者
http
.authorizeRequests()
.antMatchers("/tokens").access(
"hasIpAddress('xxx.x.xx.xx'))

但不像
@PreAuthorize("hasIpAddress('XXX.XXX.X.XX')")
public String pay(PaymentDto paymentDto){

关于spring - 无法在 PreAuthorize 中评估表达式 'hasIpAddress(..)',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39911141/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com