Cannot use AOP in cas-overlay 5.3.14

32 views
Skip to first unread message

jm

unread,
Jul 8, 2021, 12:03:49 PM7/8/21
to CAS Community
Hello everyone,

I am trying to customize CAS client IP address obtaining process, for example, try get IP
address from custom HTTP header "X-IP", if not found then try "X-Forwarded-For", finally
use underlying TCP/IP connection address (aka. request.getRemoteAddr()).

I extended ClientInfo class to acheive the target I talked above, next step is to set instance
of CustomizedClientInfo to ClientInfoHolder in the filter. I found the FilterRegistrationBean
of ClientInfoThreadLocalFilter in org.apereo.cas.audit.spi.config.CasCoreAuditConfiguration,
but there is no ConditionalOnMissingBean annotation on it, thus I cannot prevent it from
registering the ClientInfoThreadLocalFilter.

Now I am thinking about another way to solve the problem. I want to hijack the doFilter method
of ClientInfoThreadLocalFilter in AOP, like this:

@Aspect
public class ClientInfoThreadLocalFilterAspect {
    @Around("execution(* org.apereo.inspektr.common.web.ClientInfoThreadLocalFilter.doFilter(..))")
    public Object hijackClientInfoGeneration(ProceedingJoinPoint joinPoint) throws Throwable {
        final Object[] args = joinPoint.getArgs();
        Assert.isTrue(args.length == 3, "");

        final ServletRequest request = (ServletRequest) args[0];
        final ServletResponse response = (ServletResponse) args[1];
        final FilterChain filterChain = (FilterChain) args[2];

        try {
            final ClientInfo clientInfo = new CustomizedClientInfo((HttpServletRequest) request);
            ClientInfoHolder.setClientInfo(clientInfo);
            filterChain.doFilter(request, response);
        } finally {
            ClientInfoHolder.clear();
        }

        return null;
    }
}

And I instantiate it in my configuration class:

@EnableAspectJAutoProxy
@Configuration
public class CustomizedCasAuditConfiguration implements AuditTrailExecutionPlanConfigurer {
    @Bean
    public ClientInfoThreadLocalFilterAspect clientInfoThreadLocalFilterAspect() {
        return new ClientInfoThreadLocalFilterAspect();
    }
}

I am sure this configuration class is used by spring, because I have another bean in this
class which works normally. Then Set a breakpoint in the first line of hijackClientInfoGeneration
and run in debug mode, send a request, but nothing happened. I have tried to add  spring-boot-starter-aop
in build.gradle but nothing different happens.

Is there any trick I forgot when using AOP in cas-overlay?

By the way, I want to submit a patch to make this filter easier to cusomtize, will that be accept
by the maintainer?

James

jm

unread,
Jul 9, 2021, 8:33:01 AM7/9/21
to CAS Community, jm
Hi all,

Finnally I found out the solution.

The way I mentioned in the first email is not possible to work from start,
because Spring AOP only works when your pointcut is on a Spring bean.
ClientInfoThreadLocalFilter is registered by an instance of  
FilterRegistrationBean so the instance is actually Spring bean. How to  
make this work? Answer is do NOT use Spring AOP, use original aspectj,
make a aop.xml in META-INF and run your jar with a aspectj java agent,
which may bring a lot of trouble.

My colleague told me another way to solve this problem. Spring provides
some hooks in the lifecycle of beans. BeanFactoryPostProcessor is one of
them, just do it like this:

@Bean
public static BeanFactoryPostProcessor
    removeClientInfoThreadLocalFilterRegistrationProcessor() {

    return (ConfigurableListableBeanFactory beanFactory) -> {
    ¦   final BeanDefinitionRegistry registry =
    ¦   ¦   (BeanDefinitionRegistry) beanFactory;

    ¦   if (registry.containsBeanDefinition("casClientInfoLoggingFilter")) {
    ¦   ¦   registry.removeBeanDefinition("casClientInfoLoggingFilter");
    ¦   }   

    ¦   log.info("casClientInfoLoggingFilter is removed from bean factory");
    };  

}
 
then just insert customized Filter like what you did to every common Filter.
You will find casClientInfoLoggingFilter disappeared in your filter chain.

Hope this will help someone.


James
Reply all
Reply to author
Forward
0 new messages