过滤器(代码总结)
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
public class EncodeFilter implements Filter {
private String encode = "utf-8";
public void destroy() {
}
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
arg0.setCharacterEncoding(this.encode);
arg2.doFilter(arg0, arg1);
}
public void init(FilterConfig arg0) throws ServletException {
String encoding = arg0.getInitParameter("encodevalue");
if(encoding != null && encoding.length() > 0){
this.encode = encoding;
}
}
}
-----------------------------------------------------------------------
public class LoginFilter implements Filter {
public void doFilter(ServletRequest arg0, ServletResponse arg1,FilterChain arg2) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest)arg0;
HttpSession session = request.getSession();
Users user = (Users)session.getAttribute(ER_SESSION_KEY);
String uri = request.getRequestURI();
//资源区分
if(uri.endsWith(".jsp") || uri.endsWith(".do")){
//如果用户请求的是login.jsp或者Userlogin.do来完成登录,那么对于这两资源需要放行
if(uri.indexOf("login.jsp") != -1 || uri.indexOf("UserLoginAction.do") != -1){
arg2.doFilter(arg0, arg1);
}else{
//如果用户没有登录那么需要用户登录
if(user == null || user.getUsername().length() <= 0){
request.setAttribute(Constants.REQUEST_MSG_KEY,"不登录不好使!");
request.getRequestDispatcher("login.jsp").forward(arg0, arg1);
}else{
arg2.doFilter(arg0, arg1);
}
}
}else{
arg2.doFilter(arg0, arg1);
}
}
}
-----------------------------------------------------------------------
public class SafeFilter implements Filter {
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest)arg0;
HttpSession session = request.getSession();
String uri = request.getRequestURI();
Users user = (Users)session.getAttribute(ER_SESSION_KEY);
if(uri.endsWith(".jsp") || uri.endsWith(".do")){
if(uri.indexOf("login.jsp") != -1 || uri.indexOf("UserLoginAction.do") != -1){
arg2.doFilter(arg0, arg1);
}else{
Set
boolean flag = false;
for(Menus m:menu){
Set
for(Funs f:fun){
if(uri.indexOf(f.getFunurl()) != -1){
flag = true;
break;
}
}
if(flag)
break;
}
if(flag){
arg2.doFilter(arg0, arg1);
}else{
((HttpServletResponse)arg1).sendRedirect("norole.jsp");
}
}
}else{
arg2.doFilter(arg0, arg1);
}
}
}