{"id":1947,"date":"2019-09-12T12:00:00","date_gmt":"2019-09-12T10:00:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/auditing-in-spring-bootstep-by-step-tutorial\/"},"modified":"2026-07-05T03:24:28","modified_gmt":"2026-07-05T01:24:28","slug":"auditing-in-spring-bootstep-by-step-tutorial","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/auditing-in-spring-bootstep-by-step-tutorial\/","title":{"rendered":"Auditing in Spring Boot(Step by Step Tutorial)"},"content":{"rendered":"<p>In this article, I would explain step by step, how to enable Auditing in Spring Boot application. We would consider the following:<\/p>\n<ol>\n<li><a href=\"#t1\">Introduction to Auditing<\/a><\/li>\n<li><a href=\"#t2\">Create an Auditable Class<\/a><\/li>\n<li><a href=\"#t3\">Make the Classes Extend Auditable<\/a><\/li>\n<li><a href=\"#t4\">Implement the AuditorAware Interface<\/a><\/li>\n<li><a href=\"#t5\">Create an AuditorAware Bean<\/a><\/li>\n<li><a href=\"#t6\">Test the Application<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p><strong id=\"t1\">1. Introduction to Auditing<\/strong><\/p>\n<p>Auditing helps you track changes to a table. For example, yo need to know:<\/p>\n<ul>\n<li>who modified the record last<\/li>\n<li>when was it modified?<\/li>\n<li>who created the record<\/li>\n<li>when was it crated?<\/li>\n<\/ul>\n<p>Before, now what I did was to add additional fields to my entities. So when a record is inserted, I have a code to insert the current time. Then I also insert the name of the current user. Now, you don&#8217;t have to worry about this. It is done automatically for you!<\/p>\n<p>So let&#8217;s get started.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong id=\"t2\">2. Create the Auditable Class<\/strong><\/p>\n<p>Here we would create an abstract class that holds the audit information. So any entity we would like to audit would extend this class. Follow the steps below:<\/p>\n<p><strong>Step 1:<\/strong>\u00a0 create an abstract class class Auditable. This should be in<span style=\"font-size: 1rem;\">\u00a0the models package.\u00a0<\/span><span style=\"font-size: 1rem;\">This class would have the following fields:<\/span><\/p>\n<ul>\n<li>CreatedBy<\/li>\n<li>CreatedDate<\/li>\n<li>LastModifiedBy<\/li>\n<li>LastModifiedDate<\/li>\n<\/ul>\n<p>TIMESTAMP\u00a0 is available with the namespace below:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #008800; font-weight: bold\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold\">static<\/span> javax<span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">persistence<\/span><span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">TemporalType<\/span><span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">TIMESTAMP<\/span><span style=\"color: #333333\">;<\/span>\n<\/pre>\n<p><strong>Step 2:<\/strong> Annotate the fields with annotations of the same name with the field. For example, CreatedBy is annotated with @CreatedBy<\/p>\n<p><strong>Step 3:<\/strong> Annotate the class with @MappedSuperclass annotation. This means that this class has not table created for it<\/p>\n<p><strong>Step 4:<\/strong> Annotate the class with @EntityListener annotation<\/p>\n<p><strong>Step 5:<\/strong> Generate the getters and the setters<\/p>\n<p>At the end, the class would be as shown below (getters and setters are omitted for brevity):<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555; font-weight: bold;\">@MappedSuperclass<\/span>\n<span style=\"color: #555555; font-weight: bold;\">@EntityListeners<\/span><span style=\"color: #333333;\">(<\/span>AuditingEntityListener<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">class<\/span><span style=\"color: #333333;\">)<\/span>\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">abstract<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Auditable<\/span><span style=\"color: #333333;\">&lt;<\/span>U<span style=\"color: #333333;\">&gt;<\/span> <span style=\"color: #333333;\">{<\/span>\n\n    <span style=\"color: #555555; font-weight: bold;\">@CreatedBy<\/span>\n    <span style=\"color: #008800; font-weight: bold;\">protected<\/span> U createdBy<span style=\"color: #333333;\">;<\/span>\n\n    <span style=\"color: #555555; font-weight: bold;\">@CreatedDate<\/span>\n    <span style=\"color: #555555; font-weight: bold;\">@Temporal<\/span><span style=\"color: #333333;\">(<\/span>TIMESTAMP<span style=\"color: #333333;\">)<\/span>\n    <span style=\"color: #008800; font-weight: bold;\">protected<\/span> Date createdDate<span style=\"color: #333333;\">;<\/span>\n\n    <span style=\"color: #555555; font-weight: bold;\">@LastModifiedBy<\/span>\n    <span style=\"color: #008800; font-weight: bold;\">protected<\/span> U lastModifiedBy<span style=\"color: #333333;\">;<\/span>\n\n    <span style=\"color: #555555; font-weight: bold;\">@LastModifiedDate<\/span>\n    <span style=\"color: #555555; font-weight: bold;\">@Temporal<\/span><span style=\"color: #333333;\">(<\/span>TIMESTAMP<span style=\"color: #333333;\">)<\/span>\n    <span style=\"color: #008800; font-weight: bold;\">protected<\/span> Date lastModifiedDate<span style=\"color: #333333;\">;<\/span>\n    \n    <span style=\"color: #888888;\">\/\/ getters and setter here<\/span>\n<span style=\"color: #333333;\">}<\/span>\n<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong id=\"t3\">3. Make the Classes Extend Auditable<\/strong><\/p>\n<p>Now that we have the Auditable class, we can now extend it. So any entity we want to audit have to extend this class. Follow the steps:<\/p>\n<p><strong>Step 1:<\/strong> Modify the Post, User and Location file to make then extend Auditable&lt;String&gt;<\/p>\n<p><strong>Step 2:<\/strong> Launch the application.<\/p>\n<p><strong>Step 3:<\/strong> Visit http:\/\/localhost:8080\/users. Check the the four additional field are now available.<\/p>\n<p><strong>Note:<\/strong> Follow step 4 if you have enabled h2-console<\/p>\n<p><strong>Step 4(Optional)<\/strong>: Visit http:\/\/localhost:8080\/h2-console. Check that the additional fields exist in the tables<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong id=\"t4\">4. Implement the AuditorAware Interface<\/strong><\/p>\n<p>Now, you need to create a class that implements the AuditorAware interface. This is the class the returns the current Auditor. That is the current user. Follow the steps below:<\/p>\n<p><strong>Step 1:<\/strong>\u00a0In the models package, create a class the implements AuditorAware<\/p>\n<p><strong>Step 2:<\/strong> Override the getCurrentAuditor method of this class. Just return a string which represents the current user<\/p>\n<p>The class is as shown below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">SpringSecurityAuditorAware<\/span> <span style=\"color: #008800; font-weight: bold;\">implements<\/span> AuditorAware<span style=\"color: #333333;\">&lt;<\/span>String<span style=\"color: #333333;\">&gt;<\/span> <span style=\"color: #333333;\">{<\/span>\n\n\t<span style=\"color: #555555; font-weight: bold;\">@Override<\/span>\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> Optional<span style=\"color: #333333;\">&lt;<\/span>String<span style=\"color: #333333;\">&gt;<\/span> <span style=\"color: #0066bb; font-weight: bold;\">getCurrentAuditor<\/span><span style=\"color: #333333;\">()<\/span> <span style=\"color: #333333;\">{<\/span>\n\t\t\n\t\t<span style=\"color: #888888;\">\/\/ Just return a string representing the username<\/span>\n\t\t<span style=\"color: #008800; font-weight: bold;\">return<\/span> Optional<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">ofNullable<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Kindson\"<\/span><span style=\"color: #333333;\">).<\/span><span style=\"color: #0000cc;\">filter<\/span><span style=\"color: #333333;\">(<\/span>s <span style=\"color: #333333;\">-&gt;<\/span> <span style=\"color: #333333;\">!<\/span>s<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">isEmpty<\/span><span style=\"color: #333333;\">());<\/span>\n\t<span style=\"color: #333333;\">}<\/span>\n\t\n<span style=\"color: #333333;\">}<\/span>\n<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong id=\"t5\">5. Create an AuditorAware Bean<\/strong><\/p>\n<p>Finally, we need to create a bean of type AuditorAware.<\/p>\n<p><strong>Step 1<\/strong>: Annotate the main application class with @EnableJpaAuditing as shown below:<\/p>\n<p>@EnableJpaAuditing(auditorAwareRef=&#8221;auditorAware&#8221;)<\/p>\n<p><strong>Step 2:<\/strong> Write a bean method that returns an AuditorAware object.\u00a0 At the end, the main application class would be as shown below:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555; font-weight: bold;\">@EnableJpaAuditing<\/span><span style=\"color: #333333;\">(<\/span>auditorAwareRef<span style=\"color: #333333;\">=<\/span><span style=\"background-color: #fff0f0;\">\"auditorAware\"<\/span><span style=\"color: #333333;\">)<\/span>\n<span style=\"color: #555555; font-weight: bold;\">@SpringBootApplication<\/span>\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">RelationshipDemoApplication<\/span> <span style=\"color: #333333;\">{<\/span>\n\t\n\t<span style=\"color: #555555; font-weight: bold;\">@Bean<\/span>\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> AuditorAware<span style=\"color: #333333;\">&lt;<\/span>String<span style=\"color: #333333;\">&gt;<\/span> <span style=\"color: #0066bb; font-weight: bold;\">auditorAware<\/span><span style=\"color: #333333;\">()<\/span> <span style=\"color: #333333;\">{<\/span>\n\t\t<span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> <span style=\"color: #0066bb; font-weight: bold;\">SpringSecurityAuditorAware<\/span><span style=\"color: #333333;\">();<\/span>\n\t<span style=\"color: #333333;\">}<\/span>\n\t\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">static<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span><span style=\"color: #333333;\">(<\/span>String<span style=\"color: #333333;\">[]<\/span> args<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\n\t\tSpringApplication<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">run<\/span><span style=\"color: #333333;\">(<\/span>RelationshipDemoApplication<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">class<\/span><span style=\"color: #333333;\">,<\/span> args<span style=\"color: #333333;\">);<\/span>\n\t<span style=\"color: #333333;\">}<\/span>\n\n<span style=\"color: #333333;\">}<\/span>\n<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong id=\"t6\">6. Test the Application<\/strong><\/p>\n<p><strong>Step 1:<\/strong> Launch the application<\/p>\n<p><strong>Step 2:<\/strong> Open Advanced Rest Client (or PostMan Rest Client).<\/p>\n<p><strong>Step 3:<\/strong> Perform an insert operation in one of the tables and check the that audit data is added as well.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, I would explain step by step, how to enable Auditing in Spring Boot application. We would consider the following: Introduction to Auditing &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pagelayer_contact_templates":[],"_pagelayer_content":"","footnotes":""},"categories":[414],"tags":[],"class_list":["post-1947","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1947","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/comments?post=1947"}],"version-history":[{"count":1,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1947\/revisions"}],"predecessor-version":[{"id":2115,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1947\/revisions\/2115"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=1947"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=1947"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=1947"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}