简体   繁体   English

Struts2自定义登录拦截器

[英]Struts2 custom login interceptor

Can someone explain me how to write a custom login interceptor that checks username, password and also checks if the users validity date is greater than the current date. 有人可以向我解释如何编写自定义登录拦截器,该拦截器可以检查用户名,密码,还可以检查用户的有效日期是否大于当前日期。 Im new to java programming & struts 2...i would really appreciate step by step info. 我是Java编程和Struts 2的新手...我非常感谢逐步提供的信息。 I get the username, etc info by manual jdbc connection...i have a jndi setup for that. 我通过手动jdbc连接获取用户名等信息...我为此设置了一个jndi设置。 This also needs to have session management. 这也需要进行会话管理。

So a step by step with the following code samples would be nice, 因此,逐步执行以下代码示例将是不错的选择,

1) The dao using jndi to get username,etc from DB 1)使用jndi从数据库获取用户名等的dao

2) The login action with session aware 2)具有会话意识的登录操作

3) interceptor 3)拦截器

4) login.jsp 4)login.jsp

5) struts.xml definition for the interceptor 5)拦截器的struts.xml定义

6) task.jsp and task2.jsp ( internal pages that can only be seen if user is logged in) 6)task.jsp和task2.jsp(仅在用户登录后才能看到的内部页面)

Thank you! 谢谢!

You are on the right track. 您走在正确的轨道上。

There are many articles on that topic (google it). 关于该主题有很多文章(使用谷歌搜索)。 Choose one and try to understand it. 选择一个并尝试理解它。 The interceptor part should look something like this: 拦截器部分应如下所示:

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 ();
    }
}

Above code sample is part of this article where you will also find other steps. 上面的代码示例是本文的一部分,您还将在其中找到其他步骤。

Another way I would recommend is integration of spring security with Struts 2. That way you get secured and proven configurable security stack. 我建议的另一种方法是将Spring安全性与Struts 2集成。这样,您将获得安全且经过验证的可配置安全性堆栈。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM