Java tutorial
/* * Copyright 20.09.16 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.orltom.sandbox.springboot.data.hibernate; import org.hibernate.EmptyInterceptor; import org.hibernate.type.Type; import org.slf4j.LoggerFactory; import org.springframework.security.core.context.SecurityContextHolder; import java.io.Serializable; import java.util.Optional; public class EntityInterceptor extends EmptyInterceptor { private static final org.slf4j.Logger log = LoggerFactory.getLogger(EntityInterceptor.class); @Override public boolean onLoad(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) { getUsername().ifPresent(u -> log.info(">>> User '{}' get Person with ID '{}'", u, id)); return true; } @Override public void onDelete(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) { getUsername().ifPresent(u -> log.info(">>> User '{}' delete Person with ID '{}'", u, id)); } private Optional<String> getUsername() { if (SecurityContextHolder.getContext() != null && SecurityContextHolder.getContext().getAuthentication() != null) { return Optional.of(SecurityContextHolder.getContext().getAuthentication().getName()); } return Optional.empty(); } }