I'm currently loading the urls section from the "config" parameters for org.apache.shiro.web.servlet.IniShiroFilter in my web.xml.
I'd like to be able to load these from the database, as I have the ability to make dynamic content and routes in my system. (i.e. I can dynamically make and intercept /My/Url/1345/index.xhtml in the app)
Is there an easy way of doing this, or do I need to write a custom Filter?
The ShiroFilter uses a FilterChainResolver to resolve the FilterChain
to execute based on a given request. This allows FilterChains to be
resolved dynamically at runtime. By default, they are resolved based
on static definitions in INI, but they could very easily be resolved
The default implementation of the FilterChainResolver is a
PathMatchingFilterChainResolver. It looks at the URL path on the
incoming request and then calls into a FilterChainManager to get a
corresponding FilterChain for the matched path.
The DefaultFilterChainManager implementation uses an internal
in-memory Map to manage the path-to-chain configuration. When using
INI config, the map is populated based on the [urls] INI section.
I think it'd be a relatively easy exercise to implement the
FilterChainManager interface to talk to a datasource instead of an
in-memory map. You can set your implementation as a property on the
PathMatchingFilterChainResolver instance, and you should be good to
If you're using a DI/IoC framework like Spring or Guice this should be
really simple. If you're not, you might have to subclass the
IniShiroFilter (or AbstractShiroFilter) to set up the
FilterChainResolver and its nested FilterChainManager instance
directly. If you have to do that, the IniShiroFilter source code will
give you some good ideas:
In fact I've went to shiro in order to simplify and remove spring (and its security module). Guice is more appealing (as I also liked pico in the early days of IoC) I'm holding off until I really, really need to.
So, I want to ensure a fairly easy upgrade path for shiro, so will try to keep my intervention to a minimum
In short I'm going to write either:
An adaptation of the classes as suggested below (basically wrap, lookup and delegate to the DefaultFilterChainManager and IniShiroFilter),
Decorate the Ini class at instantiation in the IniShiroFilter to allow loading from other sources (which is pretty much done with the configure() method. Hmm, this maybe less clean semantically, but it'll get me there quicker...
Give me a shout if you want any of the code (it might be kinda throw away, as I'm not using a JDBC database)