如何解决管理系统用户进行审核
我有一个 Spring-Boot / Security 应用程序,该应用程序使用 Hibernate Envers 和自定义的 RevisionListener 来添加其他上下文信息。 感谢 SecurityContextHolder
,我从身份验证中获得了“当前用户”我的用例是,对于批处理操作,我希望使用批处理方法上的注释来验证“系统”用户的身份,以便我可以进行常规安全检查,然后将系统用户信息放入Envers的修订版中。
我不是Hibernate Envers生命周期(完成Revsion创建时)的专家
以下是我的坦率解决方案:
首先是RevisionListener:
public class CustomRevisionListener implements RevisionListener {
@Override
public void newRevision(Object audit) {
Optional.ofNullable(SecurityContextHolder.getContext().getAuthentication())
.filter(Authentication::isAuthenticated)
.ifPresentOrElse(authentication -> //Do the stuff,() -> throw new RuntimeException("Authentication is missing")
);
}
}
然后添加注释:
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface AsSystemUser {
}
负责认证的方面
@Aspect
public class SystemAuthenticationAspect {
@Around("@annotation(com.financeactive.hive.operations.security.annotation.AsSystemUser)")
public void around(ProceedingJoinPoint joinPoint) throws Throwable {
SecurityContext context = SecurityContextHolder.createEmptyContext();
context.setAuthentication(
new UsernamePasswordAuthenticationToken(
new org.springframework.security.core.userdetails.User(
"System","null",Collections.emptyList()
),null)
);
SecurityContextHolder.setContext(context);
joinPoint.proceed();
//SecurityContextHolder.clearContext();
}
}
最后是用法示例:
public class BatchService {
@AsSystemUser
public void process() {
//do the Job authenticated as System User
}
}
一切似乎都运行良好,除了我无法在joincut.proceed()之后清理SecurityContext时,因为版本创建是异步进行的
如果需要,我可以提供一个github示例项目
谢谢
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。