자바 구현 차단기

2162 단어 자바
1. web. xml 설정
	
	
		loginfilter
		com.LoginFilter
	
	
		loginfilter
		/*
	

2. Filter 작성
package com.parma.electric.core.interceptor;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *        
 * @author PhilShen
 *
 */
public class LoginFilter implements Filter {

	private static Logger LOGGER = LoggerFactory.getLogger(LoginFilter.class);

	@Override
	public void destroy() {

	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		String requestURI = req.getRequestURI();
		if (LOGGER.isDebugEnabled()) {
			String params = "";
			Map parameterMap = req.getParameterMap();
			for (Entry entry : parameterMap.entrySet()) {
				params += entry.getKey() + ":" + Arrays.toString(entry.getValue());
			}
			LOGGER.debug("  url:{}   :{}", req.getRequestURI(), params);
		}

		if (requestURI.endsWith("login.jsp") == false && requestURI.endsWith(".jsp")) {
				String str = (String) req.getSession().getAttribute("isLogin");
				if (str == null) {
					HttpServletResponse resp = (HttpServletResponse) response;
					resp.sendRedirect("login.jsp");
					return;
				}
		}
		chain.doFilter(request, response);
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {

	}

}

좋은 웹페이지 즐겨찾기