2014-01-17 58 views
0

如何在建議之前使用Spring AOP中的安全措施。Java - Spring AOP - 使用before建議停止執行方法?

例如,我的建議(以僞代碼)之前有這樣的:

@Before("trigger()") 
public void beforeMethod(JoinPoint point){ 
    Method[] a = point.getSignature().getClass().getDeclaredMethods(); 
    for(int i=0; i < a.length; i++){ 
      //if method has such arguments then 
     if(a[i] 'has args String name, String role, Int Money'){ 
       //And if these arguments meets such requirements 
       if(a[i].argument(Int.class) > 1000 or a[i].argument(String role).equals("normal")) 
        //Stop executing this method 
        a[i].stop 
       } 
    } 
} 

我知道這只是僞代碼,因此它可能看起來不正確的,但我希望你拿個主意。如果符合或不符合某些要求,是否有一些JoinPoint方法停止掃描的方法?

+2

如果它是一種安全措施會引發異常。 –

+0

你可以選擇不調用'proceed()'。 –

回答

4

如何拋出一個異常?

if (!isAllowed) { 
    throw new NotAuthorizedException(..) 
} 
0

其實你不行。 http://docs.spring.io/spring/docs/current/spring-framework-reference/html/aop.html

@Before("trigger()") 
public void beforeMethod(JoinPoint point){ 
    Method[] a = point.getSignature().getClass().getDeclaredMethods(); 
    for(int i=0; i < a.length; i++){ 
      //if method has such arguments then 
     if(a[i] 'has args String name, String role, Int Money'){ 
       //And if these arguments meets such requirements 
       if(a[i].argument(Int.class) > 1000 or a[i].argument(String role).equals("normal")) 
        throw new Exception("Operation Not allowed"); 
       } 
    } 
} 
+0

該鏈接中的具體內容表明您不能?你的代碼示例的要點是什麼,是不是有效的,或者是一種解決方法來實現問題中所需的行爲? –

+0

這是解決方案: –