2017-04-17 96 views
-1

我目前正在使用spring-flex集成并希望添加通过spring保护的web服务。使用名称'_loginCommandPostProcessor'创建bean时出错

为此,我试图创建两个http标记?

弹簧安全-config.xml中

<?xml version="1.0" encoding="UTF-8"?> 
<beans:beans xmlns="http://www.springframework.org/schema/security" 
    xmlns:beans="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-4.3.xsd 
    http://www.springframework.org/schema/security 
    http://www.springframework.org/schema/security/spring-security-4.2.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-4.3.xsd"> 

    <context:component-scan base-package="com.somenamespace.login" /> 
    <context:annotation-config/> 

    <beans:bean id="entryPoint" 
     class="org.springframework.flex.security4.FlexAuthenticationEntryPoint"/> 


    <http entry-point-ref="entryPoint" pattern="/messagebroker/**"> 
     <intercept-url pattern="/**" access="authenticated"/> 
     <custom-filter position="FORM_LOGIN_FILTER" ref="myAuthFilter" /> 
     <session-management session-authentication-strategy-ref="sas"/> 
    </http> 

    <http pattern="*"> 
    <intercept-url pattern="/**" access="authenticated"/> 
     <http-basic /> 
     <custom-filter position="FORM_LOGIN_FILTER" ref="myAuthFilter" /> 
     <session-management session-authentication-strategy-ref="sas"/> 
    </http> 

    <beans:bean id="myAuthFilter" class= 
"org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter"> 
     <beans:property name="sessionAuthenticationStrategy" ref="sas" /> 
     <beans:property name="authenticationManager" ref="authenticationManager" /> 

    </beans:bean> 
    <beans:bean id="sas" class="org.springframework.security.web.authentication.session.SessionFixationProtectionStrategy" /> 

    <beans:bean id="switchUserProcessingFilter" class="org.springframework.security.web.authentication.switchuser.SwitchUserFilter"> 
     <beans:property name="userDetailsService" ref="customAuthenticationProvider"/> 
     <beans:property name="switchUserUrl" value="/admin/impersonate"/> 
     <beans:property name="targetUrl" value="/cre/CRE.html"/> 
     <beans:property name="switchFailureUrl" value="/admin/switchUser"/> 
    </beans:bean> 


    <beans:bean id="defaultMessageTemplate" class="org.springframework.flex.messaging.MessageTemplate" /> 

    <beans:bean id="customAuthenticationProvider" class="com.somenamespace.login.CustomAuthenticationProvider"/> 

    <authentication-manager alias="authenticationManager"> 
    <authentication-provider ref="customAuthenticationProvider"/> <  
    </authentication-manager> 

</beans:beans> 

,我发现了以下错误:

Error creating bean with name '_loginCommandPostProcessor': Unsatisfied dependency expressed through bean property 'sessionAuthenticationStrategy'; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'org.springframework.security.web.authentication.session.SessionAuthenticationStrategy' available: expected single matching bean but found 3: org.springframework.security.web.authentication.session.CompositeSessionAuthenticationStrategy#0,org.springframework.security.web.authentication.session.CompositeSessionAuthenticationStrategy#1,sas 
+0

从错误中可以清楚地看出,除了'sas'之外,你在spring的context中初始化了2个bean。你确定你没有扫描'org.springframework.security.web.authentication.session'吗?它可以在其他xml文件中声明。 –

+0

我不确定这意味着什么。我如何确保我没有扫描它? – DangeRuss

+0

我已经发布我的回答您的问题作为下面的答案与可能的解决方案,请看看。 –

回答

0

<context:component-scan base-package="com.somenamespace.login" /> - 这个你告诉春:“请扫描com.somenamespace.login和初始化任何标有注释的豆:@Component,@Repository,@Service,@Controller进入我的应用程序上下文“。

所以,你必须初始化成是候选人的情况下3种豆被装配到财产sessionAuthenticationStrategy

  1. sas - 通过扫描com.somenamespace.login初始化(见这个答案开头的 声明)。
  2. org.springframework.security.web.authentication.session.CompositeSessionAuthenticationStrategy#0 - 您不希望初始化的bean;可能你可能在另一个xml弹簧 上下文文件(如果它们存在于你的应用程序中 - 我不知道 那个)。
  3. org.springframework.security.web.authentication.session.CompositeSessionAuthenticationStrategy#1 - 见点2

Spring不知道该选哪一个。因此你正在得到错误。

我建议你们两个可能的解决方案:

  1. 肮脏的,但快。您将您的sas bean标记为主要bean。像这样: <beans:bean id="sas" class="org.springframework.security.web.authentication.session.SessionFixationProtectionStrategy" primary=true/>请注意,如果您需要注入CompositeSessionAuthenticationStrategy类的bean或其他在您的应用程序的某处实现SessionAuthenticationStrategy但不是sas的bean,那么该解决方案将不适合您,因为它将始终注入sas
  2. 更清洁的解决方案,但可能需要更多时间:找出 会引发哪些意想不到的豆被启动并修复它。从我的点 来看,正如我所说的,它可能是额外的<context:component-scan /> 声明某处在你的spring context xml文件中(如果有多个 )。

    祝你好运!我希望这可以帮助。

相关问题