如何打印一些日志 "before" spring-data repository 方法,无需自定义 repo
How to print some logs "before" a spring-data repository method, without custom repo
我有一个 Spring 数据存储库。
当调用 http://localhost:8080/persons webservice 时,我想记录一些东西。我不想创建 MyCustomRepository<>。更清洁的选项?
回购 class:
@RepositoryRestResource(collectionResourceRel = "persons", path = "persons")
public interface PersonRepository extends PagingAndSortingRepository<Person, Long> {
List<Person> findByLastName(@Param("name") String name);
示例日志:
log.error("AccessToken: " + securityContext.getTokenString());
log.error("User: {} / {}", accessToken.getPreferredUsername(), accessToken.getName());
log.error("Principal: {}", principal.getName());
您可以创建一个方面来拦截对您的 PersonRepository
的调用。从那里您可以访问 OAuth2 访问令牌和安全上下文以检索主体。这是一个例子,
@Component
@Aspect
@Log
public class SecurityAspect {
@Autowired
private OAuth2ClientContext oauth2ClientContext;
@Pointcut("execution(public * my.example.repository.PersonRepository.*(..))")
public void pointcut() {
}
@Around("pointcut()")
public Object advice(ProceedingJoinPoint pjp) throws Throwable {
log.info(
"Entering SecurityAspect.advice() in class "
+ pjp.getSignature().getDeclaringTypeName()
+ " - method: " + pjp.getSignature().getName());
OAuth2AccessToken accessToken = oauth2ClientContext.getAccessToken();
log.info("AccessToken: " + accessToken);
if (SecurityContextHolder.getContext().getAuthentication()
instanceof OAuth2Authentication) {
OAuth2Authentication authentication =
(OAuth2Authentication) SecurityContextHolder.getContext().getAuthentication();
if (authentication.getUserAuthentication() instanceof UsernamePasswordAuthenticationToken) {
UsernamePasswordAuthenticationToken userToken =
(UsernamePasswordAuthenticationToken) authentication.getUserAuthentication();
log.info("Principal id: " + userToken.getPrincipal());
if (userToken.getDetails() instanceof Map) {
Map details = (Map) userToken.getDetails();
log.info("Principal Name: " + details.get("name"));
}
}
}
return pjp.proceed();
}
}
我有一个 Spring 数据存储库。 当调用 http://localhost:8080/persons webservice 时,我想记录一些东西。我不想创建 MyCustomRepository<>。更清洁的选项?
回购 class:
@RepositoryRestResource(collectionResourceRel = "persons", path = "persons")
public interface PersonRepository extends PagingAndSortingRepository<Person, Long> {
List<Person> findByLastName(@Param("name") String name);
示例日志:
log.error("AccessToken: " + securityContext.getTokenString());
log.error("User: {} / {}", accessToken.getPreferredUsername(), accessToken.getName());
log.error("Principal: {}", principal.getName());
您可以创建一个方面来拦截对您的 PersonRepository
的调用。从那里您可以访问 OAuth2 访问令牌和安全上下文以检索主体。这是一个例子,
@Component
@Aspect
@Log
public class SecurityAspect {
@Autowired
private OAuth2ClientContext oauth2ClientContext;
@Pointcut("execution(public * my.example.repository.PersonRepository.*(..))")
public void pointcut() {
}
@Around("pointcut()")
public Object advice(ProceedingJoinPoint pjp) throws Throwable {
log.info(
"Entering SecurityAspect.advice() in class "
+ pjp.getSignature().getDeclaringTypeName()
+ " - method: " + pjp.getSignature().getName());
OAuth2AccessToken accessToken = oauth2ClientContext.getAccessToken();
log.info("AccessToken: " + accessToken);
if (SecurityContextHolder.getContext().getAuthentication()
instanceof OAuth2Authentication) {
OAuth2Authentication authentication =
(OAuth2Authentication) SecurityContextHolder.getContext().getAuthentication();
if (authentication.getUserAuthentication() instanceof UsernamePasswordAuthenticationToken) {
UsernamePasswordAuthenticationToken userToken =
(UsernamePasswordAuthenticationToken) authentication.getUserAuthentication();
log.info("Principal id: " + userToken.getPrincipal());
if (userToken.getDetails() instanceof Map) {
Map details = (Map) userToken.getDetails();
log.info("Principal Name: " + details.get("name"));
}
}
}
return pjp.proceed();
}
}