在spring security 3.0中添加自定义过滤器

时间:2012-11-07 09:09:29

标签: java spring spring-security servlet-filters

我正在尝试添加另一个名为captcha验证过滤器的过滤器以及spring security的身份验证过滤器。 我收到了这个错误。我错过了什么? bean的初始化失败;嵌套异常是org.springframework.beans.ConversionNotSupportedException:无法将类型'java.util.LinkedHashMap'的属性值转换为属性'filterChainMap'的必需类型'java.util.Map';嵌套异常是java.lang.IllegalStateException:无法将[com.asu.edu.base.vo.CaptchaFilterVO]类型的值转换为属性'filterChainMap [/ **] [3]'所需的类型[javax.servlet.Filter] :找不到匹配的编辑器或转换策略

我的.java文件

public class CaptchaFilterVO {
    @Autowired
    private ReCaptcha reCaptcha = null;

    public void doFilterInternal(HttpServletRequest req, HttpServletResponse res,
            FilterChain chain) throws IOException, ServletException {       
        String recaptcha_response = req.getParameter("recaptcha_response_field");
        String recaptcha_challenge = req.getParameter("recaptcha_challenge_field");
        String remoteAddress = req.getRemoteAddr();
        ReCaptchaResponse reCaptchaResponse = this.reCaptcha.checkAnswer(
                remoteAddress, recaptcha_challenge, recaptcha_response);
        if (!reCaptchaResponse.isValid()) {

            System.out.println("Captcha worong. Please try again.");        

        } 
        else 
        {
            System.out.println("Captcha correct. No need to try again.");
        }
        chain.doFilter(req, res);
    }
}

springsecurity.xml

     <http auto-config="true">
        <!-- intercept-url pattern="/welcome*" access="ROLE_DEPARTMENT_MGR,ROLE_REGULAR_EMP,ROLE_GUEST_USR,ROLE_CORPORATE_MGR" />
        <intercept-url pattern="/admin*" access="ROLE_ADMIN" /-->
        <intercept-url pattern="/login" filters="none" />
        <intercept-url pattern="/resources*" filters="none" />
        <intercept-url pattern="/register" filters="none" />
        <intercept-url pattern="/logout" filters="none" />
        <intercept-url pattern="/loginfailed" filters="none" />
        <intercept-url pattern="/admin*" access="ROLE_ADMIN" />
        <intercept-url pattern="/Dashboard*" access="ROLE_DEPARTMENT_MGR,ROLE_REGULAR_EMP,ROLE_CORPORATE_MGR" />
        <intercept-url pattern="/*" access="IS_AUTHENTICATED_FULLY"/>
        <form-login login-page="/login" default-target-url="/"
            authentication-failure-url="/loginfailed"   />
        <logout logout-success-url="/logout" />
        <custom-filter ref="captchaVerifierFilter" after="FORM_LOGIN_FILTER"/>

</http>     
 <beans:bean id="captchaVerifierFilter" class="com.asu.edu.base.vo.CaptchaFilterVO"/>
 <beans:bean id="springSecurityFilterChain" class="org.springframework.web.filter.DelegatingFilterProxy"/>
<beans:bean id="myfilterChainProxy" class="org.springframework.security.web.FilterChainProxy">
    <filter-chain-map path-type="ant">     
         <filter-chain pattern="/*" filters="springSecurityFilterChain,captchaVerifierFilter"/>
    </filter-chain-map>
 </beans:bean>

编辑:公共类CaptchaFilterVO扩展OncePerRequestFilter实现javax.servlet.Filter 现在我已经扩展了OncePerRequestFilter类,但现在我正面临崩溃。请帮忙。 org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常是org.springframework.beans.BeanInstantiationException:无法实例化bean类[org.springframework.http.HttpRequest]:指定的类是一个接口     org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:894)     org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)     javax.servlet.http.HttpServlet.service(HttpServlet.java:621)     javax.servlet.http.HttpServlet.service(HttpServlet.java:722)     org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:368)     org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:109)     org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)     org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:380)

2 个答案:

答案 0 :(得分:1)

您的CaptchaFilterVO过滤器应该实现javax.servlet.Filter

答案 1 :(得分:1)

您的方法名称(doFilterInternal)表明您希望扩展org.springframework.web.filter.OncePerRequestFilter

添加:

extends OncePerRequestFilter

在您的班级名称之后。

OncePerRequestFilter实施javax.servlet.Filter

和...

Filter base class that guarantees to be just executed once per request,
on any servlet container. It provides a doFilterInternal
method with HttpServletRequest and HttpServletResponse arguments.