2012-10-11 62 views
3

我有一個彈簧服務層與@PreAuthorize註釋安全。到目前爲止,這已通過Web應用程序使用。現在我需要啓用我們的SpringBatch作業來使用這些服務。使用彈簧安全與彈簧批

在調用這些服務方法之前,允許作業授權他們的最直接的方法是什麼?

回答

5

我有同樣的問題,這裏是我做過什麼來解決這個問題:

在我的微進程,我稱這種方法:

securityUtility.authenticateAs("login", "password"); 

這是寫在我的authenticateAs方法的定義我SecurityUtility類在我的微進程注入,實現了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; 
} 

它運作良好,讓我知道,如果你需要更多的細節;)

+0

是的,這與我最終解決我的問題的方式非常相似,只是一個簡單的自定義任務進行身份驗證。唯一令人討厭的是Spring Security似乎對Spring web有一些依賴性,所以我必須將Spring Web組件包含到我的構建中以避免有關缺少過濾器的警告。 – vertti