Golang MongoDB密码特殊符号的处理

69 阅读1分钟

方法1 直接拼装password

URI := fmt.Sprintf("mongodb://%s:%s@%s:%s", c.UserName, c.Password, c.Host, c.Port)

方法2 转义password

c.URI = escapeCharacter(c.URI)  
  
o := options.Client().ApplyURI(c.URI)

方法3 使用*options.ClientOptions字段Atuth的Credential

URI := fmt.Sprintf("mongodb://%s:%s", c.Host, c.Port)  
  
o := options.Client().ApplyURI(URI)

o.Auth = &options.Credential{  
//AuthMechanism: "",  
//AuthMechanismProperties: nil,  
//AuthSource: "",  
Username: c.UserName,  
Password: c.Password,  
//PasswordSet: false,  
}