于 2004-12-28 18:49
帮忙看个限制url访问限制的过滤器实现,用了这个后,图片显示不出来,不知什么原因,
要是不设contentType页面也会乱码,
public class UrlAccessFilter implements Filter {
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest)request;
HttpServletResponse httpResponse = (HttpServletResponse)response;
HttpSession session = httpRequest.getSession();
//httpResponse.setContentType("text/html;charset=gb2312");
httpResponse.setContentType(httpRequest.getContentType());
PrintWriter out = httpResponse.getWriter();
System.out.println("进行 URL 访问校验......");
String user = (String)session.getAttribute("CZRY_DM");
if (!user.equals("00000000000")) {
String url = httpRequest.getRequestURL().toString();
for (int i=0; i
if(url.substring(i,i+2).equals("//")) {
url = url.substring(i+2,url.length());
break;
}
}
for (int i=0; i
if (url.substring(i,i+1).equals("/")) {
url = url.substring(i,url.length());
break;
}
}
String urlPage = null;
for (int i=url.length()-1; i>0; i--) {
if(url.substring(i-1,i).equals("/")) {
urlPage = url.substring(i,url.length());
break;
}
}
System.out.println(url);
if (!urlPage.equals("login.jsp")) {
Object obj = session.getAttribute("urlAccess");
if (obj!=null) {
java.util.Vector urlAccess = (java.util.Vector)obj;
boolean pass = false;
for(int i=0; i
String str = (String)urlAccess.get
;
System.out.println(str);
if (url.substring(0,str.length()).equals(str)) {
pass = true;
break;
}
}
System.out.println(pass);
if (!pass) {
httpRequest.setAttribute("errorMessage", "您没有访问该页面的权限");
session.setAttribute("errorMessage", "您没有访问该页面的权限");
// out.println("");
// out.println("");
// httpResponse.sendRedirect("/fzgs-fjky/main.html");
httpResponse.sendRedirect("/fzgs-fjky/include/redirect.jsp");
}
chain.doFilter(request,response);
} else {
httpRequest.setAttribute("errorMessage", "您没有被赋于权限,请与您的管理员联系");
session.setAttribute("errorMessage", "您没有访问该页面的权限");
// out.println("");
// out.println("");
// httpResponse.sendRedirect("/fzgs-fjky/main.html");
httpResponse.sendRedirect("/fzgs-fjky/include/redirect.jsp");
}
}
} else {
chain.doFilter(request,response);
}
}
}
我是世界的缔造者!