JJC's 테크니컬 다이어리

Spring Security LDAP (AD) 설정2 사용자 추가정보 얻기 본문

Spring

Spring Security LDAP (AD) 설정2 사용자 추가정보 얻기

털털한JJC 2017. 10. 17. 16:31

Spring Security LDAP (AD) 설정2 추가 사용자정보 얻기

여기서는 기본 LDAP 설정으로는 전체이름, 이메일주소 같은 Active Directory의 정보를 조회하지 못합니다.

그래서 추가 작업을 해주어야 하는데 그 부분의 내용을 기록 합니다.


1.  root-context.xml 수정하여 별도로 만드는 userDetailsContextMapper bean을 등록한다.

<bean id="adAuthenticationProvider"

class="org.springframework.security.ldap.authentication.ad.ActiveDirectoryLdapAuthenticationProvider">

<constructor-arg value="mycompany.com" />   <!-- AD Domain -->

<constructor-arg value="ldap://10.10.10.5/" />  <!-- AD Server-->

        <!-- 이름,이메일,전화 같은 추가 사용자정보 까지 가져오기 위한 property -->

<property name="userDetailsContextMapper">   

<bean class="com.jjc.sec.CustomUserDetailsContextMapper" />

</property>

</bean>

     <context:component-scan base-package="com.jjc.sec">

     </context:component-scan>

2. com.jjc.sec 패키지를 생성하고 필요 클래스를 추가

   2.1 CustomUserDetails

package com.jjc.sec;

import java.util.Collection;

import org.springframework.security.core.GrantedAuthority;

import org.springframework.security.core.userdetails.User;

public class CustomUserDetails extends User {

    private static final long serialVersionUID = 1416132138315457588L;

     // extra instance variables

       final String fullname;

       final String email;

       final String title;

       public CustomUserDetails(String username, String password, boolean enabled, boolean accountNonExpired,

             boolean credentialsNonExpired, boolean accountNonLocked,

             Collection<? extends GrantedAuthority> authorities, String fullname,

             String email, String title) {

           super(username, password, enabled, accountNonExpired, credentialsNonExpired,

                accountNonLocked, authorities);

           this.fullname = fullname;

           this.email = email;

           this.title = title;

       }

       public String getFullname() {

           return this.fullname;

       }

       public String getEmail() {

           return this.email;

       }

       public String getTitle() {

           return this.title;

       }

}


   2.2 CustomUserDetailsContextMapper

package com.jjc.sec;

import java.util.Collection;

import javax.naming.NamingException;

import javax.naming.directory.Attributes;

import org.springframework.ldap.core.DirContextAdapter;

import org.springframework.ldap.core.DirContextOperations;

import org.springframework.security.core.GrantedAuthority;

import org.springframework.security.core.userdetails.UserDetails;

import org.springframework.security.ldap.userdetails.UserDetailsContextMapper;

public class CustomUserDetailsContextMapper implements UserDetailsContextMapper {

    public UserDetails mapUserFromContext(DirContextOperations ctx,

            String username, Collection<? extends GrantedAuthority> authorities) {

        String fullname = "";

        String email = "";

        String title = "";

        Attributes attributes = ctx.getAttributes();

        try {

            fullname = (String) attributes.get("displayName").get(); 

            email = (String) attributes.get("mail").get(); 

           // title = (String) attributes.get("title").get(); 

        } catch (NamingException e) {

            e.printStackTrace();

        }

        CustomUserDetails details = new CustomUserDetails(username, "", true, true, true, true, authorities, fullname, email, title);

        return details;

    }

    public void mapUserToContext(UserDetails user, DirContextAdapter ctx) {

    }

}


3. HomeController의 home 메소드에 다음을 추가하여 사용자정보가 조회되는지 확인

SecurityContext secctx = SecurityContextHolder.getContext();

Authentication authentication = secctx.getAuthentication();

CustomUserDetails userDetails = (CustomUserDetails) authentication.getPrincipal();

logger.info(userDetails.toString());

logger.info(userDetails.getFullname());

logger.info(userDetails.getEmail());


4. home.jsp에 다음 security 관련 tag를 추가하여 조회된 사용자 정보가 표시되는지 확인


<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>


<%@ page session="true" %>


<html>

<head>

<title>Home</title>

</head>

<body>

<h1>

Hello world!  

</h1>


<!-- 로그인 한 사용자만 보이기 -->

<sec:authorize access="isAuthenticated()">

이 내용은 인증받은 사용자에게만 보여집니다.

</sec:authorize>


<!-- 그룹에 포함된 경우에만 보이기 - 해당 그룹을 지정 -->

<sec:authorize access="hasAuthority('CAD지원팀')">

<h2>당신은 CAD지원팀</h2>

</sec:authorize>


<!--  ID 보이기  -->

<sec:authentication property="principal.username" />


<!--  사용자이름 보이기  -->

<sec:authentication property="principal.fullname" />


<!--  email 즈소 보이기  -->

<sec:authentication property="principal.email" />

</body>

</html>


5. 실행하여 테스트해 봅니다.