※ 引述《ghchen (Seele)》之銘言:
: 主要問題:
: 如何禁止從 Controller 直接呼叫 Repository
: 問題描述:
: 我使用 spring 來開發 webapp
: 採用三層式架構
: Controller -> Service -> Repository
: // Repository 是 Spring Data 使用的術語,可以想像成 DAO
: 原本打算把 jta, auditing log 加在 Service layer 上
: 所以我不希望其他開發人員從 Controller 這裡直接呼叫 Repository
: 但是其他開發人員不可能被我控制
: 他們可以隨意的在 controller 裡面任意 autowired repository
: 接下來就隨便他們搞了
: 後來我加了一個 AOP 來限制 repository 的呼叫
: 如果是從 service package 呼叫的就放行,否則拋出例外
: @Aspect
: public class ModelAdvice {
: private Pattern pattern = Pattern.compile("^demo\\.services\\..*");
: @Before("execution(* demo.repositories..*Repository.*(..))")
: public void protectRepositories(JoinPoint joinPoint) {
: StackTraceElement[] stElements =
: Thread.currentThread().getStackTrace();
: for (StackTraceElement element : stElements) {
: Matcher matcher = pattern.matcher(element.getClassName());
: if (matcher.matches()) {
: return;
: }
: }
: throw new RuntimeException("security violation!");
: }
: }
: 雖然可以動,但是覺得不太漂亮,而且可能會很慢
使用 AOP 來作 policy enforcement 是不錯的選擇,但是應該就不要在 runtime
再去分析 runtime 時的資訊。
試著透過複合 within pointcut 直接把不是在 service classes 範圍下,去 call
repository methods 的部份織上拋出 exception 的碼,等於是在 weaving time 就
直接把不合規定的部份找出來,沒有額外 runtime 成本。
另外,需要考慮使用 reflection 去調用 repository classes 的功能,這部份也
可以透過 AspectJ 去攔截,但是就確實需要在 runtime 去判斷是否有符合你的
policy。