使用 JAVA 或 JSP 获取 Windows 用户名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16167646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 22:05:45  来源:igfitidea点击:

Get windows username using JAVA or JSP

javajsp

提问by Danny

I want to get logged username on windows , I mean the user logged in to windows using java. I used System.getProperty("user.name");but I got the server-name, not the user.

我想在 windows 上登录用户名,我的意思是用户使用 java 登录到 windows。我使用过, System.getProperty("user.name");但我得到了服务器名称,而不是用户。

回答by NilsH

Getting the name of the user logged into your web application (which I think is what you want to do, based on your comments) entirely depends on how you have implemented authentication in your system. However, if your login mechanism adds this information, the username might be available in request.getRemoteUser()or request.getUserPrincipal(). But as mentioned, it all depends. Your authentication mechanism might expose this otherwise, but then we would need more information to assist.

获取登录到您的 Web 应用程序的用户的名称(根据您的评论,我认为这是您想要做的)完全取决于您在系统中实施身份验证的方式。但是,如果您的登录机制添加了此信息,则用户名可能在request.getRemoteUser()或 中可用request.getUserPrincipal()。但如前所述,这一切都取决于。否则,您的身份验证机制可能会暴露这一点,但是我们需要更多信息来提供帮助。

System.getProperty("user.name")will only give you the username of the user running the java process.

System.getProperty("user.name")只会给你运行 java 进程的用户的用户名。