You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
스프링이 @EnableGlobalMethodSecurity(prePostEnabled = true) 를 통해 MethodSecurityInterceptor 라는 AOP Advice를 생성
해당 애노테이션이 붙은 메서드는 프록시 객체로 감싸짐
메서드 호출 전에 권한을 평가하고, 조건이 안 맞으면 예외 발생 (AccessDeniedException)
@PreAuthorize("hasRole('ADMIN')")
public void deleteAccount(Long accountId) {
// 관리자만 이 메서드를 호출할 수 있음
}
@PreAuthorize("#username == authentication.name")
public void updateProfile(String username) {
// 로그인한 사용자 본인만 수정 가능
}
}
authentication.name: 현재 인증된 사용자명
#username: 메서드 파라미터
⚠️ 주의사항
@PreAuthorize는 반드시 프록시가 생성될 수 있는 위치에 선언해야 동작해요.
같은 클래스 내부 호출은 프록시를 우회해서 동작하지 않음
인터페이스 기반이면 JDK 프록시, 클래스 기반이면 CGLIB 프록시 사용
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
🧩 핵심 개념 요약
⚙️ 동작 방식: AOP 기반 프록시
스프링이 @EnableGlobalMethodSecurity(prePostEnabled = true) 를 통해 MethodSecurityInterceptor 라는 AOP Advice를 생성
해당 애노테이션이 붙은 메서드는 프록시 객체로 감싸짐
메서드 호출 전에 권한을 평가하고, 조건이 안 맞으면 예외 발생 (AccessDeniedException)
🔐 예제: @PreAuthorize
@service
public class AccountService {
}
authentication.name: 현재 인증된 사용자명
#username: 메서드 파라미터
@PreAuthorize는 반드시 프록시가 생성될 수 있는 위치에 선언해야 동작해요.
같은 클래스 내부 호출은 프록시를 우회해서 동작하지 않음
인터페이스 기반이면 JDK 프록시, 클래스 기반이면 CGLIB 프록시 사용
💬 쉽게 말하면?
"스프링 시큐리티는 우리가 직접 AOP Aspect를 짜지 않아도, 알아서 메서드 호출 전에 권한을 체크하는 프록시를 만들어주는 것!"
Beta Was this translation helpful? Give feedback.
All reactions