gpt4 book ai didi

java - Spring Security 中的 Spring Expression Language (SpEL) 比较对象使用 equals() 或 ==?

转载 作者:搜寻专家 更新时间:2023-11-01 02:10:32 27 4
gpt4 key购买 nike

Spring Security 中的 Spring 表达式语言 (SpEL) 比较对象使用 equals() 或 ==?

例如(方法equals()没有被调用!):

class SecurityObject {      
public boolean equals(Object obj) {
//...
}
}

@PreAuthorize(" #secObject == #otherSecObject ")
public void securityMethod(SecurityObject secObject, SecurityObject otherSecObject) {
//...
}

这很正常!?我需要到处使用@PreAuthorize("#secObject.equals(#otherSecObject) ") 吗?

更新

为什么在第一种情况下 Spring Security 调用 .equals() 而在第二种情况下不调用?

      //TestObject 

public class TestObject {

private static final Logger log = LoggerFactory.getLogger(TestObject.class);

private Long id;

public TestObject(Long id) {
this.id = id;
}

@Override
public int hashCode() {
int hash = 7;
hash = 71 * hash + Objects.hashCode(this.id);
return hash;
}

@Override
public boolean equals(Object obj) {

log.info("equals");

if (obj == null) {
return false;
}

if (getClass() != obj.getClass()) {
return false;
}
final TestObject other = (TestObject) obj;
if (!Objects.equals(this.id, other.id)) {
return false;
}
return true;
}
}

//TestService

@PreAuthorize(" #one == #two ")
public String testEqualsInAnnotation(Long one, Long two) {
//...
}

@Override
@PreAuthorize(" #one == #two ")
public String testEqualsInAnnotation(TestObject one, TestObject two) {
//...
}

//Test

log.info("for Long");
Long one = new Long(500);
Long two = new Long(500);

log.info("one == two: {}", (one==two)? true : false); // print false
log.info("one equals two: {}", (one.equals(two))? true : false); // print true

testService.testEqualsInAnnotation(one, two); //OK

log.info("for TestObject");

TestObject oneObj = new TestObject(new Long(500));
TestObject twoObj = new TestObject(new Long(500));

log.info("oneObj == twoObj: {}", (oneObj==twoObj)? true : false); // print false
log.info("oneObj equals twoObj: {}", (oneObj.equals(twoObj))? true : false); // print true

testService.testEqualsInAnnotation(oneObj, twoObj); // AccessDeniedException: Access is denied

更新 2

equals() 从未被调用过

           package org.springframework.expression.spel.ast;

import org.springframework.expression.EvaluationException;
import org.springframework.expression.spel.ExpressionState;
import org.springframework.expression.spel.support.BooleanTypedValue;

/**
* Implements equality operator.
*
* @author Andy Clement
* @since 3.0
*/
public class OpEQ extends Operator {

public OpEQ(int pos, SpelNodeImpl... operands) {
super("==", pos, operands);
}

@Override
public BooleanTypedValue getValueInternal(ExpressionState state) throws EvaluationException {
Object left = getLeftOperand().getValueInternal(state).getValue();
Object right = getRightOperand().getValueInternal(state).getValue();
if (left instanceof Number && right instanceof Number) {
Number op1 = (Number) left;
Number op2 = (Number) right;
if (op1 instanceof Double || op2 instanceof Double) {
return BooleanTypedValue.forValue(op1.doubleValue() == op2.doubleValue());
} else if (op1 instanceof Long || op2 instanceof Long) {
return BooleanTypedValue.forValue(op1.longValue() == op2.longValue());
} else {
return BooleanTypedValue.forValue(op1.intValue() == op2.intValue());
}
}
if (left!=null && (left instanceof Comparable)) {
return BooleanTypedValue.forValue(state.getTypeComparator().compare(left, right) == 0);
} else {
return BooleanTypedValue.forValue(left==right);
}
}

}

最佳答案

根据 spEL 文档,您需要创建 ExpressionParser 实例,创建一个 Expression 实例并获取如下值

String name = "Nikola Tesla";
Expression exp = parser.parseExpression("name == 'Nikola Tesla'");
boolean result = exp.getValue(Boolean.class);

结果评估为“真”。也就是说,当我们需要比较任意两个对象时,我们需要覆盖 equals() 方法并将两个对象传递给 parser#parseExpression("obj1 == obj2") 然后调用exp#getValue(Boolean.class) 进行评估。类似地,Expression 实例也可以有包含Obj1.equals(Obj2) 的表达式字符串,用于检查是否相等。因此,这两种检查相等性的方法都可以使用 spEL。

关于java - Spring Security 中的 Spring Expression Language (SpEL) 比较对象使用 equals() 或 ==?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19456377/

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