ClasscastException:MyClass无法强制转换为javax.servlet.Filter

时间:2015-02-10 21:36:02

标签: java maven servlets servlet-filters

我正在尝试设置过滤器进行身份验证。

我的过滤器类如下

public class AuthenticationFilter extends HttpServlet{

    /**
     * 
     */
    private static final long serialVersionUID = -7678740843612123432L;

    public void destroy() {
        // TODO Auto-generated method stub

    }
   ///Other methods...
    public void doFilter(HttpServletRequest request, HttpServletResponse response,
            FilterChain chain) throws IOException, ServletException {

        Map<String,String> map = getHeadersInfo(request);

        chain.doFilter(request, response);
    }
        ///Other methods.

相应的web.xml如下。

 <filter>
    <filter-name>AuthFilter</filter-name>
    <filter-class>com.mypackage.AuthenticationFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>AuthFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

在搜索时,我发现由于pom.xml中的错误依赖性,很多人都会遇到此问题。但我确保相应的jar具有provided范围。

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-server</artifactId>
</dependency>

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-servlet</artifactId>
</dependency>
<!-- Other dependencies-->

有关我可能做错的事情的任何想法?链接/建议赞赏。

1 个答案:

答案 0 :(得分:2)

您的过滤器应implement Filter它所声称的接口, extend HttpServlet,除非您将两个功能合并为一个类,不是个好主意。