为MongoDb访问问题添加凭据

时间:2018-11-09 17:32:26

标签: java spring mongodb mongoose

我的Spring应用程序中有以下配置文件:

public class MongoConfig {

    @Bean
    public MongoDbFactory mongoDbFactory() throws Exception {
        return new SimpleMongoDbFactory(new MongoClient(host, port), database);
    }

    @Bean
    public MongoTemplate mongoTemplate() throws Exception {

        MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory());

        return mongoTemplate;

    }
}

现在我要做的就是设置用于访问mongo数据库的用户名/密码。

问题是在mongo-java-driver 3.6.4中似乎不赞成使用所有用于传递凭据的功能

我已经尝试过了:

MongoCredential credential = MongoCredential.createCredential(username, database, password.toCharArray());
MongoClient mongoClient = new MongoClient (new ServerAddress(host, port), Arrays.asList(credential));

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

我如下使用。

@Configuration
@EnableConfigurationProperties(MultipleMongoProperties.class)
public class MultipleMongoConfig {

    /** The mongo properties. */
    private final MultipleMongoProperties mongoProperties;

    /**
     * Instantiates a new multiple mongo config.
     *
     * @param mongoProperties the mongo properties
     */
    @Autowired
    public MultipleMongoConfig(MultipleMongoProperties mongoProperties) {
        super();
        this.mongoProperties = mongoProperties;
    }

    /**
     * Lab mongo template.
     *
     * @return the mongo template
     * @throws Exception the exception
     */
    @Primary
    @Bean(name = "labMongoTemplate")
    public MongoTemplate labMongoTemplate() throws Exception {
        return new MongoTemplate(labFactory(this.mongoProperties.getLab()));
    }

    /**
     * Historical mongo template.
     *
     * @return the mongo template
     * @throws Exception the exception
     */
    @Bean(name = "historicalMongoTemplate")
    public MongoTemplate historicalMongoTemplate() throws Exception {
        return new MongoTemplate(historicalFactory(this.mongoProperties.getHistorical()));
    }


    /**
     * Lab factory.
     *
     * @param mongo the mongo
     * @return the mongo db factory
     * @throws Exception the exception
     */
    @Bean
    @Primary
    public MongoDbFactory labFactory(final MongoProperties mongo) throws Exception {

        return getMongoDbFactory(mongo);
    }

    /**
     * Historical factory.
     *
     * @param mongo the mongo
     * @return the mongo db factory
     * @throws Exception the exception
     */
    @Bean
    public MongoDbFactory historicalFactory(final MongoProperties mongo) throws Exception {
        return getMongoDbFactory(mongo);
    }

    /**
     * Gets the mongo db factory.
     *
     * @param mongo the mongo
     * @return the mongo db factory
     */
    @SuppressWarnings("deprecation")
    private MongoDbFactory getMongoDbFactory(final MongoProperties mongo) {
        List<MongoCredential> credentials = Arrays.asList(
                MongoCredential.createCredential(mongo.getUsername(), mongo.getDatabase(), mongo.getPassword()));
        return new SimpleMongoDbFactory(
                new MongoClient(new ServerAddress(mongo.getHost(), mongo.getPort()), credentials), mongo.getDatabase());
    }

}

很好,我想到目前为止,我还没有遇到任何问题。从过去的六个月开始投入生产。