java


Spring + Hibernate auditing (no Spring Data)


I have joined a project that was built for a year with Spring and Hibernate with Agile methodology and it didn't use Spring Data and has its own user class (not descendent of org.springframework.security.core.userdetails.User ).
Auditing came up as a new feauture to be implemented. (It's not enough to log the what controllers were called, we have to log every DB field's change, and which user did it. One controller can cause a lot of DB fields to change.) What are my options?
I have been looking at Javers, Envers and Audit4j. Javers requires Spring Data, so it's out. Envers' only requirement is Hibernate, which is statisfied, but I still don't see how the User object will be passed to it from the controller (the User's auth token is in the request object).
Hibernate Envers only gives you versioning (what have happened to an object at what time), but it doesn't help you figuring out who did the change.
A simple solution to the who-part is to create two new fields on all objects that shall be audited (i.e. in a base class such as AbstractAuditableEntity): User createdBy; and User lastModifiedBy;. Add two methods (I'm assuming you're using Spring Security here):
#PrePersist
protected void setCreatedBy() {createdBy = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();}
#PreUpdate
protected void setCreatedBy() {lastModifiedBy = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();}

Related Links

Main function in java?
Java cannot find constructor
Java Beginner: Scanner Console and two 6-sided dice problem
Disable all checkstyle checks for a specific java package
How to have a HashMap as #WebParam with JBossWS 3.1.2
Java Regex check previous char before splitting
Help in Understanding Spring MVC Workflow
Casting to specific class in HQL
What is the garbage collector in Java?
JAX-WS Servlet destroy()
What's wrong with this For loop?
android service persistent
Getting statistics of the activity of my engine
converting Java code to Android
How to create video,audio chat system in rails,ruby or using node
Does WindowTester work for AWT UIs?

Categories

HOME
client
zeromq
gremlin
include
session
electron
setup-deployment
google-oauth
analysis
microservices
sharepoint-designer
youtube-api-v3
packages
jgroups
callback
enterprise-library-5
echarts
wamp
azure-storage-tables
pheatmap
dacpac
wheelnav.js
mapserver
size
orchardcms
ibm-odm
percona
django-admin
http-status-code-504
pythonanywhere
plunker
object-detection
jasonette
arabic
sox
google-search-api
core-text
android-widget
one-to-many
fgetcsv
atlassian-crucible
nouislider
phonegap
siesta-swift
textmate
language-concepts
sequential
bitbucket-pipelines
automake
multilingual
libvpx
occlusion
jna
ansible-playbook
email-templates
avro4s
gzipstream
janrain
checkboxlist
dds
hendrix
rdfs
git-diff
flutterwave
mcafee
iso8601
orthogonal
chord-diagram
parallel-data-warehouse
xcb
google-feed-api
ado.net-entity-data-model
fody
actionbardrawertoggle
essence
hexdump
netmq
jmeter-maven-plugin
qdialog
sframe
responsive-images
vstest.console.exe
ami
streambase
javax.mail
tween
clicktag
rdtsc
device-manager
centos5
jsctypes
listings
gwt-rpc
mqx
flashvars
libstdc++
dice
dsn
errai
online-compilation
doh
zpt
xmlspy
html-input
web-application-design
icanhaz.js
ugc
rtml

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App