Spring安全性与MongoDB dataSource错误

时间:2017-06-19 09:24:15

标签: java spring mongodb spring-security

我想在我的MongoDB数据库中使用Spring Security。我搜索一些示例代码以获得spring安全性,但它不能与我的mogno连接一起使用。

这是我的MongoDB配置文件

@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);
    }

}

当我使用Spring Security配置类

***************************
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.

我跑了应用程序我收到错误

{{1}}

如何为Mongo配置添加属性DataSource?

1 个答案:

答案 0 :(得分:0)

DataSource用于具有支持JDBC驱动程序的SQL数据库。它不适用于NoSQL的MongoDB(实际上Mongo JDBC驱动程序确实存在,但我不会去那里)。只需为TokenStore和ClientDetailsS​​ervice提供您自己的实现,即可从Mongo存储库(或MongoTemplate)中获取必要的信息。对于前者,将JdbcTokenStore替换为TokenStore实现。对于后者(假设您的客户端是动态的,并且需要从Mongo检索),可以按以下方式进行配置:

@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
    clients.withClientDetails(myClientDetailsService);
}