SecurityContext
сохраняется в ThreadLoacal
.Вы можете использовать следующие коды, чтобы создать поддельного администратора и установить его на SecurityContext
перед запуском sync()
:
List<GrantedAuthority> grantedAuthorities = new ArrayList<>();
//This is the permission that the admin should have. It depends on your application security configuration.
grantedAuthorities.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
// Here it does not matter what values username and password are.
// Just ensure this user has the the Admin GrantedAuthority and his account is enabled
User user = new User("admin", "password", true, true, true, true, grantedAuthorities);
Authentication authentication = new UsernamePasswordAuthenticationToken(user, user.getPassword(), user.getAuthorities());
SecurityContextHolder.getContext().setAuthentication(authentication);
Если поток, выполняющий синхронизацию (), выделен для запланированной задачи натолько для запуска, вы можете оставить этот поток для этого фальшивого администратора.В противном случае вам нужно очистить этого фальшивого администратора от ThreadLocal
после запуска sync ():
SecurityContextHolder.clearContext();