我必須爲一個項目實現一個自定義的「身份驗證提供程序」,但是當嘗試訪問身份驗證的時候遇到了麻煩JSP中的對象屬性。詳細信息: 我的自定義驗證提供商成功地創建了一個驗證對象Spring Security - bean類的無效屬性'principal'[org.springframework.security.authentication.UsernamePasswordAuthenticationToken]
Authentication auth = new UsernamePasswordAuthenticationToken(username, password, getAuthorities(userRoles));
log.info("User is authenticated");
return auth;
(這裏只是相關的代碼)
然後,在控制器的方法,我只是顯示與用戶名日誌消息(這證明了被創建並放置在安全上下文中)認證對象:
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
log.info("Welcoming user " + auth.getPrincipal());
然後在JSP頁面中我想顯示的用戶名使用
<sec:authentication property="principal"/>
然而,這會引發錯誤500:
org.springframework.beans.NotReadablePropertyException: Invalid property 'principal' of bean class [org.springframework.security.authentication.UsernamePasswordAuthenticationToken]: Bean property 'principal' is not readable...
我也注意到,
<sec:authorize ifAnyGranted="role">...
不工作,雖然用戶在認證對象添加了必要的作用。
有什麼我做錯了嗎?身份驗證正常工作,我只是無法訪問身份驗證對象的屬性。
非常感謝你,祝你有美好的一天。
你使用的是什麼版本的Spring Framework和Spring Security? – Xaerxess
對不起,忘了提及 - Spring Framework 3.1.0和Spring Security 3.1.0 –