Java 在Jhipster中创建具有角色\管理员权限的@Scheduled task

Java 在Jhipster中创建具有角色\管理员权限的@Scheduled task,java,spring-security,jhipster,spring-scheduled,Java,Spring Security,Jhipster,Spring Scheduled,我使用的是Jhipster 4.14.4 我正在尝试创建一个@scheduled任务,该任务将执行某些需要管理员权限的操作 问题是,在默认情况下,当程序启动时,主线程上下文中没有设置身份验证,除非有人通过站点登录(或通过调用“api/authenticate”) 我正在寻找一种安全的方法,将管理员角色权限传递给我的@scheduled方法,这样它就可以执行必要的操作,但不会以我想不到的任何方式“泄漏”给其他用户 我还需要将此方法与可能在主线程上下文中设置身份验证的任何用户登录隔离开来,因为这样它

我使用的是Jhipster 4.14.4

我正在尝试创建一个@scheduled任务,该任务将执行某些需要管理员权限的操作

问题是,在默认情况下,当程序启动时,主线程上下文中没有设置身份验证,除非有人通过站点登录(或通过调用“api/authenticate”)

我正在寻找一种安全的方法,将管理员角色权限传递给我的@scheduled方法,这样它就可以执行必要的操作,但不会以我想不到的任何方式“泄漏”给其他用户

我还需要将此方法与可能在主线程上下文中设置身份验证的任何用户登录隔离开来,因为这样它将无法执行相关任务

到目前为止,我所做的是修改AsyncConfiguration类,如下所示:

  • 已实现的SchedulingConfigurer接口:

    @Configuration
    @EnableAsync
    @EnableScheduling
    public class AsyncConfiguration implements AsyncConfigurer, SchedulingConfigurer
    
  • 添加了一个新bean“scheduledTaskExecutor”(除了现有的默认“taskExecutor”):

  • 重写SchedulingConfigurer接口的ConfigureTasks()方法,并设置我创建的新计划程序:

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        taskRegistrar.setScheduler(scheduledTaskExecutor());
    }
    
  • 发生的情况是,我收到一个错误,需要完全身份验证

    org.springframework.security.authentication.InsufficientAuthenticationException: Full authentication is required to access this resource
    
    然后我尝试注入AuthenticationManager并使用它创建身份验证对象:

    Authentication authentication = this.authenticationManager.authenticate(new UsernamePasswordAuthenticationToken("{admin_username}","{admin_password}"));
    
    但我得到的是:

    ***************************
    APPLICATION FAILED TO START
    ***************************
    
    Description:
    
    Parameter 0 of method liquibase in 
    net.myCompany.myApp.config.DatabaseConfiguration required a bean of type 
    'org.springframework.core.task.TaskExecutor' that could not be found.
    
    
    Action:
    
    Consider defining a bean of type 'org.springframework.core.task.TaskExecutor' in your configuration.
    
    
    Process finished with exit code 0
    
    我非常感谢你在这个问题上的帮助


    谢谢

    在使用@EnableScheduling之前,您需要启用调度,因此请将其添加到配置类文件中-

    @使能调度

    公共类AsyncConfiguration实现AsyncConfigurer、SchedulingConfigurer

    ***************************
    APPLICATION FAILED TO START
    ***************************
    
    Description:
    
    Parameter 0 of method liquibase in 
    net.myCompany.myApp.config.DatabaseConfiguration required a bean of type 
    'org.springframework.core.task.TaskExecutor' that could not be found.
    
    
    Action:
    
    Consider defining a bean of type 'org.springframework.core.task.TaskExecutor' in your configuration.
    
    
    Process finished with exit code 0