jamebal / jmal-cloud-server

JmalCloud It's a private cloud storage project that makes it simple and secure to manage your files in the cloud. JmalCloud 是一款私有云存储网盘项目,能够简单安全管理您的云端文件
https://jmalcloud.github.io
MIT License
304 stars 76 forks source link

给mongodb加上用户密码验证--结果java连接报错 #51

Closed 593769290 closed 6 months ago

593769290 commented 6 months ago

默认mongodb没有开启权限认证,需要加上用户密码验证,但验证结果java连接时报错,希望能核查修复一下。

1、默认mongodb没有开启权限认证,这不安全,不安全,不安全,加上用户密码验证配置方法也是很简单。 1.1 docker-compose.yml 文件做一些配置参数。

version: "3"
services:
  jmalcloud:
    container_name: jmalcloud_server
    image: jmal/jmalcloud:latest
    environment:
      - MONGODB_URI: mongodb://root:mogopwd@mongo:27017/jmalcloud
      - TZ: Asia/Shanghai
    volumes:
      - ./docker/jmalcloud/files:/jmalcloud/files/
    ports:
      - 7072:8088
    restart: unless-stopped
  mongo:
    container_name: jmalcloud_mongodb
    image: mongo:4.4
    environment:
      - TZ: Asia/Shanghai
      - MONGO_INITDB_ROOT_USERNAME=root
      - MONGO_INITDB_ROOT_PASSWORD=mogopwd
    volumes:
      - ./docker/jmalcloud/mongodb/data/db:/data/db
    restart: unless-stopped
    command: --wiredTigerCacheSizeGB 0.5

mongodb的数据目录需要清理掉,重新初始化mongodb[./docker/jmalcloud/mongodb ] docker-compose down docker-compose up -d

1.2 增加验证后,连接mongodb也是很简单的 docker exec -ti jmalcloud_mongodb /bin/sh -c "mongo -uroot -pmogopwd --authenticationDatabase admin jmalcloud"

2、测试验证 按上面的配置启动docker-compose.yml 相关应用。

docker-compose up -d
docker-compose ps
      Name                     Command               State                                       Ports                                     
-------------------------------------------------------------------------------------------------------------------------------------------
jmalcloud_drawio    /docker-entrypoint.sh cata ...   Up      8080/tcp, 8443/tcp                                                            
jmalcloud_mongodb   docker-entrypoint.sh --wir ...   Up      0.0.0.0:27077->27017/tcp,:::27077->27017/tcp                                  
jmalcloud_nginx     /docker-entrypoint.sh ngin ...   Up      0.0.0.0:7080->80/tcp,:::7080->80/tcp, 0.0.0.0:7089->8089/tcp,:::7089->8089/tcp
jmalcloud_office    /app/ds/run-document-server.sh   Up      443/tcp, 80/tcp                                                               
jmalcloud_server    /__cacert_entrypoint.sh /b ...   Up      8088/tcp  

2.1 cat conn_mongo.sh docker exec -ti jmalcloud_mongodb /bin/sh -c "mongo -uroot -pmogopwd --authenticationDatabase admin jmalcloud"

2.2 也可以映射mongodb 的port 到主机端口号 27077,使用客户端连接 微信图片_20240518161713 微信图片_20240518161744

3、测试失败,报错信息如下:

3.1 为了直接测试,把jar 文件等迁移到主机上使用java -jar xxx.jar --debug 运行, cat run.sh /data/devops/jmalcloud/temp/openjdk17/bin/java -Dfile.encoding=UTF-8 -Dloader.path=./clouddisk-lib -jar -Xms50m -Xmx512m clouddisk-2.8.2.jar --debug --spring.profiles.active=prod --spring.data.mongodb.uri=mongodb://root:mogopwd@192.168.1.219:27077/jmalcloud?authSource=admin --file.monitor=true --file.rootDir=./files --logging.level.root=warn --file.ip2region-db-path=./ip2region.xdb

3.2 在命令行 和 客户端可以连接上数据库的情况下,mongodb://root:mogopwd@192.168.1.219:27077/jmalcloud?authSource=admin 这个连接配置生效的情况下,程序debug模式报错如下:

