预授权方法堆栈



在另一个方法中调用预授权方法时,是将@PreAuthorize添加到调用方更好,还是我们应该使用 Spring applicationContext 调用它。我在一个例子中看到了第二种方式。接下来是代码片段,哪个更好,为什么?

方法 1

@PreAuthorize(...)
public List<String> methodA(args) {
}
@PreAuthorize(...)
public List<String> methodB(args, extraArgs) {
    List<String> aList = methodA(args);
    // Modify aList
    return aList;
}

方法 2

@PreAuthorize(...)
public List<String> methodA(args) {
}
// This will be authorized when we call methodA
public List<String> methodB(args, extraArgs) {
    ThisClass springProxy = applicationContext.getBean(ThisClass.class);
    List<String> aList = springProxy.methodA(args);
    // Modify aList
    return aList;
}

因为 Spring 使用代理定义方面。使用方法 1,当您从方法 B 调用方法 A 时,您将在代理内部。然后,Spŕing 添加的所有方面都超出了范围。您需要将预授权注释应用于定义 API 的所有方法。

最新更新