Java 使用ldap和spring安全性,如何从登录的用户处获取电子邮件

Java 使用ldap和spring安全性,如何从登录的用户处获取电子邮件,java,spring,spring-security,ldap,Java,Spring,Spring Security,Ldap,嘿,我想你可以成功地连接到ldap服务器,也可以用用户名和密码登录,但是在那之后我想得到登录用户的电子邮件,我怎么做 @Controller public class DemoController{ public MutationServices ms = new MutationServices(); @RequestMapping("login") public String logout (ModelMap model){ return "/login"; } @Requ

嘿,我想你可以成功地连接到ldap服务器,也可以用用户名和密码登录,但是在那之后我想得到登录用户的电子邮件,我怎么做

@Controller
public class DemoController{    
public MutationServices ms = new MutationServices();
@RequestMapping("login")
public String logout (ModelMap model){
    return "/login";
}

@RequestMapping("uploadFile")
public String uploadPage(ModelMap model){
    return "/upload";
}

@RequestMapping("utilisateurs")
public String getSimpleUserPage(ModelMap model){

Authenticationauth=SecurityContextHolder.getContext().getAuthentication();
    String username = auth.getName();

    @SuppressWarnings("unchecked")
    Collection<SimpleGrantedAuthority> authorities 
       (Collection<SimpleGrantedAuthority>) auth.getAuthorities();
    System.out.println("auth : " + authorities);
    model.addAttribute("username", username);
    return "/user";
}}
@控制器
公共类DemoController{
public MutationServices ms=新的MutationServices();
@请求映射(“登录”)
公共字符串注销(ModelMap模型){
返回“/登录”;
}
@请求映射(“上传文件”)
公共字符串上载页(ModelMap模型){
返回“/上传”;
}
@请求映射(“利用者”)
公共字符串getSimpleUserPage(模型映射模型){
Authenticationauth=SecurityContextHolder.getContext().getAuthentication();
字符串username=auth.getName();
@抑制警告(“未选中”)
征收机关
(集合)auth.getAuthories();
System.out.println(“auth:+authorities”);
model.addAttribute(“用户名”,用户名);
返回“/用户”;
}}
这是我的春季保安

<http>
    <form-login login-page="/login"
        authentication-failure-url="/login"
    />

    <intercept-url pattern="/utilisateurs**" access="ROLE_USERS" />
    <logout logout-success-url="/index.jsp" />

    <http-basic />
</http>
<authentication-manager>
    <ldap-authentication-provider
        user-search-filter="(cn={0})" 
        user-search-base="dc=example,dc=com"
        group-search-filter="(member={0})"
        group-search-base="dc=example,dc=com"
        group-role-attribute="cn" 
        role-prefix="ROLE_">

    </ldap-authentication-provider>
</authentication-manager>

<ldap-server url="ldap://0.0.0.0:10389"
     manager-dn="uid=admin,dc=example,dc=com"  manager-password="admin"/>


假设您使用
inetOrgPerson
LDAP模式

为此LDAP架构定义bean,这将在登录时检索电子邮件:

@Bean
公共InetOrgPersonContextMapper用户上下文映射器(){
返回新的InetOrgPersonContextMapper();
}
将bean引用添加到LDAP配置:


然后,您可以如下方式检索电子邮件:

Authentication auth=SecurityContextHolder.getContext().getAuthentication();
InetOrgPerson=(InetOrgPerson)auth.getPrincipal();
字符串email=person.getMail();

为什么要更改上一个编辑器的操作?你的代码看起来很混乱。因为我不得不,我也需要上传一个文件,我可以获得userdetails,但是我不能在ldap中获得email属性have@Controller公共类DemoController{不是代码块的一部分,也不是最后一个}。还有一些改进的空间,但是它看起来更好:)我建议重新编写你的第一段
嘿,我想你可以成功连接到ldap服务器,也可以使用用户名和密码登录,但在那之后我想获得登录用户的电子邮件,我该怎么做呢?
来解释一下:解释你的代码应该做什么,什么在起作用,什么不起作用。因为你现在的第一段只有一句话,我不知道你到底有什么问题:)不过编辑确实让它看起来更漂亮了!