我有一个springboot应用程序,可以在SecurityContext post登录中使用身份验证。调用returns来持久化任何实体、getCurrentAuditor()方法,该方法将返回用于自动更新创建的日期列的当前原则。
我使用spring "awaitility“依赖创建了一个调度器。但是,此计划调用实体的更新。当调用update并检查spring身份验证时,即使我已经从前端登录,它还是以null的形式出现。从前端开始,我能够持久化其他实体并获得身份验证对象。
根据我的理解,这可能是因为当Springboot启动并独立地发出保存请求时,调度器就会启动。如果这种理解是正确的,我应该如何解决这个问题?
发布于 2022-02-17 05:02:05
如果Scheduler可以使用"system“用户更新实体,您可以执行如下操作,并在调度程序代码中执行身份验证:
public void authenticate() {
Authentication auth = authenticationManager.authenticate(getBatch());
SecurityContext sc = SecurityContextHolder.getContext();
sc.setAuthentication(auth);
}
public UsernamePasswordAuthenticationToken getBatch() {
return UsernamePasswordAuthenticationTokenBuilder.anUsernamePasswordAuthenticationToken()
.withCredentials(batchProperties.getPassword()).withUserCode(batchProperties.getUser()).withUserDto(
userDtoFactory.getBatch()).build();
}
https://stackoverflow.com/questions/71158430
复制相似问题