Hi,
I have been struggling with getting Shiro working in another Listener in
Tomcat. I finally got it working so I am writing what I found here.
Within web.xml I have the usual EnvironmentLoaderListener
<listener>
<listener-class>org.apache.shiro.web.env.EnvironmentLoaderListener</listener-class>
</listener>
I also have another Listener
<listener>
<listener-class>org.somethingelse.OtherClass</listener-class>
</listener>
Any code executed by OtherClass couldn't get a Subject because the
ThreadContext.getSecurityManager returned null. Whenever you executed
SecurityUtils.getSubject() you would get the following exception
org.apache.shiro.UnavailableSecurityManagerException: No SecurityManager
accessible to the calling code, either bound to the
org.apache.shiro.util.ThreadContext or as a vm static singleton. This is an
invalid application configuration.
To resolve I had to populate the ThreadContext in OtherClass,
Within OtherClass:contextInitialized
WebEnvironment wm =
WebUtils.getRequiredWebEnvironment(servletContextEvent.getServletContext());
WebSecurityManager wsm = wm.getWebSecurityManager();
ThreadContext.bind(wsm);
and in OtherClass:contextDestroyed
ThreadContext.unbindSecurityManager();
ThreadContext.remove();
Now any calls to SecurityUtils.getSubject() would return a Subject.
Hope this may help someone in future.
Now the thought has crossed my mind that I have missed some vital part of
the documentation that would make what I have done above unnecessary. If so
please let me know what it is.
Thanks,
S.
--
View this message in context:
http://shiro-user.582556.n2.nabble.com/Implementing-Shiro-in-a-Listener-Documentation-Submission-tp7502910p7502910.html
Sent from the Shiro User mailing list archive at Nabble.com.