Java教程

java登录判断拦截器示例

本文主要是介绍java登录判断拦截器示例,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
@WebFilter(filterName = "validateLoginStatusFilter", urlPatterns = { "/*" }, asyncSupported = true)
public class validateLoginStatusFilter implements Filter {

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		String ServletPath = ((HttpServletRequest) request).getServletPath();
		final Set<String> ALLOWED_PATHS = Collections
				.unmodifiableSet(Set.of("/login.jsp", "/signup.jsp", "/login", "/register",));
		final Set<String> ALLOWED_STATIC = Collections
				.unmodifiableSet(Set.of(".css", ".js", ".jpg", ".png", ".svg", ".webp", ".tff", ".woff", ".woff2"));
		if (CookieUtils.hasCookie((HttpServletRequest) request, "id") || ALLOWED_PATHS.contains(ServletPath)
				|| ALLOWED_STATIC.stream().filter(e -> ServletPath.endsWith(e)).count() > 0) {
			chain.doFilter(request, response);
		} else {
			((ServletResponse) response).setContentType("text/html;charset=UTF-8");
			PrintWriter out = ((ServletResponse) response).getWriter();
			out.print(
					"<script charset='utf-8' type='text/javascript' language='javascript'>alert('you are not log in yet!')</script>");
			((HttpServletResponse) response).setHeader("Refresh", "1;URL=login.jsp");

		}
	}

}

这篇关于java登录判断拦截器示例的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!