Skip to content

Comments

feat: 권한에 따른 접근 권한 필터링 및 통합 테스트 코드 작성#22

Merged
joungGo merged 6 commits intodevelopfrom
joungGo/feat-5
Apr 16, 2025
Merged

feat: 권한에 따른 접근 권한 필터링 및 통합 테스트 코드 작성#22
joungGo merged 6 commits intodevelopfrom
joungGo/feat-5

Conversation

@joungGo
Copy link
Owner

@joungGo joungGo commented Apr 15, 2025

카테고리 구현 방법 설명 예시 코드
어노테이션 정의
메타 어노테이션 @Target, @Retention - 적용 대상 지정
- 어노테이션 유지 기간 설정
java<br>@Target(ElementType.METHOD)<br>@Retention(RetentionPolicy.RUNTIME)
권한 검증 어노테이션 @PreAuthorize와 결합 - Spring Security의 권한 검증 활용 java<br>@PreAuthorize("hasRole('ADMIN')")<br>@RequireAdmin
AOP 구현
Aspect 정의 @Aspect - AOP 클래스 선언
- 포인트컷과 어드바이스 정의
java<br>@Aspect<br>@Component
포인트컷 정의 @Pointcut - 어노테이션이 적용된 메서드 지정 java<br>@Pointcut("@annotation(com.example.RequireAdmin)")
어드바이스 구현 @Before, @Around - 메서드 실행 전/후 처리
- 권한 검증 로직 구현
java<br>@Before("adminPointcut()")<br>public void checkAdmin()
권한 검증 로직
SecurityContext 접근 SecurityContextHolder - 현재 인증 정보 조회
- 권한 정보 확인
java<br>Authentication auth = SecurityContextHolder.getContext().getAuthentication();
권한 검증 hasRole(), hasAuthority() - 역할 기반 검증
- 권한 기반 검증
java<br>auth.getAuthorities().stream()<br> .anyMatch(a -> a.getAuthority().equals("ROLE_ADMIN"))
예외 처리
권한 부족 예외 AccessDeniedException - 권한이 없는 경우 예외 발생 java<br>throw new AccessDeniedException("권한이 없습니다.");
예외 처리 Aspect @AfterThrowing - 권한 관련 예외 처리
- 적절한 응답 생성
java<br>@AfterThrowing(pointcut = "adminPointcut()",<br> throwing = "ex")
테스트
AOP 테스트 @SpringBootTest - AOP 동작 테스트
- 권한 검증 테스트
java<br>@Test<br>@WithMockUser(roles = "USER")
Mock 객체 사용 @MockBean - SecurityContext 모킹
- 권한 정보 모킹
java<br>@MockBean<br>SecurityContext securityContext

@joungGo joungGo linked an issue Apr 15, 2025 that may be closed by this pull request
@joungGo joungGo self-assigned this Apr 15, 2025
@joungGo joungGo added backend 백엔드 이슈 test 테스트 코드 이슈 feat 기능 labels Apr 15, 2025
@joungGo joungGo merged commit f0c9c8b into develop Apr 16, 2025
@joungGo joungGo deleted the joungGo/feat-5 branch April 16, 2025 05:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend 백엔드 이슈 feat 기능 test 테스트 코드 이슈

Projects

None yet

Development

Successfully merging this pull request may close these issues.

권한 및 역할 관리 (USR-004)

1 participant