Announcement Announcement Module
Collapse
No announcement yet.
Autowire AfterInvocationProviders Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Autowire AfterInvocationProviders

    I am using the namespace configuration to setup Spring security like so:

    Code:
    <security:global-method-security secured-annotations="enabled" access-decision-manager-ref="accessDecisionManager"/>
    I have several custom implementations of AfterAuthenticationProvider which I want Spring Security to poll during the interception of method invocations.

    I can see that you can register additional beans by nesting a "after-invocation-provider" element within the "global-method-security" element. However, I want to autowire these by type. I don't see a way to configure this. Is there a way?

  • #2
    You should be able to do this using a BeanPostProcessor as outlined in the FAQ. You will want to modify the MethodSecurityInterceptor to have an AfterInvocationProviderManager with your AfterInvocationProvider's added to it.

    Comment


    • #3
      I really appreciate the quick response and the helpful information. Here is my solution in case others run across the same need.


      Code:
      public class MyAfterInvocationProviderBeanPostProcessor implements BeanPostProcessor, ApplicationContextAware
      {
          private ApplicationContext applicationContext;
      
          @Override
          public Object postProcessAfterInitialization(Object bean, String name) throws BeansException
          {
              return bean;
          }
      
          @Override
          public Object postProcessBeforeInitialization(Object bean, String name) throws BeansException
          {
              if (bean instanceof MethodSecurityInterceptor)
              {
                  AfterInvocationProviderManager mgr = new AfterInvocationProviderManager();
                  mgr.setProviders(new ArrayList<AfterInvocationProvider>(
                          BeanFactoryUtils
                                  .beansOfTypeIncludingAncestors((ListableBeanFactory) applicationContext, AfterInvocationProvider.class)
                                  .values()));
                  ((MethodSecurityInterceptor) bean).setAfterInvocationManager(mgr);
              }
              return bean;
          }
      
          public void setApplicationContext(ApplicationContext applicationContext)
          {
              this.applicationContext = applicationContext;
          }
      
      }
      If you see any issues with this code or if you see a better way to do it please let me know.

      Comment

      Working...
      X