java jCIFS似乎无法进行身份验证

myss37ts  于 2023-02-11  发布在  Java
关注(0)|答案(1)|浏览(136)

我开始对使用jCIFS而不是要求用户自己手动Map驱动器会损失多少性能进行基准测试,但后来我发现我甚至无法通过使用jCIFS从文件阅读数据的第一个基本测试。
我把我的问题归结为一道测试题:

import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import com.google.common.io.ByteStreams;
import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbFile;

public class TestCifs
{
    public static void main(String[] args) throws Exception
    {
        OutputStream output = ByteStreams.nullOutputStream();

        String domain = ""; // have tried the actual name too
        String user = "<your_user_here>";
        String pass = "<your_pass_here>";

        String relativePath = "path/to/100MiB";

        // Read via UNC path (Windows doing the SMB work.)
        Path uncFile = Paths.get("\\\\192.168.1.66\\Shared", relativePath);
        Files.copy(uncFile, output);

        // Read via jCIFS
        //TODO: Figure out if this is a real URL in which case we'd have to escape stuff.
        SmbFile smbFile = new SmbFile("smb://192.168.1.66/Shared/" + relativePath,
                                      new NtlmPasswordAuthentication(domain, user, pass));
        ByteStreams.copy(smbFile.getInputStream(), output);
    }
}

如果我运行此命令,它将超过直接拷贝,然后jCIFS拷贝将失败,并显示:

Exception in thread "main" jcifs.smb.SmbAuthException: Logon failure: unknown user name or bad password.
    at jcifs.smb.SmbTransport.checkStatus(SmbTransport.java:546)
    at jcifs.smb.SmbTransport.send(SmbTransport.java:663)
    at jcifs.smb.SmbSession.sessionSetup(SmbSession.java:390)
    at jcifs.smb.SmbSession.send(SmbSession.java:218)
    at jcifs.smb.SmbTree.treeConnect(SmbTree.java:176)
    at jcifs.smb.SmbFile.doConnect(SmbFile.java:911)
    at jcifs.smb.SmbFile.connect(SmbFile.java:954)
    at jcifs.smb.SmbFile.connect0(SmbFile.java:880)
    at jcifs.smb.SmbFile.open0(SmbFile.java:972)
    at jcifs.smb.SmbFile.open(SmbFile.java:1006)
    at jcifs.smb.SmbFileInputStream.<init>(SmbFileInputStream.java:73)
    at jcifs.smb.SmbFileInputStream.<init>(SmbFileInputStream.java:65)
    at jcifs.smb.SmbFile.getInputStream(SmbFile.java:2844)
    at TestCifs.main(TestCifs.java:33)

我在user/pass中提供的用户名和密码与我在通过Windows验证服务器以装载其目录时使用的用户名和密码相同。我还尝试使用服务器的名称作为域,但没有效果。我真的对这个问题感到困惑,并准备查看Wireshark之类的东西,以找出实际问题所在,但也许我“我在做一件明显错误的事,这里有人可以指出这一点。
这可能是jCIFS自己对失败登录的诊断中的相关位:

treeConnect: unc=\\192.168.1.66\SHARED,service=?????
sessionSetup: accountName=TESTER,primaryDomain=
NtlmContext[auth=TESTER,ntlmsspFlags=0x20080004,workstation=JCIFS1_183_42,isEstablished=false,state=1,serverChallenge=null,signingKey=null]
Type1Message[suppliedDomain=,suppliedWorkstation=JCIFS1_183_42,flags=0x20080205]
00000: 4E 54 4C 4D 53 53 50 00 01 00 00 00 05 22 08 20  |NTLMSSP......". |
00010: 00 00 00 00 00 00 00 00 0D 00 0D 00 20 00 00 00  |............ ...|
00020: 4A 43 49 46 53 31 5F 31 38 33 5F 34 32           |JCIFS1_183_42   |

