Spring security db로 권한지정하기

From JCFWiKi

Jump to: navigation, search

목차

[편집] applicationContext-security.xml 설정

[편집] with db filterInvocationDefinitionSource

  • db에 url 권한 지정할때는 다음과 같은 bean을 설정해줘야한다.
 <bean id="filterInvocationInterceptor"
 class="org.acegisecurity.intercept.web.FilterSecurityInterceptor">
 <property name="authenticationManager" ref="authenticationManager" />
 <property name="accessDecisionManager" ref="accessDecisionManager" />
 <!--with db//filterInvocationDefinitionSource -->
 <property name="objectDefinitionSource"><ref bean="filterInvocationDefinitionSource"/></property>
 <bean id="secureUrlDao" class="com.daewoobrenic.acegisecurity.intercept.web.SecureUrlJdbcDaoImpl">
    <property name="dataSource"><ref bean="dataSource"/></property>
    <property name="secureUrlQuery">
        <value>SELECT url, authority FROM url_authority</value>        
    </property>
</bean>
 
<bean id="filterInvocationDefinitionSource" 
      class="com.daewoobrenic.acegisecurity.intercept.web.JdbcFilterInvocationDefinitionSource" 
      init-method="initialize">
    <property name="secureUrlJDao"><ref bean="secureUrlDao"/></property>    
</bean>

[편집] url_authority table 만들기

CREATE TABLE "SYSTEM"."URL_AUTHORITY"(
URL VARCHAR2(100) NOT NULL,
AUTHORITY VARCHAR2(50) NOT NULL)

[편집] url 권한 지정할 때 유의사항

  • 먼저 acegi에서 제공하는 sample을 보자
   <bean id="filterInvocationInterceptor" class="org.acegisecurity.intercept.web.FilterSecurityInterceptor">
      <property name="authenticationManager"><ref bean="authenticationManager"/></property>
      <property name="accessDecisionManager"><ref local="httpRequestAccessDecisionManager"/></property>
      <property name="objectDefinitionSource">
         <value>
                CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
                PATTERN_TYPE_APACHE_ANT
                /index.jsp=ROLE_ANONYMOUS,ROLE_USER
                /hello.htm=ROLE_ANONYMOUS,ROLE_USER
                /logoff.jsp=ROLE_ANONYMOUS,ROLE_USER
                /switchuser.jsp=ROLE_SUPERVISOR
                /j_acegi_switch_user=ROLE_SUPERVISOR
                /acegilogin.jsp*=ROLE_ANONYMOUS,ROLE_USER
                /**=ROLE_USER
         </value>
      </property>
   </bean>
  • 유의사항
    • 위의 value를 보면 권한을 지정해야하는 특정 page 중 가장 먼저 로딩 되는 페이지 부터 권한이 지정되어 있다.
    • 가장 마지막에는 특정페이지를 제외한 나머지 page (/**=ROLE_USER)에 대한 권한 지정이 되어 있다.
    • (중요) 파일명뒤에는 별표가 하나(ex. acegilogin.jsp*) ,폴더의 모든 하위 디렉토리에 권한을 지정하고 싶을때는 별표 두개(/**)를 지정해줘야한다.
    • 이는 acegi에서 권한을 읽을 때 순서를 타기 때문이다.
    • 그러므로 가장 앞에 url 권한은 role_anonymous가 볼 수 있는 가장 노출이 심한 page를 지정해야 한다.
      • 예를 들어, login.jsp, 회원가입.jsp, popup.jsp 등등....
      • 만약 이런 페이지들의 role이 지정되지 않는 상태에서, 이 페이지들이 포함된 폴더 전체에 대해 role_anonymous가 아닌 권한이 지정됐다면, 로그인 이전 상태에서 접근이 허용되지 않을 수 있다.
      • 그러므로 특정 페이지에 권한을 걸때는 role_anonymous에게 허락된 페이지라도 url 권한을 명시해줘야한다.
    • db에 url 권한 지정해 줄때
      • secureUrlDao bean에 있는 secureUrlQuery 에 orderby 쿼리를 추가하여 role_anonymous를 가장 위에 조회되도록한다.

[편집] role 이름 중 'Role_'빼기

  • RoleVoter를 사용한다.
<property name="accessDecisionManager">
<bean  class="org.acegisecurity.vote.AffirmativeBased">
<property  name="allowIfAllAbstainDecisions"
value="false" />
<property  name="decisionVoters">
<list>
<bean  class="org.acegisecurity.vote.RoleVoter" >
<property  name="rolePrefix">
<value></value>
</property>
</bean>
<bean
class="org.acegisecurity.vote.AuthenticatedVoter"  />
</list>
</property>
</bean>
</property>