:: jmalcloud :: (v2.8.2) :: Spring Boot :: (v3.2.4) :: JDK Version :: (v17.0.11)

2024-05-18 16:10:05.488 |DEBUG |main |SpringApplication.java:687 |org.springframework.boot.SpringApplication |Loading source class com.jmal.clouddisk.ClouddiskApplication 2024-05-18 16:10:05.553 |DEBUG |main |AbstractApplicationContext.java:671 |org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext |Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@654b72c0 2024-05-18 16:10:06.084 |DEBUG |main |YamlProcessor.java:200 |org.springframework.boot.env.OriginTrackedYamlLoader |Loading from YAML: class path resource [file.yml] 2024-05-18 16:10:06.097 |DEBUG |main |YamlProcessor.java:264 |org.springframework.boot.env.OriginTrackedYamlLoader |Merging document (no matchers set): {file={rootDir=/Users/jmal/temp/filetest/rootpath, documentDir=/Document/, documentImgDir=/Image/Document/, chunkFileDir=ugyuvgbhnouvghjbnk, videoTranscodeCache=videoTranscodeCache, luceneIndexDir=luceneIndex, monitor=true, timeInterval=3, ftpServerPort=8089, simText=[txt, html, htm, xhtml, css, less, sass, scss, js, ts, jsx, tsx, json, xml, csv, tsv, md, markdown, rst, yaml, yml, ini, toml, cfg, conf, log, bat, cmd, sh, bash, zsh, ps1, py, pyw, pyc, pyo, pyd, rb, erb, pl, pm, t, php, phtml, phps, java, jsp, jspx, jsf, jws, jsp, jtpl, scala, kt, kts, groovy, gvy, gy, gsh, swift, c, cc, cpp, cxx, h, hh, hpp, hxx, cs, csx, vb, fs, fsx, fsi, ml, mli, go, rs, rlib, d, asm, s, sql, pgsql, psql, plpgsql, pls, plb, plsql, sqlite, db, dbf, mdb, accdb, cbl, cob, cpy, tcl, tk, lua, hs, erl, hrl, ex, exs, clj, cljs, edn, lisp, lsp, scm, rkt, ss, sml, v, sv, svh, vhd, vhdl, ino, pde, bsv, f, f90, f95, f03, f08, for, f77, f18, vba, vb, bas, cls, frm, frx, tex, latex, ltx, bib, bbl, sty, cls, dtx, ins, rst, rest, org, asciidoc, adoc, asc, pod, pov, mmd, mn, muse, creole, wiki, dokuwiki, vimwiki, haddock, jsdoc, pydoc, rdoc, yard, doxygen, roxygen2, javadoc, xmldoc, html, xhtml, htm, shtm, shtml, mht, mhtml, hdml, tpl, tmpl, vue, ejs, hbs, haml, pug, jade, slim, mustache, handlebars, nunjucks, liquid, jinja2, jinja, jnj, j2, njk, twig, swig, poi, t4, tt, tt2, tpl, eta, ect, coffee, litcoffee, dart, diff, patch, hs, x, xi, xmi, xaml, kml, wsdl, plist, nfo, srt, sub, sbv, vtt, bml, mrl, irl, log, changelog, CHANGELOG, license, LICENCE, LICENSE, copying, COPYING, readme, README, todo, TODO, contributing, CONTRIBUTING, authors, AUTHORS, dockerfile, Dockerfile, code-workspace, jsconfig, tsconfig, jshintrc, jscsrc, eslintrc, eslintignore, babelrc, browserconfig, webmanifest, htaccess, gitlab-ci, travis, circleci, jenkinsfile, prettierrc, stylelintrc, lintstagedrc, commitlintrc], document=[pdf, doc, docx, xlsx, xls, xl, md, ppt, pptx], web-dav-prefix=webDAV, ip2region-db-path=/Users/jmal/studio/myProject/github/jmal-cloud-server/docker/ip2region.xdb}} 2024-05-18 16:10:06.101 |DEBUG |main |YamlProcessor.java:212 |org.springframework.boot.env.OriginTrackedYamlLoader |Loaded 1 document from YAML resource: class path resource [file.yml] 2024-05-18 16:10:07.072 |DEBUG |main |AutoConfigurationPackages.java:213 |org.springframework.boot.autoconfigure.AutoConfigurationPackages |@EnableAutoConfiguration was declared on a class in the package 'com.jmal.clouddisk'. Automatic @Repository and @Entity scanning is enabled. Exception in thread "pool-4-thread-1" org.springframework.data.mongodb.UncategorizedMongoDbException: Command failed with error 13 (Unauthorized): 'command aggregate requires authentication' on server 192.168.1.219:27077. The full response is {"ok": 0.0, "errmsg": "command aggregate requires authentication", "code": 13, "codeName": "Unauthorized"} at org.springframework.data.mongodb.core.MongoExceptionTranslator.translateExceptionIfPossible(MongoExceptionTranslator.java:135) at org.springframework.data.mongodb.core.MongoTemplate.potentiallyConvertRuntimeException(MongoTemplate.java:2997) at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:603) at org.springframework.data.mongodb.core.MongoTemplate.doExactCount(MongoTemplate.java:1225) at org.springframework.data.mongodb.core.MongoTemplate.doCount(MongoTemplate.java:1194) at org.springframework.data.mongodb.core.MongoTemplate$ExistsCallback.doInCollection(MongoTemplate.java:3106) at org.springframework.data.mongodb.core.MongoTemplate$ExistsCallback.doInCollection(MongoTemplate.java:3089) at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:601) at org.springframework.data.mongodb.core.MongoTemplate.exists(MongoTemplate.java:848) at org.springframework.data.mongodb.core.MongoTemplate.exists(MongoTemplate.java:833) at com.jmal.clouddisk.service.impl.MenuService.lambda$init$0(MenuService.java:63) at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183) at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1625) at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:509) at java.base/java.util.stream.ForEachOps$ForEachTask.compute(ForEachOps.java:290) at java.base/java.util.concurrent.CountedCompleter.exec(CountedCompleter.java:754) at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:373) at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1182) at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1655) at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1622) at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:165) Caused by: com.mongodb.MongoCommandException: Command failed with error 13 (Unauthorized): 'command aggregate requires authentication' on server 192.168.1.219:27077. The full response is {"ok": 0.0, "errmsg": "command aggregate requires authentication", "code": 13, "codeName": "Unauthorized"} at com.mongodb.internal.connection.ProtocolHelper.getCommandFailureException(ProtocolHelper.java:205) at com.mongodb.internal.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:454) at com.mongodb.internal.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:372) at com.mongodb.internal.connection.UsageTrackingInternalConnection.sendAndReceive(UsageTrackingInternalConnection.java:114) at com.mongodb.internal.connection.DefaultConnectionPool$PooledConnection.sendAndReceive(DefaultConnectionPool.java:765) at com.mongodb.internal.connection.CommandProtocolImpl.execute(CommandProtocolImpl.java:76) at com.mongodb.internal.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:209) at com.mongodb.internal.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:115) at com.mongodb.internal.connection.DefaultServerConnection.command(DefaultServerConnection.java:83) at com.mongodb.internal.connection.DefaultServerConnection.command(DefaultServerConnection.java:74) at com.mongodb.internal.connection.DefaultServer$OperationCountTrackingConnection.command(DefaultServer.java:299) at com.mongodb.internal.operation.SyncOperationHelper.createReadCommandAndExecute(SyncOperationHelper.java:273) at com.mongodb.internal.operation.SyncOperationHelper.lambda$executeRetryableRead$3(SyncOperationHelper.java:191) at com.mongodb.internal.operation.SyncOperationHelper.lambda$withSourceAndConnection$0(SyncOperationHelper.java:127) at com.mongodb.internal.operation.SyncOperationHelper.withSuppliedResource(SyncOperationHelper.java:152) at com.mongodb.internal.operation.SyncOperationHelper.lambda$withSourceAndConnection$1(SyncOperationHelper.java:126) at com.mongodb.internal.operation.SyncOperationHelper.withSuppliedResource(SyncOperationHelper.java:152) at com.mongodb.internal.operation.SyncOperationHelper.withSourceAndConnection(SyncOperationHelper.java:125) at com.mongodb.internal.operation.SyncOperationHelper.lambda$executeRetryableRead$4(SyncOperationHelper.java:189) at com.mongodb.internal.operation.SyncOperationHelper.lambda$decorateReadWithRetries$12(SyncOperationHelper.java:292) at com.mongodb.internal.async.function.RetryingSyncSupplier.get(RetryingSyncSupplier.java:67) at com.mongodb.internal.operation.SyncOperationHelper.executeRetryableRead(SyncOperationHelper.java:194) at com.mongodb.internal.operation.SyncOperationHelper.executeRetryableRead(SyncOperationHelper.java:176) at com.mongodb.internal.operation.AggregateOperationImpl.execute(AggregateOperationImpl.java:193) at com.mongodb.internal.operation.AggregateOperation.execute(AggregateOperation.java:153) at com.mongodb.internal.operation.CountDocumentsOperation.execute(CountDocumentsOperation.java:134) at com.mongodb.internal.operation.CountDocumentsOperation.execute(CountDocumentsOperation.java:41) at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(MongoClientDelegate.java:153) at com.mongodb.client.internal.MongoCollectionImpl.executeCount(MongoCollectionImpl.java:227) at com.mongodb.client.internal.MongoCollectionImpl.countDocuments(MongoCollectionImpl.java:197) at org.springframework.data.mongodb.core.MongoTemplate.lambda$doExactCount$15(MongoTemplate.java:1226) at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:601) ... 18 more ^C2024-05-18 16:10:09.711 |DEBUG |main |DocumentRoot.java:81 |org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory |Code archive: /data/devops/jmalcloud/temp/clouddisk-lib/spring-boot-3.2.4.jar 2024-05-18 16:10:09.712 |DEBUG |main |DocumentRoot.java:125 |org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory |Code archive: /data/devops/jmalcloud/temp/clouddisk-lib/spring-boot-3.2.4.jar 2024-05-18 16:10:09.712 |DEBUG |main |DocumentRoot.java:149 |org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory |None of the document roots [src/main/webapp, public, static] point to a directory and will be ignored. 2024-05-18 16:10:09.741 |INFO |main |TomcatWebServer.java:109 |org.springframework.boot.web.embedded.tomcat.TomcatWebServer |Tomcat initialized with port 8088 (http)

