2013-03-02 82 views
0

我是使用jsp,servlet,beans等的新手......。jsp:安全選擇菜單

也許一個奇怪的問題,但在jsp中製作選擇菜單的最安全方法是確保無法直接訪問它。

在這一刻我有一個登錄系統,根據我從數據庫檢索的用戶的「種類」,我根據他們擁有的「權限」將它們發送到特定的jsp。在這個頁面上,他們將獲得他們可以做的選擇。

但如果我使用類似:

<a href="next.jsp">next option</a> 

它會很容易只從外部訪問這些網頁旁邊(沒有多大用處的登錄系統即可)。

我可以使用我從前一頁檢索的bean檢查它是否爲空(如果直接訪問此頁面,這會是這樣)或類似的東西。

任何建議將受到歡迎。 thx

回答

0

您可以使用Servlet Filter來驗證用戶是否已登錄系統並驗證用戶是否有權訪問此頁面。一個例子將如StackOverflow Servlet-Filters wiki中所述。發佈相關代碼:

@Override 
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { 
    HttpServletRequest request = (HttpServletRequest) req; 
    HttpServletResponse response = (HttpServletResponse) res; 
    HttpSession session = request.getSession(false); 

    //session.getAttribute("user") contains the user info in session 
    if (session == null || session.getAttribute("user") == null) { 
     // No logged-in user found, so redirect to login page. 
     response.sendRedirect(request.getContextPath() + "/login"); 
    } else { 
     // Logged-in user found, so just continue request. 
     chain.doFilter(req, res); 
    } 
}