【问题标题】:How to get user details from spring security 3.1 and active directory by LDAP如何通过 LDAP 从 spring security 3.1 和活动目录中获取用户详细信息
【发布时间】:2013-11-06 21:13:12
【问题描述】:

我是 Java 和 Java EE 的新手。你能告诉我成功登录后如何从 Active Directore 中检索全名、公司、电话、部门、邮件等用户详细信息吗?所以:

我的 web.xml:

    <?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml
                 /WEB-INF/applicationContext-security.xml
    </param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>
<welcome-file-list>
    <welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>  
</web-app>

我的 applicationContextsecurity.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:security="http://www.springframework.org/schema/security"
 xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd">

<!-- LDAP server details --> 
<security:authentication-manager>
    <security:authentication-provider ref="ldapActiveDirectoryAuthProvider" />
</security:authentication-manager>

<beans:bean id="grantedAuthoritiesMapper" class="org.mops.security.ActiveDirectoryGrantedAuthoritiesMapper"/>

<beans:bean id="ldapActiveDirectoryAuthProvider" class="org.springframework.security.ldap.authentication.ad.ActiveDirectoryLdapAuthenticationProvider">
    <beans:constructor-arg value="xxx.local" />
    <beans:constructor-arg value="ldap://xxx.local:389/" /> 
    <beans:property name="authoritiesMapper" ref="grantedAuthoritiesMapper" />
    <beans:property name="useAuthenticationRequestCredentials" value="true" />
    <beans:property name="convertSubErrorCodesToExceptions" value="true" />
</beans:bean>

<security:http auto-config="true" pattern="/**">
    <!-- Login pages -->
    <security:form-login login-page="/" default-target-url="/user/" 
        login-processing-url="/j_spring_security_check" authentication-failure-url="/?error=true" />
    <security:logout logout-success-url="/"/>

    <!-- Security zones -->
    <!--<security:intercept-url pattern="/it/**" access="ROLE_ADMIN" />
    <security:intercept-url pattern="/user/**" access="ROLE_ADMINISTRATION" /> -->
</security:http>

我可以正常登录,我可以在控制器中获取用户名:

用户控制器.java:

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
public class UserController{
private String username;
@RequestMapping("/user")
public String User(Model model) {

    Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();

    if (principal instanceof UserDetails) {
      this.username = ((UserDetails)principal).getUsername();
    } else {
      this.username = principal.toString();
    }
    model.addAttribute("message", username);

    return "user";
 }


}

现在我考虑创建类,它存储所有用户详细信息并在我需要使用用户详细信息时创建此类的实例。

谁能一步一步告诉我如何做到这一点?

【问题讨论】:

  • 如果您只需要 Principal,您可以简单地将 Principal principal 作为处理程序方法的参数之一,Spring 会为您填充它。请参阅此处的“支持的处理程序方法参数和返回类型”部分标题:docs.spring.io/spring/docs/3.0.x/reference/mvc.html
  • 好的,但是我怎样才能得到部门等等?
  • 您需要将 Principal 转换为您期望的任何对象。否则,您将看不到对象的属性。

标签: jakarta-ee spring-mvc spring-security active-directory spring-ldap


【解决方案1】:

两周后,我找到了解决办法。

在pring-security.xml中:

<beans:bean id="ldapActiveDirectoryAuthProvider" class="org.springframework.security.ldap.authentication.ad.ActiveDirectoryLdapAuthenticationProvider" >
    <beans:constructor-arg value="xxx.yyy" />
    <beans:constructor-arg value="ldap://zzz.xxx.yyy:389/" /> 
    <beans:property name="authoritiesMapper" ref="grantedAuthoritiesMapper" />
    <beans:property name="useAuthenticationRequestCredentials" value="true" />
    <beans:property name="convertSubErrorCodesToExceptions" value="true" />
    <beans:property name="userDetailsContextMapper">
    <beans:bean class="org.springframework.security.ldap.userdetails.InetOrgPersonContextMapper" />
</beans:property>
</beans:bean>

在控制器中,我们将 Principal 转换为 InetOrgPerson:

import javax.naming.NamingException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.security.ldap.userdetails.InetOrgPerson;


@Controller
public class UserController  {
private String username;


@RequestMapping(value="/user", method = RequestMethod.GET)
public String User(Model model) throws NamingException {
Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();


    if (principal instanceof UserDetails) {
      this.username = ((UserDetails)principal).getUsername();

    } else {
      this.username = principal.toString();

    }
        model.addAttribute("username", username);        
        model.addAttribute("roomNumber", ((InetOrgPerson) principal).getRoomNumber());
    return "user";
}

}

【讨论】:

  • org.springframework.security.ldap.userdetails.LdapUserDetailsImpl 不能转换为 org.springframework.security.ldap.userdetails.InetOrgPerson
猜你喜欢
  • 2019-12-03
  • 2013-07-23
  • 1970-01-01
  • 1970-01-01
  • 2015-03-09
  • 2014-11-08
  • 1970-01-01
  • 1970-01-01
  • 2020-08-28
相关资源
最近更新 更多