`
tanbamboo
  • 浏览: 19128 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Custom Spring Security Filter

    博客分类:
  • Java
阅读更多
最近在项目中用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
0
分享到:
评论
3 楼 tanbamboo 2009-08-05  
<?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 楼 kirk1127 2009-08-04  
能不能把配置文件贴出来看下,我想自定义一个authenticationProcessingFilter,不知道怎么配置的
1 楼 shiren1118 2008-06-03  
用Spring Security 2.0.1做权限控制管理和RBAC。哪个更加好点?

相关推荐

    Spring Security实战源码

    ssecurity-customFilter项目是Spring Security实战(六)的源码; ssecurity-rememberMe项目是Spring Security实战(七)的源码; 本人开发工具是IDEA,每个项目中的代码均可以运行并测试。Eclipse也是一样可以运行...

    spring security 参考手册中文版

    Spring Security 参考 1 第一部分前言 15 1.入门 16 2.介绍 17 2.1什么是Spring Security? 17 2.2历史 19 2.3版本编号 20 2.4获得Spring安全 21 2.4.1使用Maven 21 Maven仓库 21 Spring框架 22 2.4.2 Gradle 23 ...

    Spring Security 中文教程.pdf

    1.1. Spring Security是什么? 1.2. 历史 1.3. 发行版本号 1.4. 获得Spring Security 1.4.1. 项目模块 1.4.1.1. Core - spring-security-core.jar 1.4.1.2. Web - spring-security-web.jar 1.4.1.3. ...

    SpringSecurity 3.0.1.RELEASE.CHM

    1.1. Spring Security是什么? 1.2. 历史 1.3. 发行版本号 1.4. 获得Spring Security 1.4.1. 项目模块 1.4.1.1. Core - spring-security-core.jar 1.4.1.2. Web - spring-security-web.jar 1.4.1.3. Config -...

    Spring Security-3.0.1中文官方文档(翻译版)

    Spring Security-3.0.1 中文官方文档(翻译版) 这次发布的Spring Security-3.0.1 是一个bug fix 版,主要是对3.0 中存在的一些问题进 行修 正。文档中没有添加新功能的介绍,但是将之前拼写错误的一些类名进行...

    spring-boot-reference.pdf

    Spring Boot Documentation 1. About the Documentation 2. Getting Help 3. First Steps 4. Working with Spring Boot 5. Learning about Spring Boot Features 6. Moving to Production 7. Advanced Topics II. ...

    Jetty中文手册

    如何配置自定义错误页面(Custom Error Pages) 配置Jetty Statistics(统计) 配置Jetty Statistics教程 配置Request Log教程 Java-monitor 故障排除 如何使用Jetty Dump Feature(特性) Webtide Blog–Jetty ...

    Python Testing Cookbook.pdf

    Filter out test noise, customize test reports, and tweak doctest’s to meet your needs Write testable stories using lots of tools including doctest, mocks, Lettuce, and Should DSL Get started with the...

    restful restful所需要的jar包

    * Transformer filter to easily apply XSLT stylesheets on XML representations. It is based on JDK's built-in XSLT engine. * Extensible set of core representations based on NIO readable or writable ...

    asp.net知识库

    Tool Tip 示例(FILTER版) Tool Tip示例 (htc版) 一个.net发送HTTP数据实体的类 按键跳转以及按Enter以不同参数提交,及其他感应事件 动态控制Page页的Head信息 SubmitOncePage:解决刷新页面造成的数据重复提交...

Global site tag (gtag.js) - Google Analytics