dig*_*oel 1 java spring spring-security
在我的spring配置文件中 <global-method-security pre-post-annotations="enabled"/>
在我的春季@Controller中,我有一个@RequestMapping,其上具有一个@PreAuthorize,如下所示:
@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
}
Run Code Online (Sandbox Code Playgroud)
现在,此@PreAuthorize已评估并且可以正常工作。如果将PreAuthorize放在getUnitImage方法上,则不会对其进行评估,并且我可以很好地进入该方法。这是不评估@PreAuthorize的方法。
@PreAuthorize("true == false")
public ResponseEntity<byte[]> getUnitImage( Unit unit, int size, String etag )
{
// do stuff, I get into breakpoints here.
}
Run Code Online (Sandbox Code Playgroud)
关于为什么要使用RequestMapping在一个方法上调用PreAuthorize而不在同一个类中的其他方法上调用PreAuthorize的想法?
Spring 3.0.5,Spring Security 3.0.3
这是由Spring AOP和CGLIB的工作方式引起的。具体来说,将创建一个代理类来扩展您的实际类,从而提供@PreAuthorize行为的实现。当您从同一类调用方法时,该方法不会通过代理类,因此不会执行所需的行为。
解决此问题的一种方法是改为使用AspectJ,方法是在Spring配置中将mode =“ aspectj”添加到您的global-method-security元素中。
@Transactional也会发生这种情况,另外一个专门有关@Transactional的问题提供了有关此问题的更多详细信息:
Spring @Transaction方法由同一类中的方法调用,不起作用吗?
| 归档时间: |
|
| 查看次数: |
1695 次 |
| 最近记录: |