自定义authenticationFilter Spring Security 3.2

Jac*_*gen 6 java spring spring-security

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

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

<http request-matcher="regex" use-expressions="true">
    <anonymous enabled="false" />
    <custom-filter ref="anonymousAuthFilter" position="ANONYMOUS_FILTER"/>
    ...
</http>
Run Code Online (Sandbox Code Playgroud)

豆子:

<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>
Run Code Online (Sandbox Code Playgroud)

和te Java类:

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
}
Run Code Online (Sandbox Code Playgroud)

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

PS我确实在web.xml文件中命名了delegatingFilterProxy,所以这不是问题.

Car*_*ten 1

由于它ANONYMOUS_FILTER是与名称空间相关的过滤器。您必须避免引用特定过滤器位置的任何命名空间标签:

   <http auto-config='false' request-matcher="regex" use-expressions="true">
    <custom-filter ref="anonymousAuthFilter" position="ANONYMOUS_FILTER"/>
    ...
   </http>
Run Code Online (Sandbox Code Playgroud)

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

编辑:并且一定要留下<anonymous-enabled=false/>标签。

编辑2:纠正我的答案。这个配置应该可以工作。如果没有,那么我们需要开始着眼于更大的图景,并且您必须发布更多应用程序,从完整的配置开始。