从网页开始在java web上发送userId



我正在开发一个JSF2网站,用户可以在该网站上通过java web启动来启动java应用程序。该应用程序解析mp3元数据,并发送回一个包含解析信息的xml文件。

我需要某种方法来识别发送到服务器的每个文件的用户,但我一直无法弄清楚如何做到这一点。

换句话说,目标是能够在用户将xml文件发送到服务器之前在该文件中设置userId。为了做到这一点,我需要在java web启动应用程序中以某种方式提供该Id。

我的问题是:如何获取身份证?假设最终目标是解析用户mp3文件并将元数据返回到服务器;任何关于如何以更好的方式做到这一点的想法都是非常受欢迎的。也许我描述的做这件事的方式不是最好的。

我们希望以某种方式将登录标识从web浏览器转移到另一个应用程序(如您的JNLP程序)这是一个有点非常规的黑客攻击,但我认为它应该有效:

  1. 当用户登录到web应用程序时,将用户ID和请求IP地址存储在表中。这可能是数据库表或本地文件
  2. 当java应用程序启动时,它会向Servlet发送一个getID请求
  3. servlet查找请求的IP地址并使用ID进行响应。servlet只需发回ID即可,无需将其封装在XML或HTML中

我认为只要没有两个不同的用户试图在同一时间从同一IP地址登录,这就应该有效。如果同一网络上的两个人正在使用此服务,则他们可能正在使用具有共享同一公共IP的专用IP的计算机。我们需要存储有关本地计算机的其他信息,以区分这两个用户。例如,Java可以很容易地读取计算机名称。如果你能让你的浏览器也读取类似的内容,那么这可以和IP地址一起放在表中,以解决重复的IP问题。在这个堆栈溢出问题中提到了一些想法

作为客户端的web启动应用程序可以像任何其他客户端一样向服务器标识自己——只需让用户在会话开始时登录即可。但是,由于桌面客户端可以访问本地文件系统,因此用户信息可以存储在本地。这意味着用户ID和配置首选项等数据可以存储在本地硬盘上。由于我们希望我们的桌面客户端跨平台工作,而且开发人员无法确切知道该应用程序是从哪个目录启动的,因此我们有API偏好,可以在本地文件系统之上提供一层抽象。

如果用户每次运行此应用程序时主要使用同一台计算机,则这是存储配置信息和用户首选项的最方便方式。这里有一个例子:

/**
*
* Loads the user preferences from local storage using the Preferences API.
* The actual location of this file is platform and computer user-login
* dependant, but from the perspective of the preferences API, it is stored in 
* a node referenced by the package this class resides in.
*/
private void loadPrefernces() {
try {
prefs = Preferences.userNodeForPackage(this.getClass());
}
catch(SecurityException stop) {
JOptionPane.showMessageDialog(null, "The security settings on this computer are preventing the applicationn"
+ "from loading and saving certain user preference data that the program needs to function.n"
+ "Please have your network administrator adjust the security settings to allow a Java desktopn"
+ "application to save user preferences. The exception the program encountered is below:n"
+ stop.toString(),
"Security Error", JOptionPane.ERROR_MESSAGE);
}
catch(Exception some) {
System.out.println("Other Preference Exception:");
System.out.println(some);
}
//extract information from the prefs object:
String username = prefs.get("user","default-newUser");
String userID = prefs.get("id", "0"); //a 0 ID means that the information was not found on the local computer.
}
//Use this method to store username and ID on the local computer.
public void saveUserPrefs() {
prefs.put("user", user.getUsername() );
prefs.put("id", "" + user.getID());
}

当然,如果这是用户第一次在计算机上运行应用程序,或者没有为用户分配ID,那么上面的代码对我们没有帮助。因此,我们需要构建一些登录功能。JNLP应用程序可以像浏览器一样与web服务器通信。我发现httpClient库对实现这一点非常有帮助。如果用户的浏览器位于代理之后,这可能需要一些额外的代码来允许java应用程序与服务器进行通信。

最新更新