带有MongoDB数据源错误的java Spring安全性
我想在MongoDB数据库中使用Spring安全性。我搜索了一些spring security的示例代码,但它不适用于我的mogno连接
这是我的MongoDB配置文件
@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);
}
}
当我使用Spring安全配置类时
@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);
}
}
我运行应用程序时发现错误
***************************
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.
如何为我的Mongo配置添加属性数据源
# 1 楼答案
DataSource用于SQL数据库,其驱动程序支持JDBC。它不适用于NoSQL的MongoDB(事实上MongoJDBC驱动程序确实存在,但我不会去那里)。只需为TokenStore和ClientDetailsService提供您自己的实现,即可从Mongo存储库(或MongoTemplate)获取必要的信息。对于前者,将JdbcTokenStore替换为您的TokenStore实现。对于后者(假设您的客户端是动态的,需要从Mongo检索),您可以按如下方式配置它: