2011-12-29 64 views
0

在我的Spring配置文件我有<global-method-security pre-post-annotations="enabled"/>@PreAuthorize調用一個方法,但不是另一個

在我的春天@Controller我有上有一個@PreAuthorize如下一個@RequestMapping:

@PreAuthorize("true == false") 
@RequestMapping(value="/image", method=RequestMethod.GET) 
@ResponseBody 
public ResponseEntity<byte[]> getImage(
     @RequestParam(value="imageSet", required=false) Long imageSetKey 
     , @RequestParam(required=false, defaultValue="70") Integer size 
     , @RequestParam(required=false) Unit unit 
     , @RequestHeader(value="if-none-match", required=false) String etag 
) 
{ 
    // use the latest and greatest for the unit if they specify the unit, otherwise use the imageSetKey they pass in. 
    if (unit != null) 
    { 
     return getUnitImage(unit, size, etag); 
    } 
    // more code to do other stuff 
} 

現在這個@PreAuthorize被評估並正常工作。如果我在getUnitImage方法上放置PreAuthorize,那麼它不會被評估,我進入該方法就好了。以下是不評估@PreAuthorize的方法。

@PreAuthorize("true == false") 
public ResponseEntity<byte[]> getUnitImage(Unit unit, int size, String etag) 
{ 
    // do stuff, I get into breakpoints here. 
} 

上爲什麼會預刷上一個方法調用與RequestMapping思考,而不是別人在同一個班?

春3.0.5,春季安全3.0.3

回答

2

這是由Spring AOP & CGLIB的工作方式引起的。具體來說,將創建一個代理類來擴展實際的類,這就是@PreAuthorize行爲的實現。當您從同一個類中調用方法時,它不會通過代理類,因此不會執行所需的行爲。

解決此問題的一種方法是使用AspectJ代替,在您的Spring配置中爲您的global-method-security元素添加mode =「aspectj」。

這也恰好與@Transactional,特別約@Transactional另一個問題有關於這個問題的一些細節:

Spring @Transaction method call by the method within the same class, does not work?

+0

感謝這個信息。隨着一些爭論,大量的測試,以及對Spring 3.1的升級,我得到了一切工作。也感謝@Transactional的負責人。 – digitaljoel 2012-01-07 07:35:09

1

是如何getUnitImage方法被調用?

確保您調用它的方式允許代理(即爲註釋創建的代理)攔截該調用。

相關問題