gpt4 book ai didi

java - Spring MongoDB 和 Apache Shiro

转载 作者:搜寻专家 更新时间:2023-11-01 02:51:25 25 4
gpt4 key购买 nike

我正在尝试将 Apache Shiro 与 Spring 和 MongoDB 结合使用。我正在使用 Autowiring 的 Spring Data Repositories。我为 Shiro 创建了自己的自定义领域,它使用 Spring Data 存储库与 Mongo 对话:

public class PlatformRealm extends AuthorizingRealm {

@Autowired(required = true)
protected UserRepository userRepository = null;

@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
...
}
}

我看到的问题是 userRepository 没有被 Autowiring 。我在我的控制台输出中得到了以下引用 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)

这是因为 Apache Shiro ShiroFilterFactoryBean。正在发生的是这个 bean 及其所有依赖项在容器启动时立即加载。它不会在解析依赖关系之前等待我的持久性 bean 被初始化。这会导致存储库引用为空。

以下 bean 配置通过 contextConfigLocation 参数加载:

<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>

服务 bean 配置:

<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" />

<!-- Shiro (Security) -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager" />
<property name="loginUrl" value="/login" />
<property name="successUrl" value="/" />
<!-- The 'filters' property is not necessary since any declared javax.servlet.Filter
bean -->
<!-- defined will be automatically acquired and available via its beanName
in chain -->
<!-- definitions, but you can perform instance overrides or name aliases
here if you like: -->
<!-- <property name="filters"> <util:map> <entry key="anAlias" value-ref="someFilter"/>
</util:map> </property> -->
<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">
<!-- Single realm app. If you have multiple realms, use the 'realms' property
instead. -->
<property name="realm" ref="platformRealm" />
<!-- By default the servlet container sessions will be used. Uncomment
this line to use shiro's native sessions (see the JavaDoc for more): -->
<!-- <property name="sessionMode" value="native"/> -->
</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>

<!-- Define the Shiro Realm implementation you want to use to connect to
your back-end -->
<!-- security datasource: -->
<bean id="platformRealm" class="com.resonance.platform.core.security.PlatformRealm" />

持久化 bean 配置:

<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" />

用户存储库:

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;

/**
* A repository used to manage User entities.
* @author Kyle
*/
public interface UserRepository extends CrudRepository<User, ObjectId> {

/**
* Gets a user by the specified login.
* @param login
* @return
*/
User getByLogin(String login);

}

我的问题是,如何正确解析 userRepository 依赖项?我知道 ShiroFilterFactoryBean 必须在其他依赖项和诸如此类的东西之前初始化,但必须有一种方法来解决 userRepository 依赖项。

编辑:添加了用户存储库代码。

最佳答案

我遇到了此处描述的相同问题。我注意到两个 Spring 工厂。

  1. 从加载@Service @Repository 类的 dispacher-servlet.xml 中,由于在基础包级别定义的组件扫描,所以我可以将 @Autowire 服务类放入 Controller 。
  2. 来自应用程序上下文的@Autowire 类似乎没有被标记为@Service,因为它们没有被加载。

关于java - Spring MongoDB 和 Apache Shiro,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10503101/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com