如何将bean从xml连接到@Component?

时间:2014-01-16 04:18:39

标签: java spring java-ee spring-security

我正在尝试自定义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中进行注入:

的AuthenticationManager

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

注入preAuthenticatedUserDetailsS​​ervice 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

例外是必须设置AuthenticationUserDetailsS​​ervice 意味着preAuthenticatedUserDetailsService未能注入课堂,但显然它在security-context.xml

即使我在AuthenticationUserDetailsService班级中@AutowiredAuthenticationProvider

    @Autowired
    private AuthenticationUserDetailsService<PreAuthenticatedAuthenticationToken> 
                preAuthenticatedUserDetailsService;

它仍然给我同样的错误 那我在这里想念的是什么?

由于

1 个答案:

答案 0 :(得分:0)

我认为您需要在使用PreAuthenticatedAuthenticationProvider之前设置AuthenticationUserDetailsS​​ervice。您能提供更多自定义身份验证提供程序的代码吗?正如医生所说:

  

此身份验证提供程序不会执行任何检查   身份验证*请求,因为它们应该已经存在   预认证。但是,* AuthenticationUserDetailsS​​ervice   实现可能仍会抛出UsernameNotFoundException,for   示例