SmbComSessionSetupAndX[command=SMB_COM_SESSION_SETUP_ANDX,received=false,errorCode=0,flags=0x0018,flags2=0xC803,signSeq=0,tid=0,pid=21886,uid=0,mid=2,wordCount=12,byteCount=77,andxCommand=0xFF,andxOffset=0,snd_buf_size=16644,maxMpxCount=10,VC_NUMBER=1,sessionKey=0,lmHash.length=0,ntHash.length=0,capabilities=-2147483564,accountName=null,primaryDomain=null,NATIVE_OS=Windows 8,NATIVE_LANMAN=jCIFS]
00000: FF 53 4D 42 73 00 00 00 00 18 03 C8 00 00 00 00  |ÿSMBs......È....|
00010: 00 00 00 00 00 00 00 00 00 00 7E 55 00 00 02 00  |..........~U....|
00020: 0C FF 00 DE DE 04 41 0A 00 01 00 00 00 00 00 2D  |.ÿ.ÞÞ.A........-|
00030: 00 00 00 00 00 54 00 00 80 4D 00 4E 54 4C 4D 53  |.....T...M.NTLMS|
00040: 53 50 00 01 00 00 00 05 22 08 20 00 00 00 00 00  |SP......". .....|
00050: 00 00 00 0D 00 0D 00 20 00 00 00 4A 43 49 46 53  |....... ...JCIFS|
00060: 31 5F 31 38 33 5F 34 32 57 00 69 00 6E 00 64 00  |1_183_42W.i.n.d.|
00070: 6F 00 77 00 73 00 20 00 38 00 00 00 6A 00 43 00  |o.w.s. .8...j.C.|
00080: 49 00 46 00 53 00 00 00                          |I.F.S...        |

New data read: Transport1[0.0.0.0<00>/192.168.1.66:445]
00000: FF 53 4D 42 73 6D 00 00 C0 80 41 C8 00 00 00 00  |ÿSMBsm..À.AÈ....|
00010: 00 00 00 00 00 00 00 00 00 00 7E 55 00 00 02 00  |..........~U....|

SmbComSessionSetupAndXResponse[command=SMB_COM_SESSION_SETUP_ANDX,received=false,errorCode=Logon failure: unknown user name or bad password.,flags=0x0080,flags2=0xC841,signSeq=0,tid=0,pid=21886,uid=0,mid=2,wordCount=0,byteCount=0,andxCommand=0xFF,andxOffset=0,isLoggedInAsGuest=false,nativeOs=,nativeLanMan=,primaryDomain=]
00000: FF 53 4D 42 73 6D 00 00 C0 80 41 C8 00 00 00 00  |ÿSMBsm..À.AÈ....|
00010: 00 00 00 00 00 00 00 00 00 00 7E 55 00 00 02 00  |..........~U....|
00020: 00 00 00                                         |...             |

看起来它根本没有把用户名放进邮件里,所以也许我看到的是一个bug,但是如果它真的这样做了,肯定会被其他人注意到的...
已尝试的变通方法:

  • 将身份验证信息放入URL而不是auth对象。
  • 在尝试访问SmbFile之前调用SmbSession.logon。在这种情况下,您只会从logon获得错误。
  • 使用NtlmAuthenticator.setDefault()设置默认验证器。在这种情况下,您会看到它调用验证器以获取验证详细信息,但随后似乎丢弃了这些信息,并在没有用户名的情况下发出请求。
efzxgjgh

efzxgjgh1#

我为此奋斗了一个多小时,但最终能够使用完整的URL字符串而不是NtlmPasswordAuthentication对象使其工作。我不能说为什么会出现这种情况,但可能是在将用户名和/或密码传递到连接字符串之前,它对用户名和/或密码做了一些奇怪的事情。
如果有人需要它,这里是我使用的URL格式,其中包括域:

smb://domain;username:password@server/share/path

相关问题