用于自定义筛选器的Spring Security Java配置

用于自定义筛选器的Spring Security Java配置,spring,spring-security,Spring,Spring Security,我正在将spring应用程序从XML配置转换为基于java的配置。我的代码需要一个帮助 以前我的XML配置如下 <authentication-manager id="myAuthenticationManager"> <authentication-provider user-service-ref="employeeImpl" /> </authentication-manager> <beans:bean id="employeeImpl

我正在将spring应用程序从XML配置转换为基于java的配置。我的代码需要一个帮助

以前我的XML配置如下

<authentication-manager id="myAuthenticationManager">
    <authentication-provider user-service-ref="employeeImpl" />
</authentication-manager>

<beans:bean id="employeeImpl" class="com.my.employee.EmployeeImpl">
    <beans:property name="dataSource">
        <beans:ref bean="myDatasource" />
    </beans:property>
    <beans:property name="usersByUsernameQuery">
        <beans:value>
            SELECT client_id, client_secret, enabled FROM client WHERE client_id=?
        </beans:value>
    </beans:property>       

    <beans:property name="authoritiesByUsernameQuery">
        <beans:value>
            SELECT client_id, authority FROM authorities WHERE client_id = ?
        </beans:value>
    </beans:property>
</beans:bean>

<beans:bean id="myCredentialsTokenEndpointFilter" class="com.my.web.controller.MyCredentialsTokenEndpointFilter">
    <beans:property name="authenticationManager" ref="myAuthenticationManager" />
    <beans:property name="tokenMappingQuery">
        <beans:value>INSERT INTO TOKEN_MAPPING (TOKEN_D, TOKEN_G) VALUES(?,?)</beans:value>
    </beans:property>
    <beans:property name="dataSource" ref="myDatasource" />
</beans:bean>
我的其他课程

@Configuration
@ComponentScan
public class MyBeanConfiguration
{

    @Autowired
    private DataSource dataSource;

    @Bean
    public MyService myService()
    {
        return new myServiceImpl();
    }

    @Bean
    public UserDetailsService employeeImpl()
    {
        EmployeeImpl employee = new EmployeeImpl();
        employee.setDataSource(dataSource);
        employee.setUsersByUsernameQuery("SELECT client_id, client_secret, enabled FROM my_client WHERE client_id=?");
        employee.setAuthoritiesByUsernameQuery("SELECT client_id, authority FROM authorities WHERE client_id = ?");
        return employee;
    }

}

问题是每次调试达到MyWebSecurity配置程序的
配置(AuthenticationManagerBuilder auth)时,自动连接的变量employeeImpl总是空的,所以我得到以下异常

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'MyWebSecurityConfigurator': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.my.employee.EmployeeImpl com.my.config.security.MyWebSecurityConfigurator.employeeImpl; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employeeImpl' defined in file [D:\EclipseWS\Alfresco Research Portal\Research Portal\Spring\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\SpringSecurity\WEB-INF\classes\com\my\employee\EmployeeImpl.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: 'dataSource' or 'jdbcTemplate' is required
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:700)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:381)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:293)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4959)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5455)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.my.employee.EmployeeImpl com.my.config.security.MyWebSecurityConfigurator.employeeImpl; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employeeImpl' defined in file [D:\EclipseWS\Alfresco Research Portal\Research Portal\Spring\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\SpringSecurity\WEB-INF\classes\com\my\employee\EmployeeImpl.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: 'dataSource' or 'jdbcTemplate' is required
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
    ... 22 more

我需要你的建议来理解我的错误。。无法理解我在做什么不正确

你能提供你试图用来配置Spring的Java代码吗,这样我们就可以找出问题了?嗨,Erik,谢谢你的回复,我在这里编辑了我的Java代码..需要你的输入。。如您所见,我正在XML配置的“myCredentialsTokenEndpointFilter”中注入“myAuthenticationManager”bean,如何在java配置中实现同样的效果?使用
authenticationManagerBean()
方法获取引用。我已编辑了我的原始帖子。。除了代码和例外,我得到。。我想知道我做错了什么。。请提出你的建议。。感谢我是如何做到这一点的,但我面临的另一个问题是我创建的过滤器,我添加了扩展AbstractAuthenticationProcessingFilter类的自定义过滤器,在我的自定义类中,我试图使用@Autowired private MyService MyService,它在上述MyBean配置类中声明,但当我调试到该自定义筛选器的attemptAuthentication时,我得到的myService为null。@M.Deinum
@Configuration
@ComponentScan
public class MyBeanConfiguration
{

    @Autowired
    private DataSource dataSource;

    @Bean
    public MyService myService()
    {
        return new myServiceImpl();
    }

    @Bean
    public UserDetailsService employeeImpl()
    {
        EmployeeImpl employee = new EmployeeImpl();
        employee.setDataSource(dataSource);
        employee.setUsersByUsernameQuery("SELECT client_id, client_secret, enabled FROM my_client WHERE client_id=?");
        employee.setAuthoritiesByUsernameQuery("SELECT client_id, authority FROM authorities WHERE client_id = ?");
        return employee;
    }

}
@Service
public class EmployeeImpl extends JdbcDaoImpl
{
    @Autowired
    private DataSource dataSource;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException
    {
        System.out.println("Received UserName : " + username);

        UserDetails user = super.loadUserByUsername(username);
        MyUser myUser = new MyUser(username, user.getPassword(), user.isEnabled(), user.isAccountNonExpired(), user.isCredentialsNonExpired(), user.isAccountNonLocked(),
                user.getAuthorities());

        return myUser;
    }
}
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'MyWebSecurityConfigurator': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.my.employee.EmployeeImpl com.my.config.security.MyWebSecurityConfigurator.employeeImpl; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employeeImpl' defined in file [D:\EclipseWS\Alfresco Research Portal\Research Portal\Spring\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\SpringSecurity\WEB-INF\classes\com\my\employee\EmployeeImpl.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: 'dataSource' or 'jdbcTemplate' is required
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:700)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:381)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:293)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4959)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5455)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.my.employee.EmployeeImpl com.my.config.security.MyWebSecurityConfigurator.employeeImpl; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employeeImpl' defined in file [D:\EclipseWS\Alfresco Research Portal\Research Portal\Spring\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\SpringSecurity\WEB-INF\classes\com\my\employee\EmployeeImpl.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: 'dataSource' or 'jdbcTemplate' is required
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
    ... 22 more