Filters and/or Servlets cannot be added to context [/openmrs] as the context has been initialised

(ICYMI: The solution to this thread was this ticket, which is done :slight_smile: [TRUNK-4673] - OpenMRS Issues )

Hello @ibacher @ruhanga

While We were trying to integrate keycloak with openmrs module for authentication , we are facing below issue as mentioned in the same thread.Please find the further details for reference and let us know if you have any idea or leads on resolving this issue

OpenMRS version : 2.5x Oauth2LoginModule :1.6.0

Error Trace: java.lang.IllegalStateException: Filters cannot be added to context [/openmrs] as the context has been initialised at org.apache.catalina.core.ApplicationContext.checkState(ApplicationContext.java:1269) at org.apache.catalina.core.ApplicationContext.addFilter(ApplicationContext.java:779) at org.apache.catalina.core.ApplicationContext.addFilter(ApplicationContext.java:761) at org.apache.catalina.core.ApplicationContextFacade.addFilter(ApplicationContextFacade.java:434) at org.openmrs.web.WebComponentRegistrar.setServletContext(WebComponentRegistrar.java:39) at org.springframework.web.context.support.ServletContextAwareProcessor.postProcessBeforeInitialization(ServletContextAwareProcessor.java:108) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:415) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1791) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516) at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:324) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:897) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:879) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:551) at org.openmrs.module.ModuleUtil.refreshApplicationContext(ModuleUtil.java:885) at org.openmrs.module.web.WebModuleUtil.refreshWAC(WebModuleUtil.java:844) at org.openmrs.web.Listener.performWebStartOfModules(Listener.java:661) at org.openmrs.web.Listener.performWebStartOfModules(Listener.java:641) at org.openmrs.web.Listener.startOpenmrs(Listener.java:307) at org.openmrs.web.WebDaemon$1.run(WebDaemon.java:42) INFO - ServiceContext.doneRefreshingContext(834) |2025-09-15T06:27:04,203| Done refreshing Context

Hi @sasirekha, welcome to the community.

While there was a fix done in core 2.7.0 as indicated and shared above, on the ticket TRUNK-4673, it’s likely that that fix was never leveraged by the Oauth 2 Login module. This means that for both, the latest and any released versions of the module, you probably still need to restart the OpenMRS web application to solve the problem. This would always be needed at-the-least, whenever a new instance is started for the very first time.

Hello @ruhanga

We have restarted open mrs web application multiple times and while accessing the open mrs, I’m getting below error from UI and logs are also same from the backend.Do you have any idea on this?

Just ignore that error. It shouldn’t affect you after the restart.

Hello @dkayiwa

We have restarted open mrs web application multiple times and while accessing the open mrs, I’m getting below error from UI and logs are also same from the backend.Do you have any idea on this?

Error from UI:

Hi @ruhanga @ibacher @mksd @icrc.thonorio @dkayiwa We are not able to bring up the Oauth2 Login module with OMRS 2.5.14.

  • We tried restarting the container, but still the servlet and filter does not seem to get applied.
  • We also tried restarting the OpenMRS app alone inside the container, but doesn’t help

We would also like to understand why the CustomDispatcherServlet is initialized through Component scan rather than through module config.xml ?

We would also like to understand if the fix that has been done as part of TRUNK-4673 can be backported into 2.5.x ?

@mohant, @sasirekha are you able to run the OAuth2 Login module with OpenMRS version 2.6.x or newer though? That would help determine whether the issue is specific to 2.5.x and needs to be addressed. Else there’d be some misconfigurations preventing successful deployment of the module.