我正在开发基于Spring MVC的Web应用程序,该应用程序使用Spring Security允许用户使用其Active Directory凭据登录.我想在用户登录后获取用户的电子邮件地址.我已经登录了工作,现在我正在试图找出如何获取用户的电子邮件.看起来我应该从一个InetOrgPerson
对象那里得到它.我已经尝试InetOrgPersonContextMapper
在xml配置中指定一个如下:
我试图在控制器中收到这样的电子邮件:
@RequestMapping(value = "/startProcess.html", method = RequestMethod.POST) public ModelAndView startProcess(@ModelAttribute Token token, Principal principal) { ModelAndView mav = new ModelAndView(); String user = principal.getName(); String email = ((InetOrgPerson)principal).getMail(); logger.info("Got email \"" + email + "\" for user \"" + user + "\""); ... }
这给了我一个类强制转换异常,说它不能转换UsernamePasswordAuthenticationToken
为a InetOrgPerson
.我决定尝试Principal
手动获取对象,而不是让Spring注入它:
@RequestMapping(value = "/startProcess.html", method = RequestMethod.POST) public ModelAndView startProcess(@ModelAttribute Token token, Principal principal) { ModelAndView mav = new ModelAndView(); String user = principal.getName(); Object p = SecurityContextHolder.getContext().getAuthentication().getPrincipal(); String email = ((InetOrgPerson)p).getMail(); logger.info("Got email \"" + email + "\" for user \"" + user + "\""); ... }
这告诉我它无法投射LdapUserDetailsImpl
到InetOrgPerson
.不LdapUserDetailsImpl
应该是InetOrgPerson
因为我正在使用InetOrgPersonContextMapper
?