![](/img/trans.png)
[英]sec:authorize="hasRole('ROLE_ADMIN')" in angular
[英]Creating a @Scheduled task with ROLE_ADMIN permissions in Jhipster
我正在使用Jhipster 4.14.4
我正在嘗試創建一個@scheduled任務,該任務將執行某些需要管理員權限的操作。
問題在於,默認情況下,在啟動程序時,除非有人通過站點登錄(或通過調用“ api / authenticate”)登錄,否則在主線程的上下文中未設置身份驗證。
我正在尋找一種將ADMIN_ROLE權限傳遞給我的@scheduled方法的安全方法,以便它可以執行必要的操作,但不會以任何我無法想到的方式“泄漏”給其他用戶。
我還需要將此方法與可能在主線程的上下文中設置Authentication的任何用戶登錄隔離,因為那樣便無法執行相關任務。
到目前為止,我所做的是如下修改AsyncConfiguration類:
實現了SchedulingConfigurer接口:
@Configuration @EnableAsync @EnableScheduling public class AsyncConfiguration implements AsyncConfigurer, SchedulingConfigurer
添加了一個新的bean“ scheduledTaskExecutor”(除了現有的默認“ taskExecutor”之外):
@Bean(name = "scheduledTaskExecutor") public Executor scheduledTaskExecutor() { ScheduledExecutorService delegateExecutor = Executors.newSingleThreadScheduledExecutor(); SecurityContext context = SecurityContextHolder.createEmptyContext(); Authentication authentication = new UsernamePasswordAuthenticationToken("{admin_username}","{admin_password}"); context.setAuthentication(authentication); return new DelegatingSecurityContextScheduledExecutorService(delegateExecutor, context); }
覆蓋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 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,將其添加到配置類文件中-
@EnableScheduling
公共類AsyncConfiguration實現AsyncConfigurer,SchedulingConfigurer
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.