1.流程java
判斷 url 是否爲 登陸頁面,是則放行spring
否:獲取session,存在 放行,不存在攔截數據庫
2. 稅務系統登陸:cookie
public class LoginFilter implements Filter { @Override public void destroy() { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest)servletRequest; HttpServletResponse response = (HttpServletResponse)servletResponse; String uri = request.getRequestURI(); //判斷當前請求地址是不是登陸的請求地址 if(!uri.contains("sys/login_")){ //非登陸請求 if(request.getSession().getAttribute(Constant.USER) != null){ //說明已經登陸過 //判斷是否訪問納稅服務系統 if(uri.contains("/nsfw/")){ //訪問納稅服務子系統 User user = (User)request.getSession().getAttribute(Constant.USER); //獲取spring容器 WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext()); PermissionCheck pc = (PermissionCheck)applicationContext.getBean("permissionCheck"); if(pc.isAccessible(user, "nsfw")){ //說明有權限,放行 chain.doFilter(request, response); } else { //沒有權限,跳轉到沒有權限提示頁面 response.sendRedirect(request.getContextPath() + "/sys/login_toNoPermissionUI.action"); } } else { //非訪問納稅服務子系統,則直接放行 chain.doFilter(request, response); } } else { //沒有登陸,跳轉到登陸頁面 response.sendRedirect(request.getContextPath() + "/sys/login_toLoginUI.action"); } } else { //登陸請求;直接放行 chain.doFilter(request, response); } } @Override public void init(FilterConfig arg0) throws ServletException { } }
3. 電力系統登陸攔截session
public class SystemFilter implements Filter { //存放系統沒有Session,可是須要訪問的url,像這樣的鏈接須要放行 List<String> list = new ArrayList<String>(); /**當服務啓動的時候初始化*/ public void init(FilterConfig config) throws ServletException { list.add("/index.jsp"); list.add("/image.jsp"); list.add("/system/elecMenuAction_menuHome.do"); list.add("/error.jsp"); list.add("/system/elecMenuAction_logout.do"); } /**每次訪問URL鏈接以前,都要訪問的方法*/ public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) res; //記住個人代碼放置到過濾器中 String path = request.getServletPath(); this.forwordIndexPage(request,path); //若是當前訪問的鏈接是定義list中存放的鏈接,此時須要放行 if(list.contains(path)){ chain.doFilter(request, response); return; } //存在Session(不爲空)的時候要放行,Session爲空的時候,定向到首頁 ElecUser elecUser = (ElecUser)request.getSession().getAttribute("globle_user"); if(elecUser!=null){ //從數據庫中查詢獲取登陸狀態 WebApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext()); IElecUserService elecUserService = (IElecUserService)ac.getBean(IElecUserService.SERVICE_NAME); ElecUser user = elecUserService.findElecUserByID(elecUser); Long logonStateDb = user.getLogonState(); //從Session中查詢登陸狀態 Long logonState = elecUser.getLogonState(); if(logonStateDb.equals(logonState)){ chain.doFilter(request, response); return; } else{ request.setAttribute("errorMsg", "您的帳號已經在異地登陸,換句話說,您被踢了!"); } } //重定向,因爲傳遞request做用域失效 // response.sendRedirect(request.getContextPath()+"/error.jsp"); //轉發 request.getRequestDispatcher("/error.jsp").forward(request, response); } /**銷燬*/ public void destroy() { } //記住個人代碼放置到過濾器中,在跳轉到index.jsp頁面以前 private void forwordIndexPage(HttpServletRequest request, String path) { if(path.equals("/index.jsp")){ String name = ""; String password = ""; String checked = ""; Cookie [] cookies = request.getCookies(); if(cookies!=null && cookies.length>0){ for(Cookie cookie:cookies){ if(cookie.getName().equals("name")){ name = cookie.getValue(); //處理中文二進制解碼問題 try { name = URLDecoder.decode(name, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } checked = "checked"; } if(cookie.getName().equals("password")){ password = cookie.getValue(); } } } request.setAttribute("name", name); request.setAttribute("password", password); request.setAttribute("checked", checked); } } }
攔截器攔截 url :app
<!-- 自定義過濾器,要放置到struts2的過濾器的上面 --> <filter> <filter-name>SystemFilter</filter-name> <filter-class>cn.itcast.elec.util.SystemFilter</filter-class> </filter> <filter-mapping> <filter-name>SystemFilter</filter-name> <url-pattern>*.jsp</url-pattern> <url-pattern>*.do</url-pattern> </filter-mapping>