获取使用Spring Securitu通过Active Directory登录的用户的电子邮件地址

 年轮033 发布于 2023-02-13 18:48

我正在开发基于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 + "\"");

    ...
}

这告诉我它无法投射LdapUserDetailsImplInetOrgPerson.不LdapUserDetailsImpl应该是InetOrgPerson因为我正在使用InetOrgPersonContextMapper

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有