簡體   English   中英

Struts2自定義登錄攔截器

[英]Struts2 custom login interceptor

有人可以向我解釋如何編寫自定義登錄攔截器,該攔截器可以檢查用戶名,密碼,還可以檢查用戶的有效日期是否大於當前日期。 我是Java編程和Struts 2的新手...我非常感謝逐步提供的信息。 我通過手動jdbc連接獲取用戶名等信息...我為此設置了一個jndi設置。 這也需要進行會話管理。

因此,逐步執行以下代碼示例將是不錯的選擇,

1)使用jndi從數據庫獲取用戶名等的dao

2)具有會話意識的登錄操作

3)攔截器

4)login.jsp

5)攔截器的struts.xml定義

6)task.jsp和task2.jsp(僅在用戶登錄后才能看到的內部頁面)

謝謝!

您走在正確的軌道上。

關於該主題有很多文章(使用谷歌搜索)。 選擇一個並嘗試理解它。 攔截器部分應如下所示:

public String intercept (ActionInvocation invocation) throws Exception {
    // Get the action context from the invocation so we can access the
    // HttpServletRequest and HttpSession objects.
    final ActionContext context = invocation.getInvocationContext ();
    HttpServletRequest request = (HttpServletRequest) context.get(HTTP_REQUEST);
    HttpSession session =  request.getSession (true);

    // Is there a "user" object stored in the user's HttpSession?
    Object user = session.getAttribute (USER_HANDLE);
    if (user == null) {
        // The user has not logged in yet.

        // Is the user attempting to log in right now?
        String loginAttempt = request.getParameter (LOGIN_ATTEMPT);
        if (! StringUtils.isBlank (loginAttempt) ) { // The user is attempting to log in.

            // Process the user's login attempt.
            if (processLoginAttempt (request, session) ) {
                // The login succeeded send them the login-success page.
                return "login-success";
            } else {
                // The login failed. Set an error if we can on the action.
                Object action = invocation.getAction ();
                if (action instanceof ValidationAware) {
                    ((ValidationAware) action).addActionError ("Username or password incorrect.");
                }
            }
        }

        // Either the login attempt failed or the user hasn't tried to login yet, 
        // and we need to send the login form.
        return "login";
    } else {
        return invocation.invoke ();
    }
}

上面的代碼示例是本文的一部分,您還將在其中找到其他步驟。

我建議的另一種方法是將Spring安全性與Struts 2集成。這樣,您將獲得安全且經過驗證的可配置安全性堆棧。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM