Spring安全性没有定义名为“CustomAuthenticationProvider”的bean

时间:2014-03-02 12:09:59

标签: java spring spring-security

我想去获取spring security安全认证表单。这是spring-security.xml文件的一部分

<bean id="authenticationFilter" class="com.portal.framework.web.security.CustomAuthenticationFilter">
        <property name="authenticationManager" ref="authenticationManager" />
        <property name="filterProcessesUrl" value="/login/validate" />
        <property name="usernameParameter" value="usernameOrEmail" />
        <property name="passwordParameter" value="password" />
        <property name="authenticationSuccessHandler" ref="restAuthenticationSuccessHandler" />
        <property name="authenticationFailureHandler" ref="restAuthenticationFailureHandler" />

    </bean>

    <authentication-manager alias="authenticationManager" xmlns="http://www.springframework.org/schema/security">
        <authentication-provider ref="customAuthenticationProvider" />
    </authentication-manager>

我收到了错误: 没有定义名为'customAuthenticationProvider'的bean

Bean解析由Java配置完成,如下所示:

@Configuration
@ComponentScan(basePackages = {"com.portal"})
public class MainConfiguration {

    @Bean
    public CustomAuthenticationProvider customAuthenticationProvider() {
        return new CustomAuthenticationProvider();
    }
}

此配置有问题吗?

2 个答案:

答案 0 :(得分:1)

问题通过替换

解决了
<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/someXmlfile.xml</param-value>
    </context-param>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        com.portal.configuration.IntegrationServerWebConfig
    </param-value>
</context-param>

定义班级:

@Configuration

@ImportResource({
    "classpath:/WEB-INF/mvc-dispatcher-servlet.xml",
    "classpath:/WEB-INF/spring-servlet.xml"})

@ComponentScan(basePackages = {"com.portal"})
public class IntegrationServerWebConfig {
}

答案 1 :(得分:0)

您的XML配置应定义要扫描其他组件的软件包。你可以这样做:

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

 <context:component-scan base-package="org.example"/>

</beans>