I would like to use Spring Security with my MongoDB database. I search some sample code for spring security but it not working with my mogno connection.
This is my MongoDB configuration file
@Configuration
@EnableMongoRepositories("com.lab.repository")
public class DatabaseConfiguration extends AbstractMongoConfiguration{
@Value("${spring.data.mongodb.host}")
private String host;
@Value("${spring.data.mongodb.port}")
private Integer port;
@Value("${spring.data.mongodb.username}")
private String username;
@Value("${spring.data.mongodb.database}")
private String database;
@Value("${spring.data.mongodb.password}")
private String password;
@Bean
@Override
protected String getDatabaseName() {
return database;
}
@Override
public Mongo mongo() throws Exception {
return new MongoClient(host + ":" + port);
}
}
When I use Spring Security config class
@EnableAuthorizationServer
@Configuration
public class OAuth2AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
@Autowired
private AuthenticationManager authenticationManager;
@Autowired
private DataSource dataSource;
@Override
public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
oauthServer.tokenKeyAccess("permitAll()")
.checkTokenAccess("isAuthenticated()");
}
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.jdbc(dataSource)
.withClient("clientIdPassword")
.secret("secret")
.authorizedGrantTypes(
"password", "authorization_code", "refresh_token")
.scopes("read", "post");
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints)
throws Exception {
endpoints.tokenStore(tokenStore())
.authenticationManager(authenticationManager);
}
@Bean
public TokenStore tokenStore() {
return new JdbcTokenStore(dataSource);
}
}
and I ran application I get error
***************************
APPLICATION FAILED TO START
***************************
Description:
Field dataSource in com.newssystem.lab.config.OAuth2AuthorizationServerConfig required a bean of type 'javax.sql.DataSource' that could not be found.
- Bean method 'dataSource' not loaded because @ConditionalOnClass did not find required class 'org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType'
- Bean method 'dataSource' not loaded because @ConditionalOnClass did not find required classes 'javax.transaction.TransactionManager', 'org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType'
Action:
Consider revisiting the conditions above or defining a bean of type 'javax.sql.DataSource' in your configuration.
How can I add property DataSource for my Mongo configuration?
DataSource, have you done that?DatabaseConfigurationand just returnMongoobject?@Bean public DataSource mongoDataSource(){ return (DataSource) new MongoClient(host + ":" + port); }but it's not workingMongoClient cannot be cast to javax.sql.DataSourcedataSourcefield you autowired inOAuth2AuthorizationServerConfig? I guess that is not a mongoDb related bean.