i am using java 21, spring security 6 and keycloak version 22.0.4. Here i am trying to use audit aware to enable timestamps and records who created and updated. But the problem is i am getting currunt auditor id as null.
"Message": "Disability created successfully",
"HttpStatus": "OK",
"Data": {
type here
"id": "388465a1-f06b-4d28-acbb-9110f8905ce3",
"effectiveFrom": null,
"deletedAt": null,
"createdDate": "2024-05-14T16:49:17.655439",
"createdBy": null,
"lastModifiedDate": "2024-05-14T16:49:17.655439",
"lastModifiedBy": null,
"branchId": "395d5596-af9f-4acf-bcc5-89471098a24f",
"name": "uyu problem",
"notes": "Additional notes about the disability"
}
response from the create request in postman
i am expecting loged in user id in this created by
i am using getCurrunt auditor method
@Override
public Optional<UUID> get Current Auditor() {
Authentication authentication =
SecurityContextHolder
.getContext()
.getAuthentication();
if (authentication == null ||
!authentication.isAuthenticated() ||
authentication instanceof AnonymousAuthenticationToken) {
System.out.println("Authentication is null or not authenticated");
return Optional.empty();
}
Object principal = authentication.getPrincipal();
if (principal instanceof User userPrincipal) {
System.out.println("Auditing for user: " + userPrincipal.getUsername());
return Optional.ofNullable(userPrincipal.getId());
} else {
System.out.println("Principal is not a User entity: " + principal);
return Optional.empty();
}
i have tried some ways to figure it out but can not able to get the desired results
i have addded a bean in the security config file
`@Bean
public AuditorAware<UUID> auditorAware() {
return new ApplicationAuditAware();
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "UUID")
@Column(name = "id", updatable = false, nullable = false)
private UUID id;
/** Date and time when the record was effective from. */
@Column(name = "effective_from")
private Date effectiveFrom;
/** Deleted At timestamp when the record was deleted. Otherwise null */
@Column(
name = "deleted_at",
insertable = false
)
private LocalDateTime deletedAt;
/**
* The timestamp when the entity was created.
*/
@CreatedDate
@Column(
name = "created_date",
nullable = false,
updatable = false
)
private LocalDateTime createdDate;
/**
* The timestamp when the entity was last updated.
*/
@LastModifiedDate
@Column(
name = "last_modified_date",
insertable = false
)
private LocalDateTime lastModifiedDate;
/**
* The user who created the entity.
*/
@CreatedBy
@Column(
name = "created_by",
// nullable = false,
updatable = false
)
private UUID createdBy;
/**
* The user who last updated the entity.
*/
@LastModifiedBy
@Column(
name = "last_modified_by",
insertable = false
)
private UUID lastModifiedBy;
}`
i am still getting the same output which is not desired output
Priyanshu Thakur is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.