我正在開(kāi)發(fā)一些社交系統(tǒng),我想做一些自動(dòng)將 loggedUser 注入方法參數(shù)的注釋。我試過(guò)使用 AOP 但官方文檔說(shuō):返回類(lèi)型僅限于原語(yǔ)、字符串、類(lèi)、枚舉、注釋和上述類(lèi)型的數(shù)組。方法可以有默認(rèn)值。public void getLoggedUserDetails( @CurrentUser User user){//some logic}是否可以使用 Aspects 和注釋來(lái)做到這一點(diǎn),或者我應(yīng)該尋找另一種解決方案?
1 回答
繁星點(diǎn)點(diǎn)滴滴
TA貢獻(xiàn)1803條經(jīng)驗(yàn) 獲得超3個(gè)贊
有一個(gè)@AuthenticationPrincipal,它適用于控制器方法參數(shù)。
@Controller
class UserController {
? ? @GetMapping("/self")
? ? public ResponseEntity<User> demo(@AuthenticationPrincipal User user){
? ? ? ? return new ResponseEntity<>(user, HttpStatus.OK);
? ? }
您還可以創(chuàng)建自定義注釋?zhuān)?/p>
@Target({ElementType.PARAMETER, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@AuthenticationPrincipal
public @interface CurrentUser {}
添加回答
舉報(bào)
0/150
提交
取消
