【问题标题】:How to wire the bean from xml into @Component?如何将 bean 从 xml 连接到 @Component?
【发布时间】: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>

注入 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 中。

即使我在AuthenticationProvider 类中将AuthenticationUserDetailsService 设为@Autowired

    @Autowired
    private AuthenticationUserDetailsService<PreAuthenticatedAuthenticationToken> 
                preAuthenticatedUserDetailsService;

它仍然给我同样的错误 那么我在这里错过了什么?

谢谢

【问题讨论】:

    标签: java spring jakarta-ee spring-security


    【解决方案1】:

    我认为您需要在使用 PreAuthenticatedAuthenticationProvider 之前设置 AuthenticationUserDetailsS​​ervice。您能否提供更多自定义身份验证提供程序的代码,或者是这样吗?正如文档所说:

    此身份验证提供程序不会对 身份验证 * 请求,因为它们应该已经是 预认证。但是,* AuthenticationUserDetailsS​​ervice 实现可能仍会抛出 UsernameNotFoundException,因为 例子。

    【讨论】:

      猜你喜欢
      • 2021-03-15
      • 2012-02-16
      • 2016-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多