如何使用 Java/JSP 或 javascript 通过互联网浏览器获取 Windows 用户名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7241146/
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
How to get Windows Username through internet browser using Java/JSP or javascript?
提问by Adrien Be
I want to get the Windowsusername of the user browsing my web page(this is for an intranet). This must be working on IE8, Chrome, and Firefox(Safari would be a plus).
我想获取浏览我的网页的用户的Windows用户名(这是用于内部网)。这必须适用于IE8、Chrome和Firefox(Safari 将是一个加分项)。
I came across this solution for Java:
我遇到了这个Java解决方案:
http://www.ioplex.com/: Jespa - Java Active Directory Integration
http://www.ioplex.com/:Jespa- Java Active Directory 集成
But this is a proprietarysoftware library and even the example they provide does not work on my web application because we are not using an Apache web server.
但这是一个专有软件库,即使他们提供的示例也不适用于我的 Web 应用程序,因为我们没有使用 Apache Web 服务器。
A solution in Java would be ideal if anyone got something?
如果有人得到一些东西,Java 中的解决方案将是理想的?
There seems to be some kind of solution in javascript: How to get the windows user name using javascript in google chrome browser for google chrome extension
javascript 中似乎有某种解决方案: How to get the windows user name using javascript in google chrome browser for google chrome extension
But nothing is said about IE8 and the Chrome solution seems quite a bit of work.
但是关于 IE8 什么也没说,Chrome 解决方案似乎有很多工作。
Thanks in advance
提前致谢
回答by Raman B
you can use below three files to get the windows user name into JSP session.
您可以使用以下三个文件将 Windows 用户名导入 JSP 会话。
1.user1.html
1.user1.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
<script>
var myWindow;
function openWin() {
myWindow =window.open("http://cmtech9:8080/WinUser/GetUser.jsp","Login","height=50,width=150");
setTimeout(function(){ myWindow.close() }, 3000);
}
</script>
<body onload="openWin();">
</body>
</html>
2.user.js this openes the tab and auto closes the tab
2.user.js 这将打开选项卡并自动关闭选项卡
function testing() {
window.setTimeOut("window.close();",1000)
}
3.GetUser.jsp
3.GetUser.jsp
<%@ page import="sun.misc.BASE64Encoder" %>
<%@ page import="java.util.regex.Matcher"%>
<%@ page import="java.util.regex.Pattern"%>
<head>
<script type="text/javascript" src="user.js"></script>
<script type="text/javascript">
testing()
</script>
</head>
<p><h4>Network Windows USERNAME without any login (ie)</h4></p>
<body >
<%
HttpSession sess = request.getSession();
String auth = request.getHeader("Authorization");
if (auth == null) {
response.setStatus(response.SC_UNAUTHORIZED);
response.setHeader("WWW-Authenticate", "NTLM");
return;
}
if (auth.startsWith("NTLM ")) {
byte[] msg =
new sun.misc.BASE64Decoder().decodeBuffer(auth.substring(5));
int off = 0, length, offset;
String s;
if (msg[8] == 1) {
off = 18;
byte z = 0;
byte[] msg1 =
{(byte)'N', (byte)'T', (byte)'L', (byte)'M', (byte)'S',
(byte)'S', (byte)'P', z,
(byte)2, z, z, z, z, z, z, z,
(byte)40, z, z, z, (byte)1, (byte)130, z, z,
z, (byte)2, (byte)2, (byte)2, z, z, z, z, //
z, z, z, z, z, z, z, z};
//
response.setStatus(response.SC_UNAUTHORIZED);
response.setHeader("WWW-Authenticate", "NTLM "
+ new sun.misc.BASE64Encoder().encodeBuffer(msg1).trim());
return;
}
else if (msg[8] == 3) {
off = 30;
length = msg[off+17]*256 + msg[off+16];
offset = msg[off+19]*256 + msg[off+18];
s = new String(msg, offset, length);
//out.println(s + " ");
}
else
return;
length = msg[off+1]*256 + msg[off];
offset = msg[off+3]*256 + msg[off+2];
s = new String(msg, offset, length);
//out.println(s + " ");
length = msg[off+9]*256 + msg[off+8];
offset = msg[off+11]*256 + msg[off+10];
s = new String(msg, offset, length);
sess.setAttribute("username", s);
out.println("Hello <span style='position:relative; width:190;"
+ " height:10;filter:glow(Color=#009966,Strength=1)'>");
out.println(s + "</SPAN>");
String result=s.replaceAll("\W", "");
System.out.println(result);//+""+result.length());
/* System.out.print(n.length()); */
}
%>
</body>
回答by mrBorna
nono... That's Firefox. Firefox gives you a ridiculous amount of control over the browser and even outside the browser. You will not be able to do that in chrome because it is sandboxed. Google chrome does not provide API for accessing anything outside the browser.
nono... 那是Firefox。Firefox 为您提供了对浏览器甚至浏览器外部的大量控制。您将无法在 chrome 中执行此操作,因为它已被沙盒化。谷歌浏览器不提供用于访问浏览器外任何内容的 API。
you CAN make an NPAPI plugin, but that's about it. When the NPAPI plugin runs it asks the user for unrestricted access from the plugin which is kind of suspicious for most.
你可以制作一个 NPAPI 插件,但仅此而已。当 NPAPI 插件运行时,它会要求用户不受限制地访问插件,这对大多数人来说是一种可疑的做法。
回答by Alex Ward
Here's my answer, which is an updated version of Raman B's.
这是我的答案,它是拉曼 B 的更新版本。
I've replaced sun.misc.BASE64Encoder with the more modern java.util.Base64 class.
我用更现代的 java.util.Base64 类替换了 sun.misc.BASE64Encoder。
<%@ page import="java.util.Base64" %>
<%@ page import="java.util.regex.Matcher"%>
<%@ page import="java.util.regex.Pattern"%>
<%
HttpSession sess = request.getSession();
String auth = request.getHeader("Authorization");
String result = "";
Base64.Encoder mimeEncoder = Base64.getMimeEncoder();
Base64.Decoder mimeDecoder = Base64.getMimeDecoder();
if (auth == null) {
response.setStatus(response.SC_UNAUTHORIZED);
response.setHeader("WWW-Authenticate", "NTLM");
return;
}
if (auth.startsWith("NTLM ")) {
byte[] msg = mimeDecoder.decode(auth.substring(5));
int off = 0, length, offset;
String s;
if (msg[8] == 1) {
off = 18;
byte z = 0;
byte[] msg1 =
{(byte)'N', (byte)'T', (byte)'L', (byte)'M', (byte)'S',
(byte)'S', (byte)'P', z,
(byte)2, z, z, z, z, z, z, z,
(byte)40, z, z, z, (byte)1, (byte)130, z, z,
z, (byte)2, (byte)2, (byte)2, z, z, z, z,
z, z, z, z, z, z, z, z};
response.setStatus(response.SC_UNAUTHORIZED);
response.setHeader("WWW-Authenticate", "NTLM " + mimeEncoder.encodeToString(msg1).trim());
return;
}
else if (msg[8] == 3) {
off = 30;
length = msg[off+17]*256 + msg[off+16];
offset = msg[off+19]*256 + msg[off+18];
s = new String(msg, offset, length);
}
else {
return;
}
length = msg[off+1]*256 + msg[off];
offset = msg[off+3]*256 + msg[off+2];
s = new String(msg, offset, length);
length = msg[off+9]*256 + msg[off+8];
offset = msg[off+11]*256 + msg[off+10];
s = new String(msg, offset, length);
result=s.replaceAll("\W", "");
}
%>
<p>Username: <%=result %></p>
In this test code, the username should be outputted. I've tested this with IE and Chrome on a networked device and this appears to work. Interestingly Chrome will only supply the username over SSL though - worth noting.
在这个测试代码中,应该输出用户名。我已经在联网设备上使用 IE 和 Chrome 对此进行了测试,这似乎有效。有趣的是,Chrome 只会通过 SSL 提供用户名——值得注意。
I ran this with GlassFish and didn't need any of the JavaScript - this should happily work on its own.
我用 GlassFish 运行它并且不需要任何 JavaScript - 这应该很高兴地独立工作。