我正在Spring Web MVC项目中使用Java配置来实现Spring Security,由于某种原因,@Autowired注释未在我的安全配置类中注入字段。我在SO上发现了this非常相似的问题,但是我的设置更加简单,并且可接受的答案根本不适用于我的情况。

作为参考,我遵循了Spring自己的安全性文档(here)的前三章,并实现了内存中身份验证的快速运行。然后,我想切换到JDBC身份验证,并注入带有DataSource批注的@Autowired(如this example中所示)。但是,我收到此错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'securityConfig': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.sql.DataSource com.tyedart.web.config.security.SecurityConfig.dataSource; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.sql.DataSource] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}


这是我的安全配置类。如您所见,我通过显式查找数据源来解决此问题:

@Configuration
@EnableWebMvcSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

//  @Autowired
//  private DataSource dataSource;

//  @Autowired
//  public PasswordEncoder passwordEncoder;

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {

        InitialContext ctx = new InitialContext();
        DataSource dataSource = (DataSource) ctx.lookup("java:/comp/env/jdbc/TyedArtDB");

        PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();

        auth
            .jdbcAuthentication()
                .dataSource(dataSource)
                .passwordEncoder(passwordEncoder);
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/manage/**").hasRole("ADMIN")
                .and()
            .formLogin();
    }
}


这是我非常简单的root-context.xml:

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

       <!-- Root Context: defines shared resources visible to all other web components -->

       <jee:jndi-lookup id="dataSource" jndi-name="jdbc/TyedArtDB"/>

       <bean id="passwordEncoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"/>

</beans>


我究竟做错了什么?

最佳答案

在root-context.xml中添加<context:component-scan base-package="your.package.where.your.bean.is"/>

您可以在字段声明中取消注释@Autowired并将其从承包商中删除。您可以找到更多信息here

如果您@Autowired一个bean,请不要忘记使用new删除初始化。

关于java - @Autowired在Spring Security中不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21869416/

10-09 02:03