【发布时间】:2014-04-13 22:50:14
【问题描述】:
我正在使用凭据和 IP 地址进行自定义身份验证,但是,我需要在 UI 上显示用户名和姓。虽然我没有使用 UserDetails 并且我使用的是 UsernamePasswordAuthenticationToken,但我如何才能访问 firstname 和 lastname,其中 UserDetails bean 保存在 Details 中。
Account account = (Account)accountDao.loadUserByUsername(username);
if(!account.getPassword().equals(password)) {
logger.debug("[USER AUTHENTICATION]Invalid Password:"+password);
return null;
}
logger.warn(String.format("[USER AUTHENTICATION]%s %s",new Object[]{account.getFirstName(),account.getLastName()}));
isAuthenticatedByIP = false;
for(AllowedIPAddress allowedIpAddress:account.getAllowedIPs()){
if(allowedIpAddress.getIpAddress().equals("0.0.0.0")||allowedIpAddress.getIpAddress().equals(userIPAddress)) {
isAuthenticatedByIP = true;
break;
}
}
// Authenticated, the user's IP address matches one in the database
if (isAuthenticatedByIP)
{
logger.debug("[USER AUTHENTICATION]isAuthenticatedByIP is true, IP Addresses match");
UsernamePasswordAuthenticationToken result = null;
result = new UsernamePasswordAuthenticationToken(account.getUsername(), account.getPassword(), account.getAuthorities()) ;
result.setDetails(account);
return result
} else {
logger.warn("[USER AUTHENTICATION]User IP not allowed "+userIPAddress);
}
如何在 jsp 中获取帐户字段以显示用户的欢迎消息。
【问题讨论】:
-
你实现
UserDetailsService来获取org.springframework.security.core.userdetails.UserDetails对象 -
@SantoshJoshi 我更新了上面的代码,其中帐户是 UserDetails 的实例
标签: java spring spring-mvc spring-security