2012-04-27 32 views
0

我在我的應用程序中實現了HandlerInterceptorAdapter,在preHandle方法中,我正在檢查會話是否具有「用戶」屬性。如果爲null,那麼我將重定向發送到登錄頁面。如何在Spring中驗證會話?

if (request.getSession().getAttribute("user")==null) { 
      System.out.println("hello"); 
      response.sendRedirect("login.htm"); 
      return true; 
      } 

     return super.preHandle(request, response, handler); 
    } 
當我打開瀏覽器中的任何URL

我得到一個消息服務器重定向此地址的請求的方式,將永遠不會完成 火狐檢測。

我該怎麼做,請建議我!!!!!!!!

回答

0
@RequestMapping(value="/index.html", method=RequestMethod.GET) 
public String indexView(HttpServletRequest request){ 

if (request.getSession().getAttribute("user")==null) { 
    System.out.println("hello"); 
    return "redirect:/login.html" 
    } 
else 
    return "index"; 
} 

坦率地說,我相信你應該使用Spring Security。這比使用Spring實現Web容器安全性更好,更少錯誤,更安全和更快速。

希望它有幫助。

+0

我在哪裏寫這段代碼? – ravitech 2012-04-27 12:28:07

+0

Sprring MVC控制器。用@Controller註釋標記。 – 2012-04-27 12:29:45