有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在JBoss AS 6中侦听登录事件

我有一个在JBossAS6中运行的应用程序。身份验证正在使用“表单”身份验证方法工作,并且用户正在正确登录

每当用户成功登录时,我希望能够调用一段自定义的静态代码

不幸的是,我找不到任何监听器、钩子或回调,它们将在成功登录时执行代码。HttpSessionListener确实有一个“sessionCreated”事件,但一旦用户访问任何页面,就会调用该事件,即使用户尚未成功登录。这意味着,即使查看登录表单也会触发事件

有人能给我介绍一些JBossAS6(或同等版本)的文档吗?这些文档展示了如何在用户首次成功登录时运行自定义代码

提前谢谢


共 (3) 个答案

  1. # 1 楼答案

    我能想到的解决办法是使用一个CustomFormAuthenticator扩展org.apache.catalina.authenticator.FormAuthenticator 并将其注册到/server/default/deployers/jbossweb.deployer/META-INF/war-deployers-jboss-beans.xml。 现在在JBossAS7中,他们引入了阀的概念,您可以在jboss-web.xmliteself中注册CustomAuthenticator

    类似于

    public class CustomFormAuthenticator extends FormAuthenticator {
        @override
        public boolean authenticate(Request request, Response response, LoginConfig config) throws IOException {
            boolean authenticate = super.authenticate(request, response, config);
            //here you might need to keep track whether your custom/static code executed once or not,
            //just to avoid executing the same code again and again.
            if(authenticate) {
                int i = CustomSingleton.getInstnce().getExecuteCount();
                if(i <= 0) {
                    //invoke custom code.
                    //increment the count
                    CustomSingleton.getInstnce().incrementExecuteCount();
                }
            }
        }
    }
    

    现在,需要在/server/default/deployers/jbossweb.deployer/META-INF/war-deployers-jboss-beans.xml中的server注册这个 将以下entry添加到authenticators部分

    <entry>
        <key>CUSTOM-FORM</key>
        <value>full.qaulified.CustomFormAuthenticator</value>
    </entry>
    

    然后,在网络上。xml将CUSTOM-FORM作为auth-method

    <login-config>
         <auth-method>CUSTOM-FORM</auth-method>
              <form-login-config>
                   <form-login-page>/login.html</form-login-page>
                   <form-error-page>/login-error.html</form-error-page>
              </form-login-config>
    <login-config>
    

    希望这有帮助

  2. # 2 楼答案

    您可以在安全Servlet前面添加ServletFilter实现

    在每次调用时,过滤器将测试HttpSession中的布尔标志notFirstCall

    如果该标志不存在,则该请求是用户登录后的第一个请求。它可以调用指定的作业,然后设置标志notFirstCall,将该作业标记为此会话完成

  3. # 3 楼答案

    javax.servlet.http.HttpSessionBindingListener这样的东西怎么样? 创建一个对象,在用户成功登录时按您喜欢的方式填充该对象,并将其作为属性添加到用户会话中。因此:

    public class User implements Serializable, HttpSessionBindingListener {
    private String userId;
    private Timestame logonTime;
    // any additional fields
    
    @Override
    public void valueBound(HttpSessionBindingEvent event) {
    // this method called when this object is attached to a session
        log.debug("user " + this.userId + "bound to a session - user logged in");
    // do stuff
      }
    @Override
      public void valueUnbound(HttpSessionBindingEvent event) {
    // this method called when user's session ends, value unbound, etc
        log.debug("user " + this.userId + "logged off");
    // do other stuff
      }
    
    }
    

    要绑定对象,请执行以下操作:

    // you don't create this object until a user logs in
    User userObject = new User();
    userObject.setUserId();
    userObject.setLogonTime();
    // get your request object however you normally get it
    HttpServletRequest request.getSession().setAttribute("loggedInUser", userObject);
    

    设置属性时,它将调用valueBound方法 这对于跟踪用户也很方便(将登录/注销信息保存到db等)