【发布时间】:2013-01-25 23:43:22
【问题描述】:
经过几个小时的绝望试图找出为什么我的自定义身份验证管理器没有自动装配,我来这里寻求一点帮助。 所以我有 Web 应用程序,在 Spring 上运行(使用 Stripes)。身份验证由我的自定义类完成。一切正常 - 在 jsp 中,我可以使用 jsp 标签和一切,http 拦截器可以正常工作。但是当我尝试将身份验证管理器自动连接到 Jersey“REST 类”时,一切都出错了,没有注入任何东西。
所以,在 web xml 中我有两个上下文文件:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring-context.xml
/WEB-INF/spring-security.xml
</param-value>
</context-param>
spring-security.xml:
<context:component-scan base-package="org.springframework.security" />
<context:annotation-config/>
<http use-expressions="true" auto-config="true">
</http>
<authentication-manager alias="authManager">
<authentication-provider ref="AuthenticationManagerBean" />
</authentication-manager>
<beans:bean id="AuthenticationManagerBean" class="com.manager.services.MyAuthenticationManager"/>
<beans:bean id="expressionHandler" class="org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler" />
Jersey action bean 内部是这样的:
@Inject
@Qualifier("authManager")
protected AuthenticationManager authenticationManager;
整个应用程序构建正常,码头服务器启动没有问题(MyAuthenticationManager 和 org.springframework.security.authenticationManager 都已成功预先设置),但是当尝试使用自动装配的 authmanager 时,我得到空指针。
编辑: 我已经成功地将 AuthenticationManager 自动装配到 Stripes 的 Action Beans 中,但是 Jersey 的 action bean 仍然存在问题,其中构造 @Inject @Qualifier(...) 不适用于 AuthManager(但适用于其他自动装配的 bean,尽管没有 @Qualifier,只是@注入)。
编辑 2: 经过几个小时试图找出为什么 Spring 不将 authenticationManager 注入 Jersey Beans 后,我解决了这个问题:
@Inject
@InjectParam("org.springframework.security.authenticationManager")
protected AuthenticationManager authenticationManager;
我不知道它是否是好的解决方案,但它正在工作,并且 spring 现在注入了正确的身份验证管理器。
【问题讨论】:
标签: spring security authentication autowired