Spring security Spring Security-无效属性';校长';bean类[org.springframework.security.authentication.UsernamePasswordAuthenticationToken]的

Spring security Spring Security-无效属性';校长';bean类[org.springframework.security.authentication.UsernamePasswordAuthenticationToken]的,spring-security,principal,Spring Security,Principal,我必须为一个项目实现一个自定义的“身份验证提供程序”,但在尝试在JSP中访问身份验证的对象属性时遇到了麻烦。细节: 我的自定义身份验证提供程序已成功创建身份验证对象 Authentication auth = new UsernamePasswordAuthenticationToken(username, password, getAuthorities(userRoles)); log.info("User is authenticated"); return auth; (此处仅限相关代

我必须为一个项目实现一个自定义的“身份验证提供程序”,但在尝试在JSP中访问身份验证的对象属性时遇到了麻烦。细节: 我的自定义身份验证提供程序已成功创建身份验证对象

Authentication auth = new UsernamePasswordAuthenticationToken(username, password, getAuthorities(userRoles));
log.info("User is authenticated");
return auth;
(此处仅限相关代码)

然后,在控制器方法中,我只显示一条带有用户名的日志消息(这证明身份验证对象已创建并放置在安全上下文中):

然后在JSP页面中,我想使用

<sec:authentication property="principal"/>
我还注意到

<sec:authorize ifAnyGranted="role">...
。。。
虽然用户已在身份验证对象中添加了必要的角色,但无法工作

我做错什么了吗?身份验证工作正常,我只是无法访问身份验证对象的属性


非常感谢您,祝您度过愉快的一天。

鉴于我看不出您的案例有任何问题,我认为可能是bug,它已在Spring 3.1.1中修复。您可以升级吗?

您的AuthenticationProvider必须返回UserDetails对象

来自spring文档
此标记允许访问存储在安全上下文中的当前身份验证对象。它直接在JSP中呈现对象的属性。因此,例如,如果身份验证的主要属性是Spring Security的UserDetails对象的实例,那么使用将呈现当前用户的名称。

这是一个老问题,但我认为我可以帮助其他人

作为UsernamePasswordAuthenticationToken的第一个参数,您可以发送一个用户

不要传递用户名,而是传递用户本身。但用户必须是扩展org.springframework.security.core.userdetails.userdetails的类:

Authentication auth = new UsernamePasswordAuthenticationToken(user, password, getAuthorities(userRoles));
log.info("User is authenticated");
return auth;
请参阅您使用的方法:

public UsernamePasswordAuthenticationToken(Object principal, Object credentials,
            Collection<? extends GrantedAuthority> authorities) {
        super(authorities);
        this.principal = principal;
        this.credentials = credentials;
        super.setAuthenticated(true); // must use super, as we override
    }
public UsernamePasswordAuthenticationToken(对象主体、对象凭据、,

CollectionSpring Framework和Spring Security的版本是什么?很抱歉,忘了提这一点-Spring Framework 3.1.0和Spring Security 3.1.0是的!将Spring Security和Spring Framework升级到3.1.2非常有效。非常感谢!但是,问题仍然存在。用户拥有必要的角色,但其中存在片段标记没有显示…解决了这个问题,结果是键盘和椅子之间出现了错误-我在角色名称的末尾有一个“”。Deepak,UsernamePasswordAuthenticationToken实现了身份验证,所以一切都是正确的。唯一的区别是您必须使用而不是
Authentication auth = new UsernamePasswordAuthenticationToken(user, password, getAuthorities(userRoles));
log.info("User is authenticated");
return auth;
public UsernamePasswordAuthenticationToken(Object principal, Object credentials,
            Collection<? extends GrantedAuthority> authorities) {
        super(authorities);
        this.principal = principal;
        this.credentials = credentials;
        super.setAuthenticated(true); // must use super, as we override
    }
<span class="hidden-sm-down" sec:authentication="principal.email">Email</span>