봉 블로그

Spring Security 3 소개 및 변경사항 본문

개발환경/Spring

Spring Security 3 소개 및 변경사항

idkbj 2010. 4. 15. 09:14

소개
웹어플의 대부분은 기본적인 공통기능들이 있다.
  • 계정관리(사용자관리)
  • 메뉴관리
  • 권한관리
  • 코드관리
이중에서 권한관리 기능은 '계정관리' 와 '메뉴관리'와 연관되서 사용되는데, 프로젝트마다 계정관리 와 메뉴관리, 권한관리의 방법이 달라서 한프로젝트에서 사용된 권한관리 컴포넌트를 다른 프로젝트에서 재사용하기가 꽤 어렵다.

이러한 어려움을 해결해주는것이 Spring Security 이다.

특징으로는
  • Filter 기반으로 작동되기 때문에 기존 어플의 어떠한 수정이 필요없다.
  • 따라서 권한과 관련된 기능을 쉽게 on off할수 있다.
  • 별다른 계정관리나 메뉴관리가 필수는 아니다. 자체적으로 계정관리와 URL별 접근권한을 관리할수있는 쉬운 방법을 제공한다.
  • 따라서 소규모의 프로젝트부터 대규모의 프로젝트까지 얼마든지 쉽게 적용가능하다.
  • 다양한 계정관리와 메뉴관리에 따른 기능 확장 방법을 제공한다.
  • 기타 파워플한 엔터프라이즈급 기능을 다수 제공한다. [Spring Security - Main Features 참고]

변경사항 간략
2.0.5버전과 비교해서 3.* 최신버전의 변경사항을 간략히 요약해봤다.

  • 가장 큰변화는 전체적으로 클래스 패키지와 클래스이름이 리팩토링 됬으며
  • 설정방식도 변경되었다.

기존 클래스명이 어떻게 변경됬는지는 Spring Security 배포파일에 해당 내용을 안내하고 있다.
해당 파일을 첨부한다.



변경된 설정방법을 설명하면

<authentication-manager> 이 필수설정으로 변경되었으며
Custom AuthenticationProvider를 위한 <custom-authentication-provider> 태그가 삭제되고
<authentication-manager>의 하위태그로 <authentication-provider >를 등록해야 한다.

설정예시
<security:authentication-manager alias="authenticationManager">
       <security:authentication-provider ref="sample.CustomAuthenticationProvider" />
</security:authentication-manager>


 Custom Filter 를 등록하는 방법이 아래와 같은 방법으로 변경되었다.
반드시 <http> 태그안에 <custom-filter> 태그를 이용해서 등록시켜야 한다.

2.0.5 방식
<bean id="authenticationFilter" class="sample.CustomAuthenticationProcessingFilter">       
     <property name="authenticationManager" ref="authenticationManager" />
      ....       
     <security:custom-filter position="AUTHENTICATION_PROCESSING_FILTER" />    
 </bean>


3.* 방식
<security:http auto-config="false" >
         <!-- Custom Filter 등록 -->
        <security:custom-filter position="FORM_LOGIN_FILTER" ref="authenticationFilter" />
 </security:http>

<bean id="authenticationFilter" class="sample.CustomAuthenticationProcessingFilter">       
     <property name="authenticationManager" ref="authenticationManager" />
      ....      
 </bean>

<custom-filter>태그의 position 에 명시할 Filter alias 명칭도 변경되었는데, 아래와 같다.

Table 2.1. Standard Filter Aliases and Ordering

Alias Filter Class Namespace Element or Attribute
CHANNEL_FILTER ChannelProcessingFilter http/intercept-url@requires-channel
CONCURRENT_SESSION_FILTER ConcurrentSessionFilter session-management/concurrency-control
SECURITY_CONTEXT_FILTER SecurityContextPersistenceFilter http
LOGOUT_FILTER LogoutFilter http/logout
X509_FILTER X509AuthenticationFilter http/x509
PRE_AUTH_FILTER AstractPreAuthenticatedProcessingFilter Subclasses N/A
CAS_FILTER CasAuthenticationFilter N/A
FORM_LOGIN_FILTER UsernamePasswordAuthenticationFilter http/form-login
BASIC_AUTH_FILTER BasicAuthenticationFilter http/http-basic
SERVLET_API_SUPPORT_FILTER SecurityContextHolderAwareFilter http/@servlet-api-provision
REMEMBER_ME_FILTER RememberMeAuthenticationFilter http/remember-me
ANONYMOUS_FILTER AnonymousAuthenticationFilter http/anonymous
SESSION_MANAGEMENT_FILTER SessionManagementFilter session-management
EXCEPTION_TRANSLATION_FILTER ExceptionTranslationFilter http
FILTER_SECURITY_INTERCEPTOR FilterSecurityInterceptor http
SWITCH_USER_FILTER SwitchUserFilter N/A

<http> 태그내에서 <intercept-url> 사용시 access 속성에 Spring EL을 사용할수 있게 되었다.
<http>태그의 use-expressions="true" 속성을 주면 <intercept-url pattern="/secure/**" access="isAuthenticated()" /> 와같은 설정이 가능하다.
설정예시
<http use-expressions="true">
    <intercept-url pattern="/secure/**" access="hasRole('ROLE_SUPERVISOR') and hasIpAddress('192.168.1.0/24')" />
     ...
</http>
이방법을 잘쓰면 매우 유용할것 같다.
근데 어떻게 확장하지? 표현식을 추가로 만들어서 사용해야 할것 같은데,
소스를 보니 WebExpressionVoter 에 WebSecurityExpressionHandler 인터페이스를 제공하는데... 음. 더 알아봐야 겠다.

이외에도 많은 부분이 변경된거 같은데.. 지금까지 설명한 내용으로도 왠만한 설정은 커버할듯하다.
스프링 너무 좋은거 같다. 이런놈을 선사하다니..ㅋㅋ