2011-10-18 97 views
3

我試圖實施本question.訪問當前用戶,在彈簧控制器,getAuthentication()返回null

但前提是,他們都不是爲我工作的解決方案。在我的抽象基控制器,我有以下方法(我也試過在輔助類的靜態方法,但同樣的問題):

public User getUser() { 
    User user = 
    (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();  
} 

getAuthentication()方法返回null,即使在我的JSP,以下工作是完美的:

<sec:authentication property="principal.firstname" /> 

因爲我的自定義用戶對象是可訪問的。

爲什麼getAuthentication返回null?

另外,我在校長試圖傳遞作爲參數傳遞給我的具體控制器的方法,這是空解析爲空太...

爲什麼我的主要空,但尚未jsp的工作?

回答

3

請確保您的控制器的URL沒有filters = "none"<intercept-url>,它將禁用Spring Security過濾器,因此安全上下文不會被填充。

如果您需要讓所有人都可以訪問您的控制器,請使用other approaches