【发布时间】:2014-02-04 20:35:41
【问题描述】:
我正在尝试自定义一个PreAuthenticatedAuthenticationProvider 类来扩展逻辑:
@Component("superAuthenticaionProvider")
public class SuperAuthenticaionProvider extends PreAuthenticatedAuthenticationProvider{
// this is inherited from PreAuthenticatedAuthenticationProvider
/*private AuthenticationUserDetailsService<PreAuthenticatedAuthenticationToken> preAuthenticatedUserDetailsService;*/
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
.......
}
}
继承自PreAuthenticatedAuthenticationProvider,需要一个preAuthenticatedUserDetailsService注入类,我在xml中进行注入:
身份验证管理器
<sec:authentication-manager alias="authenticationManager">
<sec:authentication-provider ref='preAuthenticatedAuthenticationProvider'/>
</sec:authentication-manager>
自定义身份验证提供程序
<bean id="preAuthenticatedAuthenticationProvider"
class="org.myapps.save.the.world.SuperAuthenticaionProvider">
<property name="preAuthenticatedUserDetailsService"
ref="preAuthenticatedUserDetailsService"/>
</bean>
注入 preAuthenticatedUserDetailsService bean
<bean id="preAuthenticatedUserDetailsService"
class="org.springframework.security.web.authentication.preauth.
PreAuthenticatedGrantedAuthoritiesUserDetailsService"/>
但是 Spring 框架给了我一个例外:
Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: An AuthenticationUserDetailsService must be set
和
Caused by: java.lang.IllegalArgumentException: An AuthenticationUserDetailsService must be set
at org.springframework.util.Assert.notNull(Assert.java:112)
at org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationProvider.afterPropertiesSet(PreAuthenticatedAuthenticationProvider.java:44)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1545)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1483)
... 26 more
例外是关于必须设置一个AuthenticationUserDetailsService
这意味着preAuthenticatedUserDetailsService 未能注入到类中,但显然它在security-context.xml 中。
即使我在AuthenticationProvider 类中将AuthenticationUserDetailsService 设为@Autowired
@Autowired
private AuthenticationUserDetailsService<PreAuthenticatedAuthenticationToken>
preAuthenticatedUserDetailsService;
它仍然给我同样的错误 那么我在这里错过了什么?
谢谢
【问题讨论】:
标签: java spring jakarta-ee spring-security