我使用这两个库取得了巨大的成功。我能够向提供 SMB 共享的 Synology NAS 进行身份验证。
请注意,我使用的版本不必是最新版本。
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-vfs2</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>eu.agno3.jcifs</groupId>
<artifactId>jcifs-ng</artifactId>
<version>2.1.5</version>
</dependency>
用于身份验证的代码可能如下所示:
final String smburl = ...;
final String username = ...;
final String password = ...;
final URI uri = new URI(smburl);
StaticUserAuthenticator auth = new StaticUserAuthenticator(null, username, password);
Properties jcifsProperties = new Properties();
// these settings are needed for 2.0.x to use anything but SMB1, 2.1.x enables by default and will ignore
jcifsProperties.setProperty("jcifs.smb.client.enableSMB2", "true");
jcifsProperties.setProperty("jcifs.smb.client.useSMB2Negotiation", "true");
CIFSContext jcifsContext = new BaseContext(new PropertyConfiguration(jcifsProperties));
FileSystemOptions options = new FileSystemOptions();
DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(options, auth);
SmbFileSystemConfigBuilder.getInstance().setCIFSContext(options, jcifsContext);
fsManager = VFS.getManager();
最后,使用 fsManager 将 URI 解析为文件对象并执行必要的操作。