在JBoss AS 6中监听登录事件

我有一个在JBoss AS6中运行的应用程序。 身份validation正在使用“FORM”身份validation方法,并且用户正在正确登录。

我希望能够在用户成功登录时调用一段自定义静态代码。

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

有人能指出我的JBoss AS 6(或同等版本)的一些文档,它们展示了当用户第一次成功登录时如何运行自定义代码?

提前致谢。

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

在每次调用时,filter将在HttpSession测试布尔标志notFirstCall

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

我可以想到的解决方法是使用CustomFormAuthenticator扩展org.apache.catalina.authenticator.FormAuthenticator并将其注册到/server/default/deployers/jbossweb.deployer/META-INF/war-deployers-jboss-beans.xml 。 现在在Jboss AS 7中,他们引入了阀门概念,您可以在jboss-web.xml自行注册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.xmlserver注册此内容。在authenticators部分添加以下entry

  CUSTOM-FORM full.qaulified.CustomFormAuthenticator  

然后,在web.xml中将CUSTOM-FORM作为auth-method

  CUSTOM-FORM  /login.html /login-error.html   

希望这可以帮助..

像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等)。