使用filter改变改变地址,但又不想被本过滤器再次拦截的方法

继承HttpServletRequestWrapper重写里面的方法

如果是servlet重写getRequestURI()

如果是spring mvc重写 getServletPath() 

可以根据getDispatcherType()类确定是那种调度类型,一般客户端请求action,或controller都是REQUEST,controller跳转到页面是FORWARD。


使用filter改变改变地址,但又不想被本过滤器再次拦截的方法_第1张图片



使用filter改变改变地址,但又不想被本过滤器再次拦截的方法_第2张图片



package com.hlzt.bjcl.filter;


import java.io.IOException;


import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;


import com.hlzt.dev.core.common.AppContext;
import com.hlzt.dev.web.context.HttpAppContext;
import com.hlzt.dev.web.context.HttpContextFactory;
import com.hlzt.dev.web.mvc.ContextVarsHelper;
import com.hlzt.platform.grant.model.UserVO;


public class MyFilter implements Filter{



public void doFilter(ServletRequest req, ServletResponse resp,  
            FilterChain chain) throws IOException, ServletException {  
HttpServletRequest request = (HttpServletRequest) req;
 HttpServletResponse response = (HttpServletResponse) resp;

String url =request.getRequestURI(); 
UserVO currentUser=(UserVO)HttpContextFactory.getContext(request, response).getContextUser();
if(currentUser!=null)
{
if(currentUser.getUsertype()<=0){//如果
boolean fbUrl=url.contains("/front/");
if(fbUrl)
url="/404.d";
else if(url.contains("/back/"));
else
{
  url=url.substring(0, url.lastIndexOf("/"))+"/back"+url.substring(url.lastIndexOf("/"));
}
}
else
{
boolean fbUrl=url.contains("/back/");
if(fbUrl)
url="/404.d";
else if(url.contains("/front/"));

else
  url=url.substring(0, url.lastIndexOf("/"))+"/front"+url.substring(url.lastIndexOf("/"));
}
}

//System.out.println("请求前"+request.getServletPath());
chain.doFilter(new MyHttpServletRequestWrapper(request,url), response);
// System.out.println("请求后"+request.getServletPath());
    }


@Override
public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub

}


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

}  

/**
* 重新封装request包装类
* @author Administrator
*
*/
class MyHttpServletRequestWrapper extends HttpServletRequestWrapper{
private String url;

public MyHttpServletRequestWrapper(HttpServletRequest request,String url) {
super(request);
this.url=url;
//System.out.println("####################url改变后"+request.getRequestURI());
}


/*@Override
         public String getRequestURI() {//servlet根据这个获得路径
System.out.println("getRequestURI执行");
           return "/bjcl/user/back/index.shtm";
           
         }*/
/*@Override
public StringBuffer getRequestURL(){
System.out.println("getRequestURL执行");
return new StringBuffer("/bjcl/user/back/index.shtm");

}*/
@Override
public String getServletPath() {//spring mvc 根据这个方法获得路径
System.out.println(super.getMethod()+"  "+super.getAuthType()+"  "+super.getContentType()+"  "+super.getDispatcherType().name()+"******************请求地址"+super.getServletPath());
if(super.getDispatcherType().name().equals("REQUEST"))//当是客服端请求服务器类型时,设置新url
//新url地址为spring  mvc controller前的地址
return url;
else//当不是请求类型时,此时为服务器跳转,转发为FORWARD等等,默认服务器跳转地址
//服务器跳转的地址为/page/bjcl/order/user.jsp,是controller return后的地址。
   return super.getServletPath();
}


}
   


}

你可能感兴趣的:(使用filter改变改变地址,但又不想被本过滤器再次拦截的方法)