【发布时间】:2012-09-21 12:42:25
【问题描述】:
我必须为一个项目实现一个自定义的“身份验证提供程序”,但是当我尝试在 JSP 中访问身份验证的对象属性时遇到了麻烦。细节: 我的自定义身份验证提供程序成功创建了一个身份验证对象
Authentication auth = new UsernamePasswordAuthenticationToken(username, password, getAuthorities(userRoles));
log.info("User is authenticated");
return auth;
(这里只写相关代码)
然后,在控制器方法中,我只是显示带有用户名的日志消息(这证明 Authentication 对象已创建并放置在安全上下文中):
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">...
不起作用,尽管用户在 Authentication 对象中添加了必要的角色。
我做错了什么吗?身份验证工作正常,我只是无法访问身份验证对象的属性。
非常感谢您,祝您有美好的一天。
【问题讨论】:
-
你使用什么版本的 Spring Framework 和 Spring Security?
-
对不起,忘了提 - Spring Framework 3.1.0 和 Spring Security 3.1.0