Spring Security找不到DataSource

时间:2016-10-05 18:44:34

标签: java mysql authentication spring-boot spring-security

我是Spring Boot和Spring Security的新手,我正在尝试设置Spring Security以使用MySQL数据库中的登录凭据。

SecurityConfig类:

   @Service
   @Configuration
   @EnableWebSecurity
    public class SecurityConfig extends WebSecurityConfigurerAdapter {

    private DataSource dataSource;

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }


@Override
protected void configure(HttpSecurity http) throws Exception {
    http
            .authorizeRequests().anyRequest().authenticated();
    http
            .formLogin().failureUrl("/login?error")
            .defaultSuccessUrl("/")
            .loginPage("/login")
            .permitAll()
            .and()
            .logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout")).logoutSuccessUrl("/login")
            .permitAll();
}

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

    auth
            .jdbcAuthentication().dataSource(dataSource)
            .usersByUsernameQuery("SELECT username, password, enabled FROM users WHERE username = ?")
            .authoritiesByUsernameQuery("SELECT username as username, enabled as authority FROM users WHERE username = ?");
    }
}

spring.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="userDaoImpl" class="com.watchdog.dao.UserDaoImpl">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <!--<property name="url" value="jdbc:mysql://localhost:3306/bsbuckne" />-->
        <property name="url" value="jdbc:mysql://localhost:3306/test" />
        <property name="username" value="root" />
        <property name="password" value="password" />
    </bean>
</beans>

我认为我的问题是我的dataSource未被识别。我得到的堆栈跟踪是

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'securityConfig': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Property 'dataSource' is required
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:355) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]

如果我将身份验证类型更改为内存并手动设置用户名&amp;密码验证有效。

我很困惑的原因是因为我在处理所有数据库CRUD操作的不同DAO类中使用相同的setDataSource方法,并且它正在按预期工作。是什么让我的SecurityConfig类无法找到我的dataSource?

1 个答案:

答案 0 :(得分:1)

请尝试注释:

@Autowired
private DataSource dataSource;

@Autowired
public void setDataSource(DataSource dataSource) {
    this.dataSource = dataSource;
}