{"action":"created","issue":{"url":"https://api.github.com/repos/spring-projects/spring-boot/issues/2562","labels_url":"https://api.github.com/repos/spring-projects/spring-boot/issues/2562/labels{/name}","comments_url":"https://api.github.com/repos/spring-projects/spring-boot/issues/2562/comments","events_url":"https://api.github.com/repos/spring-projects/spring-boot/issues/2562/events","html_url":"https://github.com/spring-projects/spring-boot/issues/2562","id":59083337,"number":2562,"title":"MongoClient autoconfiguration does not take into account authentication-database property","user":{"login":"chli1","id":2370018,"avatar_url":"https://avatars.githubusercontent.com/u/2370018?v=3","gravatar_id":"","url":"https://api.github.com/users/chli1","html_url":"https://github.com/chli1","followers_url":"https://api.github.com/users/chli1/followers","following_url":"https://api.github.com/users/chli1/following{/other_user}","gists_url":"https://api.github.com/users/chli1/gists{/gist_id}","starred_url":"https://api.github.com/users/chli1/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/chli1/subscriptions","organizations_url":"https://api.github.com/users/chli1/orgs","repos_url":"https://api.github.com/users/chli1/repos","events_url":"https://api.github.com/users/chli1/events{/privacy}","received_events_url":"https://api.github.com/users/chli1/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/spring-projects/spring-boot/labels/bug","name":"bug","color":"fc2929"}],"state":"closed","locked":false,"assignee":{"login":"wilkinsona","id":914682,"avatar_url":"https://avatars.githubusercontent.com/u/914682?v=3","gravatar_id":"","url":"https://api.github.com/users/wilkinsona","html_url":"https://github.com/wilkinsona","followers_url":"https://api.github.com/users/wilkinsona/followers","following_url":"https://api.github.com/users/wilkinsona/following{/other_user}","gists_url":"https://api.github.com/users/wilkinsona/gists{/gist_id}","starred_url":"https://api.github.com/users/wilkinsona/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wilkinsona/subscriptions","organizations_url":"https://api.github.com/users/wilkinsona/orgs","repos_url":"https://api.github.com/users/wilkinsona/repos","events_url":"https://api.github.com/users/wilkinsona/events{/privacy}","received_events_url":"https://api.github.com/users/wilkinsona/received_events","type":"User","site_admin":false},"milestone":{"url":"https://api.github.com/repos/spring-projects/spring-boot/milestones/33","html_url":"https://github.com/spring-projects/spring-boot/milestones/1.2.2","labels_url":"https://api.github.com/repos/spring-projects/spring-boot/milestones/33/labels","id":902476,"number":33,"title":"1.2.2","description":"","creator":{"login":"philwebb","id":519772,"avatar_url":"https://avatars.githubusercontent.com/u/519772?v=3","gravatar_id":"","url":"https://api.github.com/users/philwebb","html_url":"https://github.com/philwebb","followers_url":"https://api.github.com/users/philwebb/followers","following_url":"https://api.github.com/users/philwebb/following{/other_user}","gists_url":"https://api.github.com/users/philwebb/gists{/gist_id}","starred_url":"https://api.github.com/users/philwebb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/philwebb/subscriptions","organizations_url":"https://api.github.com/users/philwebb/orgs","repos_url":"https://api.github.com/users/philwebb/repos","events_url":"https://api.github.com/users/philwebb/events{/privacy}","received_events_url":"https://api.github.com/users/philwebb/received_events","type":"User","site_admin":false},"open_issues":1,"closed_issues":89,"state":"open","created_at":"2014-12-13T03:31:15Z","updated_at":"2015-02-26T15:30:12Z","due_on":"2015-02-26T08:00:00Z","closed_at":null},"comments":1,"created_at":"2015-02-26T14:52:34Z","updated_at":"2015-02-26T15:36:04Z","closed_at":"2015-02-26T15:30:12Z","body":"I have set username, password and authentication-database properties set in my configuration file as follows:\r\n\r\nspring.data.mongodb.database=customdata\r\nspring.data.mongodb.username=top\r\nspring.data.mongodb.password=secret\r\nspring.data.mongodb.authentication-database=admin\r\n\r\nThen I'm using the default autoconfiguration to create a MongoClient object and passing it to mongeez. But I did not manage to do a successful authentication. Stepping through the code and looking into the server logs, I saw that mongo client still tries to authenticate using the database \"customdata\" instead of \"admin\".\r\n\r\nI think the problem is within the method MongoProperties.createMongoClient(MongoClientOptions) that creates a new MongoClient.\r\n\r\nThe client is configured to use credentials from its properties object, but it does not use the authenticationDatabase proptery. Instead it uses the configured database property when creating the Credentials object.\r\n\r\nSee:\r\n\r\n    credentials = Arrays.asList(MongoCredential.createMongoCRCredential(\r\n        this.username, getMongoClientDatabase(), this.password));\r\n\r\nI would expect here that getAuthenticationDatabase() is used instead of getMongoClientDatabase(), if available.\r\n\r\nAs a work-around I'm currently implementing the createMongoClient by myself and creating  credentials using the authentication database, see:\r\n\r\n    @Autowired\r\n    private MongoProperties properties;\r\n\r\n    @Bean\r\n    public Mongo mongo() throws UnknownHostException {\r\n        List<MongoCredential> credentials = null;\r\n        if (properties.getUsername() != null || properties.getPassword() != null) {\r\n            credentials = Arrays.asList(MongoCredential.createMongoCRCredential(\r\n                    properties.getUsername(), properties.getAuthenticationDatabase(), properties.getPassword()));\r\n        }\r\n        String host = properties.getHost() == null ? \"localhost\" : properties.getHost();\r\n        int port = properties.getPort() == null ? 27017 : properties.getPort();\r\n        return new MongoClient(Arrays.asList(new ServerAddress(host, port)),\r\n                credentials, options);\r\n    }\r\n\r\nThat solves the problem for me but, it's a rather special solution, I think the client database property should be used as fall-back if authentication database property is not set, but if it _is_ set, I would expect that it is used here."},"comment":{"url":"https://api.github.com/repos/spring-projects/spring-boot/issues/comments/76198940","html_url":"https://github.com/spring-projects/spring-boot/issues/2562#issuecomment-76198940","issue_url":"https://api.github.com/repos/spring-projects/spring-boot/issues/2562","id":76198940,"user":{"login":"wilkinsona","id":914682,"avatar_url":"https://avatars.githubusercontent.com/u/914682?v=3","gravatar_id":"","url":"https://api.github.com/users/wilkinsona","html_url":"https://github.com/wilkinsona","followers_url":"https://api.github.com/users/wilkinsona/followers","following_url":"https://api.github.com/users/wilkinsona/following{/other_user}","gists_url":"https://api.github.com/users/wilkinsona/gists{/gist_id}","starred_url":"https://api.github.com/users/wilkinsona/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wilkinsona/subscriptions","organizations_url":"https://api.github.com/users/wilkinsona/orgs","repos_url":"https://api.github.com/users/wilkinsona/repos","events_url":"https://api.github.com/users/wilkinsona/events{/privacy}","received_events_url":"https://api.github.com/users/wilkinsona/received_events","type":"User","site_admin":false},"created_at":"2015-02-26T15:36:04Z","updated_at":"2015-02-26T15:36:04Z","body":"@chli 1.2.2 is imminent. If you have a chance to test a snapshot (available from https://repo.spring.io/libs-snapshot) it'd be much appreciated."}}