NutzCN Logo
问答 nutzmongo如何使用安全连接
发布于 2960天前 作者 佐蓝 1785 次浏览 复制 上一个帖子 下一个帖子
标签:

不用--auth时可以连接并插数据,
使用安全连接时,如下加上用户名和密码会报错,请问除了改配置文件还需要改其他的吗:

@Test
	public void insertTest() {
		// 插入操作
		User user = new User();
		user.setAge("12");
		user.setId("102");
		user.setName("tom1");
		userService.insert(user);
		List<User> users = userService.query(ZMoDoc.NEW("id", "102"));
		Assert.assertEquals(user, users.get(0));
	}
zMongo : {
		args : ["perfdba", "hope123", "127.0.0.1", 27017],
		factory : "org.nutz.mongo.ZMongo#me"
	},
org.nutz.ioc.IocException: [userService, zMongo] # FAIL to create Ioc Bean name=[zMongo]
	at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:149)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:210)
	at org.nutz.ioc.val.ReferTypeValue.get(ReferTypeValue.java:26)
	at org.nutz.ioc.weaver.FieldInjector.inject(FieldInjector.java:31)
	at org.nutz.ioc.weaver.DefaultWeaver.fill(DefaultWeaver.java:56)
	at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:135)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:210)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:239)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:146)
	at com.hoperun.elvish.UserServiceTest.setUp(UserServiceTest.java:29)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.nutz.lang.born.BorningException: Fail to born 'org.nutz.mongo.ZMongo'
 by args: [
  @(perfdba)
  @(hope123)
  @(127.0.0.1)
  @(27017)] becasue:
java.lang.IllegalArgumentException: source can not be null
	at org.nutz.lang.born.MethodBorning.born(MethodBorning.java:20)
	at org.nutz.ioc.weaver.DefaultWeaver.born(DefaultWeaver.java:67)
	at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:114)
	... 32 more
Caused by: java.lang.IllegalArgumentException: source can not be null
	at com.mongodb.assertions.Assertions.notNull(Assertions.java:37)
	at com.mongodb.MongoCredential.<init>(MongoCredential.java:233)
	at com.mongodb.MongoCredential.createMongoCRCredential(MongoCredential.java:144)
	at org.nutz.mongo.ZMongo.me(ZMongo.java:78)
	at org.nutz.mongo.ZMongo.me(ZMongo.java:69)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.nutz.lang.born.MethodBorning.born(MethodBorning.java:17)
	... 34 more

10 回复

需要把database写上

args : ["perfdba", "hope123", "127.0.0.1", 27017, "my-database"],

@wendal 兽总,刚加上了database,出现了权限不够的问题,用mongo连可以登录

Command failed with error 18: 'auth failed' on server 127.0.0.1:27017. The full response is { "ok" : 0.0, "errmsg" : "auth failed", "code" : 18 }

那肯定是密码不对吧?

@wendal 使用相同的密码在mongo可以登录,密码对的,使用的mongodb是3.2.10版本,nutzmongo是1.r.57版本,nutz是1.r.58,版本没问题吧

把mongo的命令贴一下

@wendal 是这个吗

> use nwimscmdb
switched to db nwimscmdb
> db.auth("perfdba","hope123")
1
>

https://github.com/nutzam/nutzmongo/commit/de4a021b91113dba83a7b6b7b03da524a5e43c99 中的java/src/org/nutz/mongo/ZMongo.java

可以用快照版,或者自定义一个工厂方法来适配

添加回复
请先登陆
回到顶部