ITeye博客

Custom Spring Security Filter

tanbamboo · 2008-06-02 16:32 · Java · 企业架构 · Spring · 项目管理 · Acegi · 配置管理 · Security · Markdown

最近在项目中用Spring Security 2.0.1做权限控制管理,由于以前用过Acegi Security,基本上很快搞定。但是后来在移植自己扩展的AuthenticationProcessingFilter的时候,发现了很多问题。 经过一些网络搜索和测试,问题解决,做个记录:

  1. 如果有自定义的Filter用于替换系统默认filter的时候,需要把http的auto-config选项关闭掉,否则会导致与默认filter冲突,Order相同。
  2. http配置里面的anonymous、login、logout等,实际上是配置了过滤器链中的默认的AnonymousFilter、authenticationProcessingFilter、logoutFilter等。本例中由于仅扩展authenticationProcessingFilter,所以只需要去掉login配置。
  3. login配置还包括了一个authenticationProcessingFilterEntryPoint的概念,所以当替换掉默认的authenticationProcessingFilter之后,需要在http中,通过属性entry-point-ref指向一个authenticationProcessingFilterEntryPoint。

由此可见,其实spring security在架构上还是和acegi一脉相承的,只是spring security在配置方面做了更多的magic处理,优点是:入门容易,配置内容更少;缺点是:增加了magic成分,也增加了查找错误、扩展的难度。

评论(3)

  1. #3 楼 · tanbamboo · 2009-08-05 14:52
    <?xml version="1.0" encoding="UTF-8"?>
    
    <b:beans xmlns="http://www.springframework.org/schema/security"
        xmlns:b="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-2.5.xsd
                            http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.1.xsd">
    
        <http realm="Contacts Realm" entry-point-ref="authenticationProcessingFilterEntryPoint">
            <intercept-url pattern="/admin/**/*" access="ROLE_ADMIN" />
           
           	<intercept-url pattern="/**" access="IS_AUTHENTICATED_ANONYMOUSLY"/>
    
            <port-mappings>
                <port-mapping http="8080" https="8443" />
            </port-mappings>
            
            <anonymous/>
            <!--<form-login login-page="/admin/login" authentication-failure-url="/admin/login?error=1"/>-->
            <!--<logout logout-success-url="/admin/login"/>-->
        </http>
    
        <authentication-manager alias="authenticationManager"/> 
        
        <b:bean id="authenticationProcessingFilterEntryPoint" class="org.springframework.security.ui.webapp.AuthenticationProcessingFilterEntryPoint">
    	    <b:property name="loginFormUrl" value="/admin/login" />
        </b:bean>
    
        <b:bean id="authenticationProcessingFilter" class="com.sample.yours.AuthenticationProcessingFilter">
            <custom-filter position="AUTHENTICATION_PROCESSING_FILTER" />
            
            <b:property name="defaultTargetUrl" value="/" />
            <b:property name="authenticationManager" ref="authenticationManager" />
            <b:property name="filterProcessesUrl" value="/j_spring_security_check" />
            <b:property name="authenticationFailureUrl" value="/admin/login?error=1" />
        </b:bean>
        
        <b:bean id="logoutFilter" class="org.springframework.security.ui.logout.LogoutFilter">
             <custom-filter position="LOGOUT_FILTER" />
        	 <b:constructor-arg value="/admin/login" />
        	 <b:constructor-arg >
    	    	 <b:list>
    	    	 	<b:bean class="com.sample.yours.LogoutHandler" ></b:bean>
    	    	 	<b:bean class="org.springframework.security.ui.logout.SecurityContextLogoutHandler" ></b:bean>
    	    	 </b:list>
        	 </b:constructor-arg>
        </b:bean>
        
        <b:bean id="passwordEncoder" class="org.springframework.security.providers.encoding.Md5PasswordEncoder">
        </b:bean>
    
        <authentication-provider>
           <!-- <password-encoder hash="md5"/> -->
           <password-encoder  ref="passwordEncoder" />
            
            <jdbc-user-service data-source-ref="dataSource"  
            users-by-username-query="SELECT u.username, u.password, u.enable FROM user u WHERE u.username=?"
            authorities-by-username-query="SELECT username, action FROM user_action ua WHERE ua.username=?"
            />
       </authentication-provider>
    
       <!-- Automatically receives AuthenticationEvent messages -->
       <b:bean id="loggerListener" class="org.springframework.security.event.authentication.LoggerListener"/>   
    </b:beans>
    

    这个配置文件里面还替换了默认的Logout处理,增加了一个自定义的logoutHandler。

  2. #2 楼 · kirk1127 · 2009-08-04 15:01

    能不能把配置文件贴出来看下,我想自定义一个authenticationProcessingFilter,不知道怎么配置的

  3. #1 楼 · shiren1118 · 2008-06-03 20:11

    用Spring Security 2.0.1做权限控制管理和RBAC。哪个更加好点?

评论功能维护中,当前仅展示历史评论。