[스프링시큐리티] AOP 메소드 보안

Welcome to Seoyun Dev Log·2024년 12월 17일
0

보안

목록 보기
12/18

MethodInterceptor(스프링시큐리티 지원), Pointcut, Advisor, AuthorizationManager 등을 커스텀하게 생성하여 AOP 메서드 보안을 구현 할 수 있다

  • 순수한 AOP 기술만 가지고도 메서드 보안을 구현할 수 있는 것
  • joinpoint: Advice를 적용할 수 있는 대상 자체를 의미한다. (클래스, 필드, 메서드 중 어느곳에 적용할 것인지 만약 클래스 단위로 적용한다면 joinpoint는 클래스가 되는 것)
    단, 스프링에서는 메서드에만 Advice를 적용할 수 있도록 한정한다 따라서 joinpoint 또한 메서드에 한정.
  • joinpoint 대상에서 실행되어야 하는 지점, 조건이 pointCut이 된다.

초기화

  • CGLibAopProxy : 프록시 객체를 생성하는 객체

구현 예제

  • MethodInterceptor를 구현받을 때 aop로 구현
public class CustomMethodInterceptor implements MethodInterceptor {
private final AuthorizationManager<MethodInvocation> authorizationManager;
public CustomMethodInterceptor(AuthorizationManager<MethodInvocation> authorizationManager) { this.authorizationManager=authorizationManager; //메서드보안검사를수행할인가관리자를전달한다
}
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); 
if(//권한 심사 
authorizationManager.check(() ->authentication, invocation).isGranted()) {
returninvocation.proceed(); //실제대상객체를호출한다 }else{
throw new AccessDeniedException("Access Denied"); }
}
}

@Bean
public MethodInterceptor customMethodInterceptor() {
AuthorizationManager<MethodInvocation> authorizationManager = AuthenticatedAuthorizationManager.authenticated(); return new CustomMethodInterceptor(authorizationManager); // AOP 어라운드 어드바이스를 선언한다
}

@Bean
public Pointcut servicePointcut() {
AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
pointcut.setExpression("execution(* io.security.Myservice.*(..))"); // AOP 수행 대상 클래스와 대상 메소드를 지정한다 return pointcut;
}

@Bean
public Advisor serviceAdvisor(MethodInterceptor customMethodInterceptor, Pointcut servicePointcut) { // 초기화 시 Advisor 목록에 포함된다 return new DefaultPointcutAdvisor(servicePointcut, customMethodInterceptor);
}
  • DefaultPointcutAdvisor : 스프링시큐리티에서 기본적으로 생성하는 Advisor
    Pointcut, MethodInterceptor를 담고 있다
profile
하루 일지 보단 행동 고찰 과정에 대한 개발 블로그

0개의 댓글

관련 채용 정보