Data Change Recording Interceptor
Recording data changes and controlling operation security is very important in database operations. Mongo-Plus provides a data change recording interceptor
DataChangeRecorderInnerInterceptor
that can automatically log operations to the database and also support security threshold controls, such as limiting the number of batch updates or inserts.
DataChangeRecorderInnerInterceptor
DataChangeRecorderInnerInterceptor
is an interceptor provided by Mongo-Plus. It automatically records data changes during database operations and can control operations based on configured security thresholds, e.g., limiting the number of records in a single batch update or insert to no more than 1000.setExceptionMessage(String)
: Message when exceeding the thresholdsetIgnoredColumnList(List)
: Tables to ignoresetBatchUpdateLimit(Integer)
: Maximum number of records for batch updatessetDisplayCompleteData(Boolean)
: Whether to display complete data. If enabled,changedData
field can be very largeAdded in version 2.1.3:
enableSaveDatabase(BaseMapper)
: Enable saving the log information to the databasesetDatasourceName(String)
: Data source, default is obtained from contextisMasterDatasource(Boolean)
: Whether to use the default data source to save data. IfdatasourceName
is set, this setting is ignored, but switching the data source still works. Recommended to use the master data source to avoid consecutive switchessetDatabaseName(String)
: Database name, default is obtained from the contextsetCollectionName(String)
: Collection name, default isDATA_CHANGE_RECORD
Example
In SpringBoot, register DataChangeRecorderInnerInterceptor
as a Bean and configure other options as needed:
@Bean
public DataChangeRecorderInnerInterceptor dataChangeRecorderInnerInterceptor(BaseMapper baseMapper){
DataChangeRecorderInnerInterceptor dataChangeRecorderInnerInterceptor = new DataChangeRecorderInnerInterceptor();
// Max records for batch updates
dataChangeRecorderInnerInterceptor.setBatchUpdateLimit(100);
// Message when exceeding threshold
dataChangeRecorderInnerInterceptor.setExceptionMessage("Operation exceeds threshold");
// Tables to ignore
dataChangeRecorderInnerInterceptor.setIgnoredColumnList(new ArrayList<>());
// Display complete data
dataChangeRecorderInnerInterceptor.setDisplayCompleteData(true);
// Enable saving records to the database
dataChangeRecorderInnerInterceptor.enableSaveDatabase(baseMapper);
// Use master data source to save records
dataChangeRecorderInnerInterceptor.isMasterDatasource(true);
return dataChangeRecorderInnerInterceptor;
}
Using the Plugin
After configuring the plugin, use Mongo-Plus CRUD methods. The plugin will automatically record data changes and enforce security control:
If batch update, insert, or multi-_id delete operations exceed the configured threshold, the plugin will throw an exception.
Notes
- Ensure an appropriate security threshold is set to prevent unsafe batch operations.
- The plugin automatically records data changes, but you need to implement the logging logic yourself.
- Consider database performance and actual operation needs when configuring and using the plugin.
DataChangeRecorderInnerInterceptor
is a powerful tool to automatically record data changes and control operation safety. Proper configuration ensures database operation safety and data integrity. Always follow best practices to maintain system security and stability.