java 板


LINE

※ 引述《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。 --



※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 218.173.236.72
※ 文章网址: http://webptt.com/cn.aspx?n=bbs/java/M.1399313711.A.CA1.html
1F:推 ghchen:感谢S大的意见,我会朝你说的方向去试试看 05/06 09:19
2F:推 dream1124:请问第二段可以举更具体的例子吗? 很有意思 05/06 19:08
3F:推 swpoker:但只能从runtime来侦测吧~顶多只在test case那边测测 05/08 11:43
第二段的部分在实作上应该还挺直觉的,所以我才没有直接给出范例。 如果要给个具体的例子,那就拿原帖中应用为例。假设所有 Repository 相关的 classes 都在 package demo.repositories 之下(含 subpackage);所有 Service 相关的 classes 都在 package demo.services 之下(含 subpackage)。 不能直接碰 repository classes 的原则是指,除了 repository classes 与 services classes 之外的所有 class 都不能够直接去调用 repository classes 的任何 method,也不能存取任何 repository classes 的 field。 要强制遵守这个原则大致上可以这麽做(假设违例处抛出一个带有提示讯息的 runtime exception 即可达到成效): public aspect DontTouchModel { pointcut manipulateRepository() : call(* demo.repositories..*.*(..)); pointcut readFieldOfRepository() : get(* demo.repositories..*.*); pointcut writeFieldOfRepository() : set(* demo.repositories..*.*); before() : manipulateRepository() && !within(demo.repositories..*) && !within(demo.services..*) { throw new RuntimeException( "Please don't touch repositories classes directly."); } before() : readFieldOfRepository() && !within(demo.repositories..*) && !within(demo.services..*) { throw new RuntimeException( "Please don't touch repositories classes directly."); } before() : writeFieldOfRepository() && !within(demo.repositories..*) && !within(demo.services..*) { throw new RuntimeException( "Please don't touch repositories classes directly."); } } 把整个 project 的 classes 拿来与 DontTouchModel 织在一起後,非 repository/ service classes 者不论如何拿到 repository classes 的 object/class,只要 他有把 reference casting 成 repository classes/interface 来使用,就会被 揪出来(除非他透过 reflection 去使用这些 reference,这就是第三段的部分)。 这种做法对於那些符合原则的 statement 不会添加 runtime 成本(因为这些码完全 没有被加工),只有不符合原则的部分有额外成本(其实也没有啦,你根本就要他挂掉 了 :D)。 ※ 编辑: sbrhsieh (1.172.225.226), 05/09/2014 17:14:33
4F:推 ghchen:再次感谢S大的解说!! 05/10 09:46







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:BabyMother站内搜寻

TOP