MethodInterceptor(스프링시큐리티 지원), Pointcut, Advisor, AuthorizationManager 등을 커스텀하게 생성하여 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);
}