vfs2 and samba(smb) version 2

偶尔善良 提交于 2019-12-24 07:34:48

问题


we disabled smbv1 in our windows machine and now we can't connect using smb2 we are using commons-vfs2(2.0) and commons-vfs2-sandbox(2.0) here is my code:

@Test
    public void testConnection() throws FileSystemException {
         String folder = "\\\\10.0.0.0\\smb";

        folder = folder.replaceAll("\\\\", "/");
        StringBuilder builder = new StringBuilder(128).append(Protocol.CIFS.getProtocolPrefix()).append(':')
                .append(folder);
        String fileURI = builder.toString();
        System.out.println(fileURI);

        FileSystemOptions fsOptions = null;

        String password = "mAdmin";
        String username = "myPassword";
        String domain = "";
        StaticUserAuthenticator auth = new StaticUserAuthenticator(domain, username, password);
        fsOptions = new FileSystemOptions();
        DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(fsOptions, auth);
        FileSystemManager manager = VFS.getManager();

        FileSystemManager fileSystemManager = manager;
        FileObject fileObject = fileSystemManager.resolveFile(fileURI, fsOptions);
        boolean result = fileObject.isReadable();

        System.out.println(fileURI +" " + result);
    }

this is how I disabled smb v1

Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters" SMB1 -Type DWORD -Value 0 -Force

How I enabled SMBV2

Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters" SMB2 -Type DWORD -Value 1 -Force

https://support.microsoft.com/en-us/help/2696547/how-to-enable-and-disable-smbv1-smbv2-and-smbv3-in-windows-and-windows-server

in case we are enablying SMB1 it's working

[TRACE]

org.apache.commons.vfs2.FileSystemException: Could not determine if file "smb://10.0.0.0/smb/" is readable.
    at org.apache.commons.vfs2.provider.AbstractFileObject.isReadable(AbstractFileObject.java:1761)
    at com.pa.util.files.FileUtilsTest.testConnection(FileUtilsTest.java:109)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.apache.commons.vfs2.FileSystemException: Could not determine the type of file "smb://10.0.0.0/smb/".
    at org.apache.commons.vfs2.provider.AbstractFileObject.getType(AbstractFileObject.java:1526)
    at org.apache.commons.vfs2.provider.AbstractFileObject.exists(AbstractFileObject.java:1022)
    at org.apache.commons.vfs2.provider.AbstractFileObject.isReadable(AbstractFileObject.java:1757)
    ... 26 more
Caused by: jcifs.smb.SmbException: Failed to connect: 0.0.0.0<00>/10.0.0.0
jcifs.util.transport.TransportException
java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(SocketInputStream.java:210)
    at java.net.SocketInputStream.read(SocketInputStream.java:141)
    at jcifs.util.transport.Transport.readn(Transport.java:29)
    at jcifs.smb.SmbTransport.peekKey(SmbTransport.java:388)
    at jcifs.smb.SmbTransport.negotiate(SmbTransport.java:288)
    at jcifs.smb.SmbTransport.doConnect(SmbTransport.java:319)
    at jcifs.util.transport.Transport.run(Transport.java:241)
    at java.lang.Thread.run(Thread.java:745)

    at jcifs.util.transport.Transport.run(Transport.java:258)
    at java.lang.Thread.run(Thread.java:745)

    at jcifs.smb.SmbTransport.connect(SmbTransport.java:309)
    at jcifs.smb.SmbTree.treeConnect(SmbTree.java:156)
    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.exists(SmbFile.java:1415)
    at org.apache.commons.vfs2.provider.smb.SmbFileObject.doGetType(SmbFileObject.java:133)
    at org.apache.commons.vfs2.provider.AbstractFileObject.getType(AbstractFileObject.java:1517)
    ... 28 more

[pom.xml]

       <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-vfs2</artifactId>
            <version>2.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-vfs2-sandbox</artifactId>
            <version>2.0</version>
        </dependency>

Please Help


回答1:


Unfortunately commons-vfs2 does not support the SMBv2 protocol. The only open source implementation currently of an SMBv2 client is SMBJ. Using SMBJ you could connect as follows to your share:

SMBClient client = new SMBClient(SmbConfig.createDefaultConfig());
Connection c = client.connect("10.0.0.0");
Session s = c.authenticate(new AuthenticationContext("myAdmin", "myPassword", "");
DiskShare share = s.connectShare("smb");


来源:https://stackoverflow.com/questions/44098675/vfs2-and-sambasmb-version-2

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