在Spring Batch中使用Spring Security

ver*_*tti 3 spring spring-security spring-batch

我有一个使用@PreAuthorize注释保护的Spring Service层.到目前为止,这已经通过Web应用程序使用.现在我需要启用SpringBatch作业来使用这些服务.

在调用这些服务方法之前,让作业授权的最直接的方法是什么?

c4k*_*c4k 6

我遇到了同样的问题,这就是我要解决的问题:

在我的tasklet中,我调用了这个方法:

securityUtility.authenticateAs("login", "password");
Run Code Online (Sandbox Code Playgroud)

以下是我在我的tasklet中注入的SecurityUtility类中编写的authenticateAs方法的定义,实现了ApplicationContextAware:

/**
 * Authenticate a user
 * @param username
 * @param password
 * @return
 */
public Authentication authenticateAs(String username, String password) {
    ProviderManager providerManager = (ProviderManager)applicationContext.getBean("authenticationManager");
    Authentication authentication = providerManager.authenticate(new UsernamePasswordAuthenticationToken(username, password));
    setAuthentication(authentication);
    return authentication;
}
Run Code Online (Sandbox Code Playgroud)

它运作良好,如果您需要更多细节请告诉我;)