Reputation: 7889
I have a Spring Service layer that is secured with @PreAuthorize
annotation. So far this has been used through a web application. Now I need to enable our SpringBatch jobs to use these services.
What's the most straight-forward way to enable jobs to authorize them before calling these service methods?
Upvotes: 3
Views: 3816
Reputation: 4426
I had the same problem, here is what I did to solve it :
In my tasklet, I called this method :
securityUtility.authenticateAs("login", "password");
Here is the definition of my authenticateAs method written in my SecurityUtility class injected in my tasklet, implementing 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;
}
It works well, let me know if you need more details ;)
Upvotes: 6