SSM登陸攔截器實現
阿新 • • 發佈:2018-09-14
nbsp dex ssm ont pri pre inter implement als
首先在springmvc中配置攔截器
<!-- 配置攔截器 --> <mvc:interceptors> <mvc:interceptor> <!-- 攔截所有mvc控制器 --> <mvc:mapping path="/**"/> <!-- mvc:exclude-mapping是另外一種攔截,它可以在你後來的測試中對某個頁面進行不攔截,這樣就不用在 LoginInterceptor的preHandler方法裏面獲取不攔截的請求uri地址了(優選)--> <mvc:exclude-mapping path="/admin/loginPage.action" /> <mvc:exclude-mapping path="/admin/varifyCode.action" /> <bean class="nyist.admin.utils.LoginInterceptor"></bean> </mvc:interceptor> </mvc:interceptors>
然後寫攔截器:
public class LoginInterceptor implements HandlerInterceptor { @Override public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) throws Exception { // TODO Auto-generated method stub } @Override public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3) throws Exception { // TODO Auto-generated method stub } @Override public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception { HttpSession session = arg0.getSession(); //String uri = request.getRequestURI(); // 獲取登錄的uri,這個是不進行攔截的 //if(session.getAttribute("LOGIN_USER")!=null || uri.indexOf("system/login")!=-1) {// 說明登錄成功 或者 執行登錄功能 if(session.getAttribute("admin")!=null) { // 登錄成功不攔截 return true; }else { // 攔截後進入登錄頁面 arg1.sendRedirect(arg0.getContextPath()+"/admin/loginPage.action"); return false; } } }
攔截器這樣就設置好了
SSM登陸攔截器實現