⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 authenticationfilter.java

📁 fahirjgjkdfjkl 丰卡时间阿科技安分 发 啊啊啊啊啊
💻 JAVA
字号:
package com.bookshop.web.filter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import javax.servlet.Filter;
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.http.HttpServletRequest;

/**
 * �ж��û��Ƿ��¼��filter
 */
public class AuthenticationFilter implements Filter {
	/**
	 * The filter configuration object we are associated with.  If this value
	 * is null, this filter instance is not currently configured.
	 */
	protected FilterConfig filterConfig = null;

	/**
	 * Enable this filter or not.
	 */
	protected boolean enabled = true;

	/**
	 * The request will be ignored.
	 */
	protected String[] ignore = null;

	/**
	 * If authentication failed, will be redirect to this path.
	 */
	protected String redirectPath = null;

	/**
	 * Take this filter out of service.
	 */
	public void destroy() {
		this.filterConfig = null;
	}

	/**
	 * Processing authentication
	 *
	 * @param request The servlet request we are processing
	 * @param result The servlet response we are creating
	 * @param chain The filter chain we are processing
	 *
	 * @exception IOException if an input/output error occurs
	 * @exception ServletException if a servlet error occurs
	 */
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		boolean redirect = true;

		if (enabled) {
			if (req.getSession().getAttribute("user") == null) {
				for (int i = 0; i < ignore.length; i++) {

					if (ignore[i].equals(req.getServletPath()))
						redirect = false;
				}
				if (redirect) {
					RequestDispatcher rd = null;
					rd = req.getRequestDispatcher(redirectPath);
					rd.forward(request, response);
				} else {
					// Pass control on to the next filter
					chain.doFilter(request, response);//若无此句虽执行此filter,但是request不会向后传递,导致不会进入请求页面
				}
			} else {
				// Pass control on to the next filter
				chain.doFilter(request, response);
			}
		} else {
			// Pass control on to the next filter
			chain.doFilter(request, response);
		}
	}

	/**
	 * Place this filter into service.
	 *
	 * @param filterConfig The filter configuration object
	 */
	public void init(FilterConfig filterConfig) throws ServletException {
		this.filterConfig = filterConfig;
		this.redirectPath = filterConfig.getInitParameter("redirect");
		
		String value = filterConfig.getInitParameter("ignore");

			StringTokenizer token = new StringTokenizer(value, ", ");
			List list = new ArrayList();
			while (token.hasMoreElements()) {
				list.add(token.nextToken());
			}
			ignore = (String[]) list.toArray(new String[0]);
		
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -