@Retention(RetentionPolicy.RUNTIME)
// 타겟은 파라미터에만 붙이겠다.
@Target(ElementType.PARAMETER)
// 익명 사용자인 경우에는 null로, 익명 사용자가 아닌 경우에는 실제 account 객체로
// Principal 을 다이나믹 하게 꺼내기 위해 @CurrentUser 생성
@AuthenticationPrincipal(expression = "#this == 'anonymousUser' ? null : user")
public @interface CurrentUser {
}
@GetMapping("/test")
public AdvancedResponseBody<String> login(@CurrentUser User user) {
System.out.println(user);
return AdvancedResponseBody.of(Status.OK);
}
익명유저인 경우 null, 익명유저가 아닌경우 User 객체 return
https://kyu9341.github.io/java/2020/04/30/java_springBootLogin/
https://freedeveloper.tistory.com/217
//추가
@Retention(RetentionPolicy.RUNTIME)
// 타겟은 파라미터에만 붙이겠다.
@Target(ElementType.PARAMETER)
// 익명 사용자인 경우에는 null로, 익명 사용자가 아닌 경우에는 실제 account 객체로
// Principal 을 다이나믹 하게 꺼내기 위해 @CurrentUser 생성
@AuthenticationPrincipal(expression = "#this == 'anonymousUser' ? @userService.throwUser() : user")
public @interface CurrentUser {
}
헤더가 없을 때 에러를 던지기 위해 @userService.throwUser() 코드 추가
계속 방법을 찾다 안 돼서 스택오버플로우에 있는 방법 + 실행이 되는 방법을 조합해서 내가 만든 코드라 적절한지 모르겠다.
일단 헤더가 없으면 401 에러를 던지게 된다