The version Spring Boot:2.3.0 MongoDB:4.2.7

The configuration for mongodb

spring:
  data:
    mongodb:
      uri: mongodb://root:12345678@localhost:27017/springboot?authSource=admin

I can run my unit test normally by using above configuration.But when I change it to:

spring:
  data:
    mongodb:
      host: localhost
      port: 27017
      database: springboot
      username: root
      password: 12345678
      authentication-database: admin

my unit test throws exception like this:

org.springframework.data.mongodb.UncategorizedMongoDbException: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-256, userName='root', source='admin', password=<hidden>, mechanismProperties=<hidden>}; nested exception is com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-256, userName='root', source='admin', password=<hidden>, mechanismProperties=<hidden>}

So, are there any differences between these two configurations? Shouldn't it be equivalent? Plz help me out.


The code

@SpringBootTest
public class MongodbTest {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Test
    public void test() {
        List<UserTest> insertList = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            UserTest userTest = new UserTest();
            userTest.setId(Long.valueOf(i));
            userTest.setName("name" + i);
            userTest.setAge(new Random().nextInt(30));
            userTest.setPhone("1000000" + i);
            insertList.add(userTest);
        }
        mongoTemplate.insertAll(insertList);
    }
}
@Document
@Data
public class UserTest implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    private Long id;

    @Field
    private String name;

    @Field
    private Integer age;

    @Field
    private String phone;
}

Comment From: wilkinsona

You haven't quoted the value of password so the YAML spec dictates that it be parsed it as a numeric value. This then gets turned into a char[] containing a single character rather than the characters 1, 2, 3, 4, 5, 7, 8. You can avoid the problem by quoting the value of password to indicate that it should be parsed as a string:

spring:
  data:
    mongodb:
      host: localhost
      port: 27017
      database: springboot
      username: root
      password: "12345678"
      authentication-database: admin

If you have any further questions, please follow up on Stack Overflow or Gitter. As mentioned in the guidelines for contributing, we prefer to use GitHub issues only for bugs and enhancements.

Comment From: MoncyXu

OK, thanks. I'll try it.