2 qq 30067349 qq_30067349 于 2016.03.01 14:18 提问

fiter可以过滤jsp等后缀的url,但就是过滤不了.do请求,fiter该怎么写 求帮忙 2C
/*public void doFilter(ServletRequest arg0, ServletResponse arg1,
        FilterChain arg2) throws IOException, ServletException {
    // 获得在下面代码中要用的request,response,session对象
    log.info(".....ThirdPartyStoresFilter...doFilter.....");    
    HttpServletRequest servletRequest = (HttpServletRequest) arg0;
    HttpServletResponse servletResponse = (HttpServletResponse) arg1;
    HttpSession session = servletRequest.getSession();
    String  user =(String) session.getAttribute("username");
    // 获得用户请求的URI
    String path = servletRequest.getRequestURI();
    System.out.println(servletRequest.getRequestURL()+"(00000000");
    // 登陆页面无需过滤
    if (path.indexOf("/login.jsp") > -1 || path.indexOf("/images") > -1
            || path.indexOf("/js") > -1 || path.indexOf("/style") > -1) {
        arg2.doFilter(arg0, arg1);
        return;
    }

    //得到上下文路径
    String returnUrl = servletRequest.getContextPath();
    try {

        // 判断如果没有取到员工信息,就跳转到登陆页面
        if (StringUtils.isBlank(user)) {
            // 跳转到登陆页面
            servletResponse.sendRedirect(returnUrl
                    + "/shop/login.jsp");
        } else {
            // 已经登陆,继续此次请求
            arg2.doFilter(arg0, arg1);
        }
    } catch (Exception e) {
        servletResponse.sendRedirect(returnUrl
                + "/shop/login.jsp");
    }
}
<filter>
    <filter-name>filter</filter-name>
    <filter-class>...</filter-class> 
</filter>
<filter-mapping>
    <filter-name>filter</filter-name>
    <url-pattern>/shop/*</url-pattern>
    </filter-mapping>
*/

4个回答

a510835147
a510835147   2016.03.01 14:34

你是不是还用了 struts啊? .do 请求应该是被它给拦截了

qq_30067349
qq_30067349 回复_追逐梦想_: 我想重新配置个
接近 2 年之前 回复
a510835147
a510835147 回复qq_30067349: 那你把 你这个过滤器的配置放在 struts1 的过滤器的上面就好了
接近 2 年之前 回复
qq_30067349
qq_30067349 是的我用了 struts1
接近 2 年之前 回复
rui888
rui888   Ds   Rxr 2016.03.01 14:35

 A URL pattern is a URI that is relative to the application context. Patterns can include:

Path mapping, starting with / and ending with /*
This pattern identifies any resource that starts with a given path, for example: /catalog/* or /europe/poland/*

Extension mapping, starting with *.
This pattern identifies any resource with the given extension, for example, *.jsp or *.gif

Default servlet mapping, containing only / This pattern identifies the default servlet of the application.
Exact matches
This pattern uses a string that represents a specific resource, for example, /snoop is a servlet mapping and /list/banner.jsp is a file mapping.

So if you need mapping like /secure*, you need either redesign your app to /secure/* so each resource will be a subpath, or apply multiple patters to one web resource collection like /secureA , /secureB, /secureEtc
qq_30067349
qq_30067349 我是指定目录下的所有请求,就是login.jsp不被过滤其他的都要实现过滤
接近 2 年之前 回复
qq_20039385
qq_20039385   2016.03.01 20:15

struts1没用过,对于struts2还是比较熟悉,struts2只要配置了filer拦截器所有的请求都会先进入拦截器的包括静态资源,只有在filer(拦截器)里面doFilter(放行)的才不拦截,建议把配置贴出来看看,拦截器是根据配置的url目录拦截的,/* 拦截所有请求

u013772876
u013772876   2016.03.10 17:05

看看你的web.xml配置是不是对请求后缀做了限制

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!