593769290 commented 6 months ago

我的完整的 docker-compose.yml 如下:

version: "3"
services:
  mongo:
    container_name: jmalcloud_mongodb
    image: mongo:4.4.29
    environment:
      - TZ=Asia/Shanghai
      - MONGO_INITDB_ROOT_USERNAME=root
      - MONGO_INITDB_ROOT_PASSWORD=mogopwd
    volumes:
      - ./mongodb/data/db:/data/db
    restart: unless-stopped
    ports:
      - 27077:27017
    command: --wiredTigerCacheSizeGB 0.5

  jmalcloud:
    container_name: jmalcloud_server
    image: jmal/jmalcloud:latest
    environment:
      - MONGODB_URI=mongodb://root:mogopwd@mongo:27017/jmalcloud?authSource=admin
      - TZ=Asia/Shanghai
    volumes:
      - ./jmalcloud/files:/jmalcloud/files/
    restart: unless-stopped

  nginx:
    container_name: jmalcloud_nginx
    image: jmal/jmalcloud-nginx:latest
    ports:
      - 7080:80
      - 7089:8089
    environment:
      TZ: Asia/Shanghai
    links:
      - jmalcloud
      - office
      - drawio-webapp
    restart: unless-stopped

  office: 
    container_name: jmalcloud_office
    image: onlyoffice/documentserver:7.0.0.132
    environment:
      TZ: Asia/Shanghai
    restart: unless-stopped

  drawio-webapp: 
    container_name: jmalcloud_drawio
    image: jgraph/drawio:20.2.3
    environment:
      TZ: Asia/Shanghai
    restart: unless-stopped
jamebal commented 6 months ago

下个版本修复uri连接问题