jrmullen
jrmullen

Reputation: 346

Spring Security cannot find DataSource

I am new to Spring Boot and Spring Security, and I am trying to set up Spring Security to use my login credentials from MySQL database.

SecurityConfig class:

   @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>

I believe my problem is that my dataSource is not being recognized. The stack trace I am getting is

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]

If I change the authentication type to in-memory and manually set a username & password the authentication works.

The reason I am so confused is because I am using this same setDataSource method in a different DAO class that is handling all of my database CRUD operations and it is working as expected. What is keeping my SecurityConfig class from finding my dataSource?

Upvotes: 2

Views: 924

Answers (1)

Sergii Getman
Sergii Getman

Reputation: 4381

have your tried annotate:

@Autowired
private DataSource dataSource;

or

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

Upvotes: 1

Related Questions