调用getBean时的org.springframework.beans.factory.NoUniqueBeanDefinitionException

时间:2015-01-13 07:37:38

标签: java spring spring-security vaadin

我将spring security整合到了More-Vaadin/springsecurity-integration Vaadin的版本比较旧,所以我使用较新的VaadinVersion重写了一些元素。

但问题是在此方法的身份验证期间我捕获了异常。

public void handleAuthentication(String username, String password, HttpServletRequest request) {
    UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password);

    token.setDetails(new WebAuthenticationDetails(request));
    HttpSession session = request.getSession();
    ServletContext servletContext = session.getServletContext();
    WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
    AuthenticationManager authManager = wac.getBean(ProviderManager.class); //exception
    Authentication authentication = authManager.authenticate(token);
    SecurityContextHolder.getContext().setAuthentication(authentication);
}

我在NoUniqueBeanDefinitionException

中有wac.getBean
org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.springframework.security.
authentication.AuthenticationManager] is defined: expected single matching bean but found 3: org.springframework.security.au
thentication.ProviderManager#0,org.springframework.security.config.authentication.AuthenticationManagerFactoryBean#0,org.spr
ingframework.security.authenticationManager

当我尝试使用ProvideManager.class时,我得到了相同的异常。 我的context文件看起来像这样

<beans:beans xmlns="http://www.springframework.org/schema/security"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns:beans="http://www.springframework.org/schema/beans"
             xsi:schemaLocation="
                http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
                http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd">

    <global-method-security secured-annotations="enabled"/>

    <http pattern="/VAADIN/**" security="none"/>
    <http pattern="/static/**" security="none"/>

    <http auto-config="true">
        <intercept-url pattern="/" access="IS_AUTHENTICATED_ANONYMOUSLY"/>
    </http>

    <authentication-manager>
        <authentication-provider>
            <user-service>
                <user name="admin" authorities="ROLE_USER, ROLE_ADMIN" password="admin"/>
            </user-service>
        </authentication-provider>
    </authentication-manager>

    <beans:bean id="springSecurityFilterChain" class="org.springframework.security.web.FilterChainProxy">
        <filter-chain-map path-type="ant">
            <filter-chain pattern="/**" filters="securityContextPersistenceFilter"/>
        </filter-chain-map>
    </beans:bean>

    <beans:bean id="securityContextPersistenceFilter" class="org.springframework.security.web.context.SecurityContextPersistenceFilter"/>

</beans:beans>

有什么问题?我自己没有实现任何AuthenticationManager

1 个答案:

答案 0 :(得分:0)

为什么不像这样命名您的authenticationManager? <authentication-manager alias="myAuthenticationManager">

并称之为:wac.getBean("myAuthenticationManager");