1. 서론
회원이 로그인은 한 후 사이트를 이용하는 동안 그 회원이 로그인 정보를 계속 가지고 활용해야 한다.
코드를 짤때 마다 계속 회원 정보를 가지고 와야하는데 같은 코드를 반복 해야 한다.
이것이 불 필요하게 느껴저 로그인 한 회원 정보를 가져오기 위해 어노테이션으로 구현하여 쉽게 활용하자.
2. 코드
package com.mingkyy.MyDiary.oauth;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ ElementType.PARAMETER, ElementType.ANNOTATION_TYPE })
@Retention(RetentionPolicy.RUNTIME)
@AuthenticationPrincipal(expression = "#this == 'anonymousUser' ? null : member")
public @interface CurrentMember {
}
3. 설명
@Target
- 어노테이션이 적용되는 위치한다.
- ElementType.PARAMETER : 파라미터
- ElementType.ANNOTATION_TYPE : 어노테이션 타입
@Retention
- 어노테이션이 적용될 범위로 어떤 시점까지 어노테이션이 영항을 미치는지 결정한다
- RetentionPolicy.RUNTIME : 클래스 파일에 포함됨, JVM이 로드해서 리플렉션 API로 참조 가능하다.
@AuthenticationPrincipal
- 로그인 한 사용자 정보를 받아 올 수있다.
- (expression = "#this == 'anonymousUser' ? null : member" )
: 익명 유저이니? 즉 로그인한 사용자가 없다면 null 값을 return 해주고 있다면 member로 return 해준다.
'Springboot' 카테고리의 다른 글
[Sprong Boot]Swagger 사용하기 (0) | 2023.02.02 |
---|---|
[Spring Boot]OAuth2 (NAVER, KAKAO, GOOGLE LOGIN) (0) | 2023.01.31 |
Spring Security적용, WebSecurityConfigurerAdapte 문제 해결 (0) | 2023.01.10 |
Repository 생성, Repository TestCode 작성하기 (0) | 2023.01.09 |
Jpa Domain 작성하기 (0) | 2023.01.09 |