I am trying to use Apache Shiro with Spring and MongoDB. I use Spring Data Warehouses, which are auto-updated. I created my own custom world for Shiro, which uses the Spring datastore to talk to Mongo:
public class PlatformRealm extends AuthorizingRealm { @Autowired(required = true) protected UserRepository userRepository = null; @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { ... } }
The problem that I see is that userRepository is not automatically supported. I get the following line in my console release, citing PlatformRealm:
INFO org.springframework.web.context.support.XmlWebApplicationContext - Bean 'platformRealm' of type [class com.resonance.platform.core.security.PlatformRealm] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
This is because of Apache Shiro ShiroFilterFactoryBean. This bean happens, and all its dependencies are loaded immediately after the container starts. This does not wait until my beans save is initialized before the dependencies are resolved. This causes the repository link to be null.
The following bean configurations are loaded through the contextConfigLocation parameter:
<context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/web-platform-persistence.xml, /WEB-INF/web-platform-services.xml </param-value> </context-param>
Services bean Configuration:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> <bean id="userSession" class="com.resonance.platform.web.core.services.ShiroUserSessionService" /> <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <property name="securityManager" ref="securityManager" /> <property name="loginUrl" value="/login" /> <property name="successUrl" value="/" /> <property name="filterChainDefinitions"> <value> # some example chain definitions: /admin/** = passThruFilter, roles[admin] /** = passThruFilter </value> </property> </bean> <bean id="passThruFilter" class="org.apache.shiro.web.filter.authc.PassThruAuthenticationFilter" /> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <property name="realm" ref="platformRealm" /> </bean> <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" /> <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor" /> <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"> <property name="securityManager" ref="securityManager" /> </bean> <bean id="platformRealm" class="com.resonance.platform.core.security.PlatformRealm" />
Persistence of bean config:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mongo="http://www.springframework.org/schema/data/mongo" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> <mongo:mongo id="mongo" /> <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> <constructor-arg ref="mongo" /> <constructor-arg value="platform" /> <property name="writeConcern"> <util:constant static-field="com.mongodb.WriteConcern.SAFE" ></util:constant> </property> </bean> <mongo:repositories base-package="com.resonance.platform.core.data.repositories" />
User repository:
package com.resonance.platform.core.data.repositories; import org.bson.types.ObjectId; import org.springframework.data.repository.CrudRepository; import com.resonance.platform.core.entities.User; public interface UserRepository extends CrudRepository<User, ObjectId> { User getByLogin(String login); }
My question is, how can I correctly determine the dependency of userRepository? I understand that ShiroFilterFactoryBean needs to be initialized to other dependencies and something else, but there must be a way to get the userRepository dependency.
EDIT: added user repository code.