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
在2021年7月9日星期五 UTC+8 上午12:03:49<jm> 写道:
> 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
>
--
- Website: https://apereo.github.io/cas
- Gitter Chatroom: https://gitter.im/apereo/cas
- List Guidelines: https://goo.gl/1VRrw7
- Contributions: https://goo.gl/mh7qDG
---
You received this message because you are subscribed to the Google Groups "CAS
Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/a/apereo.org/d/msgid/cas-user/f03b0a9f-7e0f-4327-a922-d0f09622b064n%40apereo.org.