编程语言
首页 > 编程语言> > java-如何将Spring Security与Spring Batch集成在一起?

java-如何将Spring Security与Spring Batch集成在一起?

作者:互联网

我想创建一个安全的Web应用程序,用户可以在其中启动Spring Batch作业.我想跟踪谁启动了哪个作业,因此必须将JobInstance(或JobExecution?)与用户相关联.我还想查询由特定用户启动的JobExecutions.这似乎并不常见,因为我无法在参考指南中找到解决方案.

我是否需要为Spring Batch的持久性层编写自己的实现?仅将字段添加到表中,这似乎有很多开销.有人知道一个简单的解决方案吗?

解决方法:

正如@Artem Bilan所建议的,我将当前用户注入到JobParameters中.我编写了一个简单的服务类,该类提供了一种查询给定用户的执行ID的方法:

@Service
public class JobService {

    private static final String FIND_EXECUTIONS = "SELECT JOB_EXECUTION_ID FROM batch_job_execution_params WHERE KEY_NAME = 'user' AND STRING_VAL = ? ORDER BY JOB_EXECUTION_ID DESC";

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public List<Long> findJobExecutionIdsByUser(String username) {
        return jdbcTemplate.queryForList(FIND_EXECUTIONS, Long.class, username);
    }

}

使用此列表,我可以像这样从默认JobExplorer获取JobExecutions:

final List<Long> executionIds = jobService.findJobExecutionIdsByUser("user");
final List<JobExecution> executions = new ArrayList<>();

for (Long executionId : executionIds) {
    final JobExecution execution = explorer.getJobExecution(executionId);

    if (execution != null) {
        executions.add(execution);
    }
}

标签:spring-security,spring-batch,spring,java
来源: https://codeday.me/bug/20191120/2045150.html