码迷,mamicode.com
首页 > 其他好文 > 详细

过滤器权限校验

时间:2015-07-29 22:40:32      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:

package com.web.study;

import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.Filter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class PermissionFilter implements Filter {

	public PermissionFilter() {
		
	}

	private FilterConfig filterConfig;

	public void init(FilterConfig filterConfig) throws ServletException {
		
	}

	public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain) throws IOException,ServletException {

		HttpServletRequest request = (HttpServletRequest)req;
		HttpServletResponse response = (HttpServletResponse)res;
		
		String servletPath = request.getServletPath();
		HttpSession session = request.getSession();
		String flag = (String)session.getAttribute("flag");
		
		if (servletPath.equals("/Jsp/logon.jsp")||servletPath.equals("/Jsp/index.jsp")||servletPath.equals("/LoginServlet")) {
			filterChain.doFilter(req, res);
		}
		else {
			if(flag!=null&&flag.equals("login_success")){
				filterChain.doFilter(req, res);
			}
			else if (flag!=null && flag.equals("login_error")) {
				request.setAttribute("return_uri", servletPath);
				RequestDispatcher rd = request.getRequestDispatcher("/Jsp/login.jsp");
				rd.forward(request, response);
			}
			else {
				request.setAttribute("return_uri", servletPath);
			    RequestDispatcher rd = request.getRequestDispatcher("/Jsp/login.jsp");
			    rd.forward(request, response);
			}
			
		}
		
		
	}

	public void destroy() {

	}
}

  

过滤器权限校验

标签:

原文地址:http://www.cnblogs.com/wzz1020/p/4687343.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!