Spring Securityでは、戻り値を組み込み、を使用するメソッドを保護したいと思います@PostAuthorize
。
1人のユーザーが所有者ではないリソースにアクセスできないようにする制約を追加したいと思います。私が直面している問題は、プリンシパルIDを1つの値のコレクションに対してチェックしたいということです。
シナリオ:
ドメインオブジェクト:
public class Car implements Serializable {
private Integer id;
private Collection<Driver> drivers;
...
}
public class Driver implements Serializable {
private Integer id;
...
}
サービス:
@PostAuthorize("hasRole('ROLE_ADMIN') or principal.id == returnObject.drivers.driver.id")
public Car getCar(int id) throws DAOException {
...
return carDAO.get(id);
}
もちろん、このスペル表現は機能しません。
SEVERE: El Servlet.service() para el servlet [dispatcher] en el contexto con ruta [] lanzó la excepción [Request processing failed; nested exception is java.lang.IllegalArgumentException: Failed to evaluate expression 'hasRole('ROLE_ADMIN') or principal.id == returnObject.drivers.driver.id'] con causa raíz
org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 42): Field or property 'driver' cannot be found on object of type 'org.eclipse.persistence.indirection.IndirectList'
コレクションで機能する例は見たことがありません。 この未解決の質問は似ていますが、私の特定のシナリオに一致するかどうかはわかりません。そのようなことをすることは可能ですか?それは私がやろうとしていることをする別の方法ですか?