编程语言
首页 > 编程语言> > 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");

		}
	}

}

标签:Set,java,示例,request,ServletResponse,拦截器,ALLOWED,login,response
来源: https://blog.csdn.net/cxyd4399/article/details/123190600