1

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?

7
  • you need to define DataSource, have you done that? Commented Jun 19, 2017 at 9:31
  • Can I do this in DatabaseConfiguration and just return Mongo object? Commented Jun 19, 2017 at 9:39
  • yes. please..... Commented Jun 19, 2017 at 9:39
  • Ok I try do that @Bean public DataSource mongoDataSource(){ return (DataSource) new MongoClient(host + ":" + port); } but it's not working MongoClient cannot be cast to javax.sql.DataSource Commented Jun 19, 2017 at 9:55
  • what is the dataSource field you autowired in OAuth2AuthorizationServerConfig? I guess that is not a mongoDb related bean. Commented Jun 19, 2017 at 9:59

1 Answer 1

1

DataSource is for SQL databases with drivers supporting JDBC. It won't work with MongoDB which is NoSQL (actually Mongo JDBC drivers do exist but I wouldn't go there). Simply provide your own implementations for TokenStore and ClientDetailsService that get the necessary information from a Mongo repository (or MongoTemplate). For the former, replace JdbcTokenStore with your TokenStore implementation. For the latter (assuming your clients are dynamic and need to be retrieved from Mongo), you can configure it as follows:

@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
    clients.withClientDetails(myClientDetailsService);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.