2017-04-24 124 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