日期:2014-05-19  浏览次数:20738 次

用filter 做拦截器还要在别的地方配置么
springmvc用filter 做拦截器还要在别的地方配置么
Java code
package com.bolo.examples;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import com.bolo.examples.base.entity.User;


public class MyFilter implements Filter {
    private static final String FILTERED_REQUEST = "@@session_context_filtered_request";
    private static final String[] INHERENT_ESCAPE_URIS = { "/index.jsp",
            "/index.html", "/login.jsp" };
    public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {
        if (request != null && request.getAttribute(FILTERED_REQUEST) != null) {
            chain.doFilter(request, response);
        } else {
            request.setAttribute(FILTERED_REQUEST, Boolean.TRUE);
            HttpServletRequest httpRequest = (HttpServletRequest) request;
            User userContext = getSessionUser(httpRequest);
            if (userContext == null    && !isURILogin(httpRequest.getRequestURI(), httpRequest)) {
                String toUrl = httpRequest.getRequestURL().toString();
                if (!StringUtils.isEmpty(httpRequest.getQueryString())) {
                    toUrl += "?" + httpRequest.getQueryString();
                }
                httpRequest.getSession().setAttribute(LOGIN_TO_URL, toUrl);
                request.getRequestDispatcher("/index.jsp").forward(request,response);
                return;
            }
            chain.doFilter(request, response);
        }
    }

    //@Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }
   /**
    * 当前URI资源是否需要登录才能访问
    * @param requestURI
    * @param request
    * @return
    */
    private boolean isURILogin(String requestURI, HttpServletRequest request) {
        if (request.getContextPath().equalsIgnoreCase(requestURI)||(request.getContextPath() + "/").equalsIgnoreCase(requestURI))
            return true;
        for (String uri : INHERENT_ESCAPE_URIS) {
            if (requestURI != null && requestURI.indexOf(uri) >= 0) {
                return true;
            }
        }
        return false;
    }

    protected User getSessionUser(HttpServletRequest request) {
        return (User) request.getSession().getAttribute(USER_CONTEXT);
    }

    //@Override
    public void destroy() {

    }
}



------解决方案--------------------
和配置普通的servlet差不多吧.你自己测试下能行没就知道了啊
------解决方案--------------------
编写完filter,需要在web.xml中进行配置,代码如下:
<filter>
<filter-name>myFilter</filter-name>
<filter-class>--此处为你编写的MyFilter路径--</filter-class>
</filter>

<filter-mapping>
<filter-name>myFilter</filter-name>
<url-pattern>/*</url-pattern> //若想进行细化过滤,使用/*;若只想对jsp页面进行过滤,则使用*.jsp;意思类推
</filter-mapping>