0

I have created a Java application and used Java Service Wrapper method 3 that implements the WrapperListener to convert it to a Windows Service.

The WrapperManager can be used to identify various events that get raised and caught in the wrapper listener controlEvent function, this includes a user logging off, however doesn't have a user login event.

Is there a way I can detect a user logging in to the system and perform some kind of action in my Java application? If not, is there a way to detect a spike in CPU usage that I can use to make an assumption of a user logging in? This is required because my application needs to perform some action when a user logs in.

4

2 回答 2

2

服务控制管理器中没有用于用户登录的登录控制代码。此外,Java Service Wrapper 使用公共控制信号。使用事件处理时使用 CTRL_LOGON_EVENT 没有多大意义:https ://msdn.microsoft.com/en-us/library/windows/desktop/ms683242.aspx

您可能需要考虑在 Windows 事件日志中查看安全性下的事件 id 4648 并编写事件订阅的实现,但这需要了解 Windows API 和 Java Native Access 库的使用:

https://msdn.microsoft.com/en-us/library/windows/desktop/aa385771.aspx

上面的解决方法并不理想,但它是一种可能的途径。

更多关于您正在从事的服务的信息会很有帮助。

编辑:

在用户登录时,该程序应以用户身份运行,(请参阅底部的命令):

登录服务延迟.java

import org.tanukisoftware.wrapper.WrapperManager;

public class LoginServiceDefer {
    public static String svc = "loginservice";
    public static int ctrl   = 155;

    public static void main(String[] args) {
        System.out.println("Sending user control code.");
        try {
            WrapperManager.sendServiceControlCode(svc, ctrl);
            WrapperManager.stop(0);
        } catch (Exception re) {
            System.err.println("System error. Unable to send control code to service, " + svc + ", with control code, " + ctrl + ".");
            re.printStackTrace();
            WrapperManager.stop(1);
        }
    }
}

这是将在您的服务中使用的侦听器。它作为系统运行:

登录监听器.java

import org.tanukisoftware.wrapper.event.WrapperEvent;
import org.tanukisoftware.wrapper.event.WrapperEventListener;
import org.tanukisoftware.wrapper.event.WrapperServiceControlEvent;


public class LoginListener implements WrapperEventListener
{
    public LoginListener() { }

    public void fired( WrapperEvent event ) {
        if (event instanceof WrapperServiceControlEvent) {
            WrapperServiceControlEvent scEvent = (WrapperServiceControlEvent) event;
            switch (scEvent.getServiceControlCode()) {
                case 155:
                    // LoginServiceDefer has sent a control code.
                    break;
            }
        }
    }
}

在 WrapperManager.start() 之前添加这一行

WrapperManager.addWrapperEventListener(new LoginListener(), WrapperEventListener.EVENT_FLAG_SERVICE);

将这两行添加到您的 wrapper.conf

wrapper.java.additional.1=-Djava.security.manager
wrapper.java.additional.2=-Djava.security.policy=java.policy

创建一个名为 java.policy 的新文件。

将其放在 Wrapper.exe 所在的文件夹中(应该是 wrapper-windows/bin/wrapper.exe)。

// NOTE: There are ways of limiting the permissions for the notifier using the the library.
grant codeBase "file:../lib/wrapper.jar" {
        permission java.security.AllPermission;
};

// Change *my_login_notifier.jar* to whatever LoginServiceDefer.java is as a jar.
grant codeBase "file:./*my_login_notifier.jar*" {
        permission java.security.AllPermission;
};

在用户登录时运行此命令,确保 cd 到路径或编辑 java.policy 文件:

java -Djava.security.manager -Djava.security.policy=java.policy -jar *my_login_notifier.jar*.jar
于 2015-11-28T04:10:30.977 回答
0

还有另一种名为JavaExe的解决方案。使用此工具,您可以将一些系统事件接收到您的 Java 应用程序中,例如用户连接或断开连接,...

于 2015-11-28T17:32:45.883 回答