问题
we have a scenario in which we are sharing a folder with group Everyone (which I think includes all users from your domain)
when we are using \\server-ip\sharedFolder in Win+R we are able to connect to the folder and browse with out any password (my computer is logged in using a user on same domain)
when I code it in java application using File class like this
File f = new File(\\server-ip\sharedFolder );
it works then also, But I want to make my application work on mac/linux also so I am trying to use JCIFS for this purpose. But I am having issues while authenticating what should I use as username and password when guest account is also out of picture. I tried
package test;
import java.net.MalformedURLException;
import jcifs.smb.NtlmAuthenticator;
import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbException;
import jcifs.smb.SmbFile;
public class JCIFSTest {
public static void main(String args[]){
String user = "";
String pass ="";
String sharedFolder="sup";
String path="smb://server-ip/"+sharedFolder+"/";
//ntlm
// NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("",user, pass);
try {
SmbFile smbFile = new SmbFile(path,NtlmPasswordAuthentication.ANONYMOUS);
if(smbFile.isDirectory()){
for(SmbFile f: smbFile.listFiles()){
System.out.println(f.getName());
if(f.isDirectory()){
for(SmbFile g: f.listFiles()){
System.out.println(g.getName());
}
}
}
}
//SmbFile.
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SmbException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
but this is also giving error "Account Disabled" I guess its trying to user guest which is disabled.
since when connecting with windows using simple \\server-ip\sharedFolder it works, I think there will be a way to do this. I want to figure out how windows is authencating when we are doing this???
EDIT: Found the way to do it. You need not be added to the list of users who are allowed to login. If you are in the Everyone List and the folder is shared with Everyone you need to give your username and password during NTLM authentication and the folders that are shared with everyone can be accessed after that
回答1:
However this late reply, but as Matheiu pointed out I should probably write an answer here to help others.
There was no code change required. It was more of a conceptual thing.
Actually when you share your file with everyone it's actually shared with everyone on your DOMAIN or guest if the guest account is enabled.
You need some way to authenticate when someone is trying to access it. When you access it like this \server-ip\sharedFolder using windows it authenticates itself using your logged-in user and if you are part of the same domain it gives you access.
You need to do the same when you are connecting via JCIFS using linux/mac. You need to give your username and password and if your user is part of that domain you will be authenticated and you can access things that are shared publically or shared with everyone.
来源:https://stackoverflow.com/questions/22717765/jcifs-how-to-authenciate-when-folder-is-shared-with-everyone