LoginSignup
0
0

【エラー】SpringでMongoDBに接続したけどCRUD操作できない

Posted at

はじめに

Spring BootでMongoDBのCRUD操作を実行しようとしたところ、エラーが発生しました。
エラー発生から解決に至るまでを紹介します。

使っているもの

Springが提供する公式ガイドを試していました。

  • Spring Boot(3.2.0)
  • Spring Boot Starter Data MongoDB(3.2.1)

【問題】delete操作を呼び出すと”not authorized”と言われてしまう

▼ エラー発生箇所のコードです。repository.deleteAll()の部分でエラーが発生します。

AccessingDataMongodbApplication.java
@SpringBootApplication
public class AccessingDataMongodbApplication implements CommandLineRunner {

	public static void main(String[] args) {
		SpringApplication.run(AccessingDataMongodbApplication.class, args);
	}
	
	@Autowired
	CustomerRepository repository;

	@Override
	public void run(String... args) throws Exception {
		repository.deleteAll();
		// 省略
	}
}

▼ コンソールに「MongoCommandException」が出力されます。

Caused by: com.mongodb.MongoCommandException: Command failed with error 13 (Unauthorized): 'not authorized on test to execute command { delete: "customer", ordered: true, $db: "test", lsid: { id: UUID("(省略)") } }' on server localhost:27017. The full response is {"ok": 0.0, "errmsg": "not authorized on test to execute command { delete: \"customer\", ordered: true, $db: \"test\", lsid: { id: UUID(\"(省略)\") } }", "code": 13, "codeName": "Unauthorized"}

▼ エラーメッセージのJSON形式の部分は以下の通りです。

{
  "ok": 0.0, 
  "errmsg": "not authorized on test to execute command { 
    delete: 'customer',
    ordered: true, 
    $db: 'test', 
    lsid: { id: UUID('(省略)') } 
  }", 
  "code": 13, 
  "codeName": "Unauthorized"
}

明らかに認証による問題が発生していることがわかります。

▼ アプリケーション⇔MongoDBの接続設定は以下の通りです。

application.properties
# MONGODB (MongoProperties)
spring.data.mongodb.uri=mongodb://user:pass@localhost:27017/test

ユーザ「user」でアクセス中です。

【原因】アクセス中のユーザにdeleteを実行する権限が無かった

同じ問題にぶち当たっている方がいました。原因はユーザの権限にあるようです。

▼ mongoshから、ユーザ「user」の情報を確認してみます。

test> db.getUser("user")
{
  _id: 'test.user',
  userId: new UUID("(省略)"),
  user: 'user',
  db: 'test',
  roles: [ { role: 'userAdmin', db: 'test' } ],
  mechanisms: [ 'SCRAM-SHA-1', 'SCRAM-SHA-256' ]
}
test>

userAdmin権限のユーザでアクセスしていました。
userAdmin権限は、ロールとユーザーの作成・変更はできますが、データ操作はできません。

つまり、delete操作を行える権限をもつユーザでアクセスするよう、修正しなければならないようです。

【解決方法】ユーザの権限を変更する

ユーザの権限をreadWrite権限に変更します。
readWrite権限は、全コレクションのデータ操作が可能です。すなわち、delete操作を実行できます。

MongoDBのコマンドを実行

mongoshから以下のコマンドを実行し、権限を変更します。

▼ ユーザの「userAdmin」権限を削除するコマンド

test> db.revokeRolesFromUser(
   "user",
   [
     { role: "userAdmin", db: "test" }
   ]
)
{ ok: 1 }
test>

▼ ユーザに「readWrite」権限を付与するコマンド

test> db.grantRolesToUser(
   "user",
   [
     { role: "readWrite", db: "test" }
   ]
)
{ ok: 1 }
test>

▼ ユーザの情報を取得して、変更できているか確認する

test> db.getUser("user")
{
  _id: 'test.user',
  userId: new UUID("(省略)"),
  user: 'user',
  db: 'test',
  roles: [ { role: 'readWrite', db: 'test' } ],
  mechanisms: [ 'SCRAM-SHA-1', 'SCRAM-SHA-256' ]
}
test>

roleの値が「readWrite」に変更されたことがわかります。

以上の変更を行ったところ、無事エラーは解消されました。

おわりに

MongoDBには他にもいろんな種類の権限があるようです。
今回は学習目的ですが、実務で扱う際は、ユースケースやセキュリティ要件に応じて適切な権限を検討しなければなりませんね。大変勉強になりました。

お読みいただきありがとうございました。この記事が誰かのお役に立つことを祈っております。

参考文献

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0