Reputation: 21
I am using Spring Security 4 and I want to authenticate by querying the database. I have created Users and Authorities tables. My jdbc connection bean of datasource, is defined in a java file using annotations. My spring security configuration is in xml. I have read that the same contextLoader is being used and hence there should be no problem loading all the beans. But, how do I import the dataSource bean defined in the java file, into my spring security config file?
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
@Configuration
@MapperScan("com.iz.acsadmin.dao")
public class DataConfig{
@Bean(name = "dataSource" )
public DataSource getDataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName("org.postgresql.Driver");
dataSource.setUrl("jdbc:postgresql://192.168.1.3:5432/Student");
dataSource.setUsername("postgres");
dataSource.setPassword("admin");
return dataSource;
}
...
The XML Spring-Config file
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="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
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">
<http auto-config="true" use-expressions="true">
<intercept-url pattern="/" access="permitAll" />
<intercept-url pattern="/test.htm" access="hasRole('USER') or hasRole('ADMIN') or hasRole('DBA')" />
<intercept-url pattern="/welcome.htm" access="hasRole('USER') or hasRole('ADMIN') or hasRole('DBA')" />
<form-login />
</http>
<authentication-manager >
<authentication-provider>
<jdbc-user-service data-source-ref="dataSource"
users-by-username-query=
"SELECT USERNAME, PASSWORD, ENABLED FROM USERS WHERE USERNAME=?"
authorities-by-username-query=
"SELECT USERNAME, AUTHORITY FROM AUTHORITIES WHERE USERNAME=?"
/>
</authentication-provider>
</authentication-manager>
</beans:beans>
The dataSource bean in the xml file is not able to find the main dataSource bean in the java file.
Upvotes: 0
Views: 13972
Reputation: 909
In order to get this configuration working, simply add this to your Spring XML config :
<context:component-scan base-package="com.your.package" />
This line tells Spring to load your @Configuration annotated classes from the package "com.your.package" (assuming that the DataConfig class is in this package).
By the way, don't forget to add Spring context XSD location :
<beans:beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/security" xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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/security http://www.springframework.org/schema/security/spring-security.xsd">
<context:component-scan base-package="com.your.package" />
<!-- some other configuration -->
Upvotes: 1
Reputation: 467
As another answer points out make sure you have:
> In order to get this configuration working, simply add this to your
> Spring XML config :
>
> <context:component-scan base-package="com.your.package" /> This line
> tells Spring to load your @Configuration annotated classes from the
> package "com.your.package" (assuming that the DataConfig class is in
> this package).
While you are at it make sure you have:
<context:annotation-config />
I am new to Spring, but wonder if dataSource as a name is unique.
I would try two things, annotate your datasource as primary like so:
@Primary
@Bean(name = "dataSource" )
public DataSource getDataSource() {
If that didn't work I would try a different name and point to it in the Security Config:
@Bean(name = "myDataSource" )
public DataSource getDataSource() {
Upvotes: 0