MongoDB
 sql >> Cơ Sở Dữ Liệu >  >> NoSQL >> MongoDB

spring-data-mongodb đưa ra ngoại lệ xác thực mongodb

I think maybe the mongodb's authorization has little hard to understand.
This works for me,

    @Configuration
public class MongoConfig {

    @Value("${mongo.host}")
    private  String host;

    @Value("#{new Integer('${mongo.port}')}")
    private  Integer port;

    @Value("${mongo.database}")
    private  String database;

    @Value("${mongo.username}")
    private  String username;

    @Value("${mongo.password}")
    private  String password;

    public @Bean MongoClientFactoryBean mongoDbFactory() throws Exception {
        MongoClientFactoryBean clientFactoryBean = new MongoClientFactoryBean();
        clientFactoryBean.setHost(host);
        clientFactoryBean.setPort(port);
        MongoCredential credential = MongoCredential.createScramSha1Credential(username, database, password.toCharArray());
        clientFactoryBean.setCredentials(new MongoCredential[]{credential});
        return clientFactoryBean;
    }

    public @Bean MongoTemplate mongoTemplate(Mongo mongo) throws Exception {
        MongoTemplate mongoTemplate = new MongoTemplate(mongo, database);
        return mongoTemplate;

    }
}

---------------------------------
mongo.host=127.0.0.1
mongo.port=27017
mongo.username=hisoka
mongo.password=welcome
mongo.database=test
----------------------------
Just in mongod cmd create user hisoka for db test,
db.createUser({"user":"hisoka","pwd":"welcome","roles":[{role:"readWrite",db:"test"}]});
------------------------------
Take look in mongoChef, the mongodb auth info:
{ 
    "_id" : "test.hisoka", 
    "user" : "hisoka", 
    "db" : "test", 
    "credentials" : {
        "SCRAM-SHA-1" : {
            "iterationCount" : NumberInt(10000), 
            "salt" : "tfXYRbCj6N433PFWJzwarA==", 
            "storedKey" : "pMnNVX7Co7AY7Q4b/dtq18IQfeE=", 
            "serverKey" : "x8vUt590SYVxqW2PW6DA849iTgE="
        }
    }, 
    "roles" : [
        {
            "role" : "readWrite", 
            "db" : "test"
        }
    ]
}


  1. Redis
  2.   
  3. MongoDB
  4.   
  5. Memcached
  6.   
  7. HBase
  8.   
  9. CouchDB
  1. Cách sao lưu cơ sở dữ liệu Open edX MongoDB của bạn

  2. Mongodb:db.printShardingStatus () / sh.status () goi trong Java (và JavaScript)

  3. Mongoose mở rộng xác thực mặc định

  4. Tự động hóa MongoDB với SaltStack

  5. Nhúng tin nhắn trả lời bên trong tin nhắn gốc với mongodb bằng cách sử dụng mongoid