2017-04-24 125 views
1

我使用自定义方法为我的春季安全pre授权批注,我需要传递一长串烫发。我想外部存储该列表,因为它在一些地方使用,但我可以似乎不知道如何参考上述清单。它似乎总是通过为空。SpEL参考实例变量的类

@RestController 
@RequestMapping("/example") 
public class MyController { 

...constructor/other stuff 
public List<String> perms_I_want_to_reference = Arrays.asList("super","long","list") 

@PreAuthorizze("@securityService.MyCustomMethod(principal, *this where I want to reference perms*) 
@RequestMapping(method = RequestMethod.GET) 
public ResponseEntity<?>doSomethingTopSecret(){ 
} 

} 

我已经尝试#和制作列表的静态和使用T但到目前为止,没有什么工作。

+0

Whay你需要通过他们?你不能只从securityService.MyCustomMethod访问它们吗? –

+0

所以很多控制器使用该服务,并且不知道谁在呼叫它。这是真的我想我可以重构以某种方式知道或接收谁在呼叫。我得到这个工作,使烫发清单静态和使用T,但想知道如果有人知道如果/如何/当我不能做我想要的 – Barry

回答

1

从注释中访问字段的唯一方法是通过反射。为了做到这一点,Spring需要访问该类的领域。我没有听说过的方法来获得当前类的引用时表达正在评估,但一个方式做你想要的是引用一个bean本身并访问现场:

public List<String> perms_I_want_to_reference = Arrays.asList("super","long","list"); 

@PreAuthorizze("@securityService.MyCustomMethod(principal, @myController.perms_I_want_to_reference)") 
@RequestMapping(method = RequestMethod.GET) 
public ResponseEntity<?>doSomethingTopSecret(){ } 
+0

以上,所以我发现我可以使用'T'完整的包名称和访问静态属性该类,但结束了类似于你的解决方案,我用另一个刚刚加载常量的bean,因为我觉得它读得最好。完全限定的软件包和变量名称对于我来说是冗长的。 – Barry