自定义authenticationFilter Spring Security 3.2

时间:2013-05-27 13:04:34

标签: java spring spring-security

对于一个项目,我尝试使用Spring Security 3.2作为基本安全性。因为这个项目已经启动并运行,所以我已经拥有了另一个(自己的)安全层。因此,我制作了一个自定义身份验证提供程序来融化安全层。工作正常,直到我还需要进行自定义匿名身份验证(Spring Security Documentation, chapter 13)。

所以我做了一个自定义过滤器并删除了原始过滤器:

<http request-matcher="regex" use-expressions="true">
    <anonymous enabled="false" />
    <custom-filter ref="anonymousAuthFilter" position="ANONYMOUS_FILTER"/>
    ...
</http>

豆子:

<beans:bean id="anonymousAuthFilter" class="own.package.auth.SecurityAnonymousAuthenticationFilter">
    <beans:property name="key" value="anonymousKey "/>
    <beans:property name="userAttribute" value="anonymous,ROLE_ANONYMOUS"/>
</beans:bean>

和te Java Class:

public class SecurityAnonymousAuthenticationFilter extends GenericFilterBean implements InitializingBean {
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        logger.info("Entering doFilter method");
        //implementation code here
    }

    //other methods
}

问题是请求服务器时不会调用doFilter方法。但是调用了init方法afterPropertiesSet()...是否有人理解为什么我的customFilter没有被触发?

P.S。我确实在web.xml文件中命名了delegatingFilterProxy,因此不是问题。

1 个答案:

答案 0 :(得分:1)

由于ANONYMOUS_FILTER是与名称空间相关的过滤器。您必须避免引用特定过滤器文件的任何名称空间标记:

   <http auto-config='false' request-matcher="regex" use-expressions="true">
    <custom-filter ref="anonymousAuthFilter" position="ANONYMOUS_FILTER"/>
    ...
   </http>

有关进一步参考,请参阅第2.3.5节中的Spring安全性文档:http://static.springsource.org/spring-security/site/docs/3.0.x/reference/ns-config.html

修改:并确保保留<anonymous-enabled=false/>标记。

编辑2:更正了我的回答。这种配置应该有效。如果没有,那么我们需要开始查看更大的图片,并且必须从完整配置开始发布更多应用程序。