Send userId at java web start from web page

孤街醉人 提交于 2019-12-11 18:22:11

问题


I'm developing a JSF2 site where the users may start a java application through java web start. The app parses mp3 metadata and sends back a xml file with the parsed information.

I need some way of identifying the user for each file that is sent to the server and I have not been able to figure out how to do this.

In other words the goal is to be able to set the userId in the xml file before the user sends it to the server. In order to do that, I need to somehow have that Id available in the java web start application.

My question is: how do i get the id? Given that the ultimate goal is to parse user mp3 files and get metadata back to the server; any idea of how to do that in a better way is very welcome. Maybe my described way of doing it isn't the best.


回答1:


We want to somehow transfer the login identity from the web browser to another application (like your JNLP program) This is a bit of an unconventional hack, but I think it should work:

  1. When the user logs into the web app, store the user ID and the request IP address in a table. This could be a database table or a local file.
  2. When the java app launches, it sends a getID request to a Servlet.
  3. The servlet looks up the IP address of the request and responds with the ID. The servlet could just send the ID back - no need to wrap it in XML or HTML.

I think this should work as long as you don't have two different users trying to log in at around the same time from the same IP address. If two people on the same network are using this service, they may be using computers with private IPs that share the same public IP. We would need to store something else about the local computer to differentiate the two users. Java can easily read the computer name, for instance. If you can get your browser to also read something like that, then this could go in the table along with the IP address to solve the duplicate IP problem. Some ideas are mentioned in this stack overflow question




回答2:


A web start app acting as a client can identify itself to a server the same way that any other client can - simply have the user log in at the beginning of a session. But since a desktop client has access to the local file system, user information can be stored locally. This means that data such as a userID and configuration preferences can be stored on the local hard drive. Since we want our desktop clients to work across platforms and since the developer cannot know exactly which directory this application is launching from, we have the preferences API to provide a layer of abstraction above the local file system.

If the user will primarily be using the same computer each time they run this application, this is most convenient way to store configuration info and user preferences. Here's an example:

/**
 *
 * 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 application\n"
                + "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 desktop\n"
                + "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());
}

Of course, the above code does not help us if this is the first time the user is running the application on the computer or if the user was not assigned an ID. We therefore need to build some login functionality. The JNLP application can communicate with the web server the same way that a browser would do so. I found the httpClient libraries to be very helpful for implementing this. If the user's browser is behind a proxy, this may require some additional code to allow you java application to talk communicate back to the server.



来源:https://stackoverflow.com/questions/12877346/send-userid-at-java-web-start-from-web-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!