今天其实是很烦躁的一天。浑身乏力,好蛋疼,很多事,都感觉很压抑,效率很低。
过滤器:
web.xml配置: <filter> <filter-name>mobileFilter</filter-name> <filter-class>com.oauth.system.utils.MobileFilter</filter-class> <init-param> <param-name>logonStrings</param-name><!-- 对登录页面不进行过滤 --> <param-value>/mobileUser/forgetPassword;/mobileUser/init;/mobileUser/initLogin;/mobileUser/login;/mobileUser/initRegedit;/mobileUser/validateAccountCode;/mobileUser/validateBindPhone;/mobileUser/register;/mobileUser/sendVerifyCode;/mobileUser/verifyCode;</param-value> </init-param> <!-- <init-param> <param-name>includeStrings</param-name>只对指定过滤参数后缀进行过滤 <param-value>.do;.jsp</param-value> </init-param> --> <init-param> <param-name>redirectPath</param-name><!-- 未通过跳转到登录界面 --> <param-value>/mobileUser/initLogin</param-value> </init-param> </filter> 对应java类: package com.oauth.system.utils; import java.io.IOException; 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 javax.servlet.http.HttpServletResponseWrapper; import com.oauth.mobile.bean.UserMb; public class MobileFilter implements Filter { public FilterConfig config; @Override public void destroy() { this.config = null; } /* (non-Javadoc) * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain) * 过滤手机店的 */ @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest hrequest = (HttpServletRequest)request; HttpServletResponseWrapper wrapper = new HttpServletResponseWrapper((HttpServletResponse) response); String logonStrings = config.getInitParameter("logonStrings"); //<!--登录登陆页面--> String redirectPath = hrequest.getContextPath() + config.getInitParameter("redirectPath");//<!--没有登陆转向页面--> String openid = request.getParameter("openid"); String[] logonList = logonStrings.split(";"); if (MobileFilter.isContains(hrequest.getRequestURI(), logonList)) {//<!--对登录页面不进行过滤--> chain.doFilter(request, response); return; } UserMb user = ( UserMb ) hrequest.getSession().getAttribute("mobileUser");//<!--判断用户是否登录--> if (user == null) { wrapper.sendRedirect(redirectPath); return; }else { chain.doFilter(request, response); return; } } @Override public void init(FilterConfig config) throws ServletException { this.config = config; } public static boolean isContains(String container, String[] regx) { boolean result = false; for (int i = 0; i < regx.length; i++) { if (container.indexOf(regx[i]) != -1) { return true; } } return result; } }
时间: 2024-12-17 17:26:20