【发布时间】:2013-04-29 09:39:27
【问题描述】:
我正在尝试显示没有使用 spring security 的用户的无效尝试。我正在使用自定义 User 类来获取除用户名和密码之外的其他用户详细信息。我创建了两个侦听器类,即 AuthenticationSuccessEventListener 和 AuthenticationFailureListener 来更新用户的无效尝试。
现在在 onApplicationEvent 方法中,我正在尝试获取自定义用户对象 (CustomUserDetails),如下所示:
@Component
public class AuthenticationFailureListener implements ApplicationListener<AuthenticationFailureBadCredentialsEvent> {
@Autowired
private ILoginDAO loginDAO ;
@Override
public void onApplicationEvent(AuthenticationFailureBadCredentialsEvent event) {
CustomUserDetails user = (CustomUserDetails)event.getAuthentication().getPrincipal();//I get ClassCastException here.
String strUserID = user.getUserID();
CustomUserDetails customUser = loginDAO.loadUserByUsername(strUserID);
if (customUser != null){
...
} } }
event.getAuthentication().getPrincipal() 返回一个字符串,即我试图将其转换为 CustomUserDetails(自定义用户类)的用户名,我得到错误。
P.S - 我在登录页面中输入用户 ID/密码,因此我将用户 ID 作为参数传递给所有方法,包括 loadUserByUsername(strUserID)。
如何从 AuthenticationFailureBadCredentialsEvent / AuthenticationSuccessEvent 对象中获取我的自定义用户对象?
【问题讨论】:
标签: spring spring-security spring-webflow