我正在開發(fā)一些社交系統(tǒng),我想做一些自動將 loggedUser 注入方法參數(shù)的注釋。我試過使用 AOP 但官方文檔說:返回類型僅限于原語、字符串、類、枚舉、注釋和上述類型的數(shù)組。方法可以有默認(rèn)值。public void getLoggedUserDetails( @CurrentUser User user){//some logic}是否可以使用 Aspects 和注釋來做到這一點,或者我應(yīng)該尋找另一種解決方案?
1 回答

繁星點點滴滴
TA貢獻1803條經(jīng)驗 獲得超3個贊
有一個@AuthenticationPrincipal
,它適用于控制器方法參數(shù)。
@Controller
class UserController {
? ? @GetMapping("/self")
? ? public ResponseEntity<User> demo(@AuthenticationPrincipal User user){
? ? ? ? return new ResponseEntity<>(user, HttpStatus.OK);
? ? }
您還可以創(chuàng)建自定義注釋:
@Target({ElementType.PARAMETER, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@AuthenticationPrincipal
public @interface CurrentUser {}
添加回答
舉報
0/150
提交
取消