<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    paulwong

    Spring Boot Data Mongodb Starter自動配置那些坑

    正好做Mongodb主從復制嘗試使用Spring Boot Data Mongodb Starter插件鏈接訪問Mongodb數據庫集群。

    遇到的坑:

    • spring.data.mongodb.host和spring.data.mongodb.port形式不適合集群配置,會報host無法識別異常
    • spring.data.mongodb.uri中經常拋出authentication failed異常


    解決辦法:

    1.  對于第一個坑,請使用spring.data.mongodb.uri。如果使用了uri,則其余的host/username/password/db/auth-db這些全部無效。
    2.  對于第二個坑,請在spring.data.mongodb.uri中指定replicaSet和authsource,另外記得把所有集群節點服務器地址都列全。
      如果auth-db和db是同一個,則無需加authsource,如果不同,則加authsource=admin


    我沒有把authsource指定,所以一直報authentication failed異常。然后只好一點點去發掘問題點,最后查到在com.mongodb.ConnectionString類中的createCredentials中

    private MongoCredential createCredentials(final Map<String, List<String>> optionsMap, final String userName,
                                                  final char[] password) {
            AuthenticationMechanism mechanism = null;
            String authSource = (database == null) ? "admin" : database;
            String gssapiServiceName = null;
            String authMechanismProperties = null;

            for (final String key : AUTH_KEYS) {
                String value = getLastValue(optionsMap, key);

                if (value == null) {
                    continue;
                }

                if (key.equals("authmechanism")) {
                    mechanism = AuthenticationMechanism.fromMechanismName(value);
                } else if (key.equals("authsource")) {
                    authSource = value;
                } else if (key.equals("gssapiservicename")) {
                    gssapiServiceName = value;
                } else if (key.equals("authmechanismproperties")) {
                    authMechanismProperties = value;
                }
            }


            MongoCredential credential = null;
            if (mechanism != null) {
                switch (mechanism) {
                    case GSSAPI:
                        credential = MongoCredential.createGSSAPICredential(userName);
                        if (gssapiServiceName != null) {
                            credential = credential.withMechanismProperty("SERVICE_NAME", gssapiServiceName);
                        }
                        break;
                    case PLAIN:
                        credential = MongoCredential.createPlainCredential(userName, authSource, password);
                        break;
                    case MONGODB_CR:
                        credential = MongoCredential.createMongoCRCredential(userName, authSource, password);
                        break;
                    case MONGODB_X509:
                        credential = MongoCredential.createMongoX509Credential(userName);
                        break;
                    case SCRAM_SHA_1:
                        credential = MongoCredential.createScramSha1Credential(userName, authSource, password);
                        break;
                    default:
                        throw new UnsupportedOperationException(format("The connection string contains an invalid authentication mechanism'. "
                                                                               + "'%s' is not a supported authentication mechanism",
                                mechanism));
                }
            } else if (userName != null) {
                credential = MongoCredential.createCredential(userName, authSource, password);
            }

            if (credential != null && authMechanismProperties != null) {
                for (String part : authMechanismProperties.split(",")) {
                    String[] mechanismPropertyKeyValue = part.split(":");
                    if (mechanismPropertyKeyValue.length != 2) {
                        throw new IllegalArgumentException(format("The connection string contains invalid authentication properties. "
                                + "'%s' is not a key value pair", part));
                    }
                    String key = mechanismPropertyKeyValue[0].trim().toLowerCase();
                    String value = mechanismPropertyKeyValue[1].trim();
                    if (key.equals("canonicalize_host_name")) {
                        credential = credential.withMechanismProperty(key, Boolean.valueOf(value));
                    } else {
                        credential = credential.withMechanismProperty(key, value);
                    }
                }
            }
            return credential;
        }


    authSource默認會指向我們目標數據的數據庫。然而在身份驗證機制中我們通常需要指向admin。(非常想報粗口,代碼作者在這里腦袋被men擠了么)。所以需要強制指定authSource中指定。具體指定方式如下:

     

     

     

     

    mongodb://{用戶名}:{密碼}@{host1}:27017,{host2}:27017,{host3}:27017/{目標數據庫}?replicaSet={復制集名稱}&write=1&readPreference=primary&authsource={授權數據庫}

    posted on 2020-03-17 09:39 paulwong 閱讀(1948) 評論(0)  編輯  收藏 所屬分類: MONGODBSPRING BOOT

    主站蜘蛛池模板: 亚洲一级毛片免费看| 永久免费无码日韩视频| 又黄又爽一线毛片免费观看| 国产精品永久免费视频| 91亚洲国产成人久久精品| 免费国产人做人视频在线观看| 日韩av无码免费播放| 亚洲精品宾馆在线精品酒店| 国产精品亚洲片在线观看不卡| 久久不见久久见中文字幕免费| 成年网站免费入口在线观看| 亚洲性无码av在线| 亚洲第一视频在线观看免费| 亚欧在线精品免费观看一区| 爱情岛论坛免费视频| 亚洲免费电影网站| 亚洲伊人久久精品影院| 国外成人免费高清激情视频 | 牛牛在线精品观看免费正| 91亚洲自偷手机在线观看| 亚洲av无码不卡私人影院| 免费精品国产日韩热久久| 你是我的城池营垒免费观看完整版 | 免费一级毛片在播放视频| 3d动漫精品啪啪一区二区免费| 国产区图片区小说区亚洲区| 亚洲理论精品午夜电影| 精品亚洲视频在线观看| 在线观看成人免费视频| 久久久久av无码免费网| 免费精品久久天干天干| 免费看一级高潮毛片| 亚洲精品国产摄像头| 亚洲精品在线播放| 亚洲va在线va天堂va四虎 | 亚洲综合一区二区精品久久| 亚洲无码在线播放| 亚洲精品无码专区久久同性男| 免费黄色app网站| 成人性生交大片免费看无遮挡| 99爱在线观看免费完整版|