JJC's 테크니컬 다이어리

Spring Security LDAP (AD) 설정 1기본 설정 본문

Spring

Spring Security LDAP (AD) 설정 1기본 설정

털털한JJC 2017. 10. 17. 10:05

Spring Legacy Application에서

Spring Security 를 사용하여 Active Directory 인증을 받는 설정을 기록합니다.

Reference와 버전이 다양하여 나같은 Spring 초보가 검색으로 설정하기에는 생각처럼 쉽지 않았습니다. 많은 시간의 삽질 끝에 해결한 내용을 남깁니다.

Spring Tool Suite 3.9.1.Release 를 사용하여 개발을 진행합니다.


1. 프로젝트 생성

    File -> New -> Spring Legacy Project 를 선택하여

    Templates: 부분에서 Spring MVC Project를 선택한후 Project name을 지정하여 생성한다.

    이때: top-level- package 를 com.jjc.myapp 으로 지정


2. pom.xml 파일에 관련 dependency 추가

   2.1 버전 관련 오류를 없애기 위하여 spring.io.platform 관련 설정 추가함

       <dependencyManagement>

        <dependencies>

            <dependency>

                <groupId>io.spring.platform</groupId>

                <artifactId>platform-bom</artifactId>

                <version>Brussels-SR5</version>

                <type>pom</type>

                <scope>import</scope>

            </dependency>

        </dependencies>

    </dependencyManagement>


  2.2 spring security 관련 설정 추가

<!-- Spring Security -->

<dependency>

<groupId>org.springframework.security</groupId>

<artifactId>spring-security-core</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.security</groupId>

<artifactId>spring-security-web</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.security</groupId>

<artifactId>spring-security-config</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.security</groupId>

<artifactId>spring-security-ldap</artifactId>

</dependency>

<dependency>

  <groupId>org.springframework.security</groupId>

  <artifactId>spring-security-taglibs</artifactId>

</dependency>


3. web.xml 수정하여 spring-security 관련설정을 별도 xml 파일로 지정

    <context-param>

<param-name>contextConfigLocation</param-name>

<param-value>

/WEB-INF/spring/root-context.xml

/WEB-INF/spring/applicationContext-security.xml

</param-value>

</context-param>


4. web.xml 수정하여 filter 설정 추가

<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>


5. applicationContext-security.xml 생성하고 관련 내용을 작성


   5.1 파일안에서 security 를 default namespace가 되도록 xml schema를 설정한다.

<?xml version="1.0" encoding="UTF-8"?>

<beans:beans xmlns:beans="http://www.springframework.org/schema/beans"

xmlns="http://www.springframework.org/schema/security"

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.xsd

http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">

5.2 아래와 같이 작성한다 (파란색 라인 추가)

<?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns:beans="http://www.springframework.org/schema/beans" xmlns="http://www.springframework.org/schema/security" 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.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd"> <authentication-manager erase-credentials="true"> <authentication-provider ref='adAuthenticationProvider' /> </authentication-manager>

<http pattern="/css/**" security="none"/> <http pattern="/user/login*" security="none"/> <http auto-config="true" use-expressions="true"> <intercept-url pattern="/**" access="isFullyAuthenticated()" /> <form-login /> <logout /> </http> <!-- In-Memory... 요건 개발/테스트용 <authentication-manager> <authentication-provider> <user-service> <user name="jjc" password="xxxxx" authorities="ROLE_USER, ROLE_ADMIN" /> <user name="bob" password="bobspassword" authorities="ROLE_USER" /> </user-service> </authentication-provider> </authentication-manager> --> </beans:beans>



6. root-context.xml 에서 Active Directory 관련 security bean 관련 내용을 작성

<bean id="adAuthenticationProvider"

class="org.spriㅌngframework.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>


7. 실행/로그인 테스트해본다.