I have a servers file on my computer with http-auth-types = Negotiate;ntlm;basic
The following Java program with svnkit 1.10.2 should connect to a repository :
public static void main(String[] args) throws SVNException
{
SVNClientManager clientManager = SVNClientManager.newInstance();
ISVNAuthenticationManager manager = SVNWCUtil.createDefaultAuthenticationManager();
clientManager.setAuthenticationManager(manager);
SVNURL repositoryURL = SVNURL.parseURIEncoded(args[0]);
SVNRepository repository = clientManager.createRepository(repositoryURL, true);
Collection<SVNDirEntry> contentOfFolder = repository.getDir("", SVNRevision.HEAD.getNumber(),
null, SVNDirEntry.DIRENT_ALL, (Collection<SVNDirEntry>) null);
System.out.println(contentOfFolder);
}
Unfortunately, I get
org.tmatesoft.svn.core.SVNAuthenticationException: svn: E170001: Authentication required for ...
What am I doing wrong? Do I somehow need to tell svnkit to use ntlm?
Related
I am currently working on a project that has a built-in library loader for maven dependencies.
This is my current code:
public class ResolverTest {
private static final File LOCAL_REPO_DIR = new File("localRepo");
public static void main(String[] args) throws MalformedURLException, DependencyResolutionException {
downloadLibraries(
Arrays.asList(
"https://repo1.maven.org/maven2/",
"https://repository.apache.org/content/repositories/snapshots/"
),
List.of("commons-io:commons-io:2.12.0-SNAPSHOT")
);
}
public static List<URL> downloadLibraries(List<String> repositories, List<String> libraries) throws DependencyResolutionException, MalformedURLException {
// setup connection
var locator = MavenRepositorySystemUtils.newServiceLocator();
locator.addService(RepositoryConnectorFactory.class, BasicRepositoryConnectorFactory.class);
locator.addService(TransporterFactory.class, HttpTransporterFactory.class);
var repoSystem = locator.getService(RepositorySystem.class);
var session = MavenRepositorySystemUtils.newSession();
session.setChecksumPolicy(RepositoryPolicy.CHECKSUM_POLICY_FAIL);
session.setUpdatePolicy(RepositoryPolicy.UPDATE_POLICY_NEVER);
session.setLocalRepositoryManager(repoSystem.newLocalRepositoryManager(session, new LocalRepository(LOCAL_REPO_DIR)));
session.setTransferListener(new LoggingTransferListener());
session.setReadOnly();
List<RemoteRepository> remoteRepos = new ArrayList<>();
var repoIdx = 0;
for (var repoUrl : repositories) {
remoteRepos.add(new RemoteRepository.Builder(String.valueOf(repoIdx), "default", repoUrl).build());
repoIdx++;
}
remoteRepos = repoSystem.newResolutionRepositories(session, remoteRepos);
// download libraries
var request = new DependencyRequest(
new CollectRequest(
(Dependency) null,
libraries.stream().map(library ->
new Dependency(new DefaultArtifact(library), null)
).toList(),
remoteRepos
),
null
);
var results = repoSystem.resolveDependencies(session, request).getArtifactResults();
var urls = new ArrayList<URL>();
for (var result : results) {
urls.add(result.getArtifact().getFile().toURI().toURL());
}
return urls;
}
private static class LoggingTransferListener extends AbstractTransferListener {
#Override
public void transferInitiated(TransferEvent event) {
System.out.println("Init transfer " + event.getResource().getRepositoryUrl() + event.getResource().getResourceName());
}
}
}
implementation("org.apache.maven:maven-resolver-provider:3.8.6")
implementation("org.apache.maven.resolver:maven-resolver-connector-basic:1.7.3")
implementation("org.apache.maven.resolver:maven-resolver-transport-http:1.8.2")
The issue I'm experiencing is that every time the program is restarted and the library loader is called again, the resolver tries to download the maven-metadata.xml file of the dependency from all repositories that didn't have it in the previous attempt.
This only seems to happen with snapshot dependencies.
In the example above I'm also using RepositoryPolicy.UPDATE_POLICY_NEVER. If it is set to RepositoryPolicy.UPDATE_POLICY_ALWAYS, the resolver pulls the metadata from all repositories (like it should).
So why is it still pulling from the other repos, even if UPDATE_POLICY_NEVER is set?
Setting the session to offline mode (session.setOffline(true);) also doesn't work: The maven resolver throws a DependencyResolutionException: org.eclipse.aether.resolution.DependencyResolutionException: Cannot access central (https://repo.maven.apache.org/maven2) in offline mode and the artifact (path to artifact) has not been downloaded from it before. (The dependency is in my local repo, but wasn't downloaded from central)
Well...
// this one prevents maven from downloading metadata.xml
// if I understand properly it tries to find maven-metadata-0.xml
// in local repository and in case of failure tries remote repositories
session.setResolutionErrorPolicy(new SimpleResolutionErrorPolicy(true, false));
// this one prevents maven from creating _remote.repositories files
// in local repository, yours: in my local repo, but wasn't downloaded from central
session.setLocalRepositoryManager(repoSystem.newLocalRepositoryManager(session, new LocalRepository(LOCAL_REPO_DIR, "simple")));
I need to connect to a shared folder with proper user credentials (username, password, domain).
Then when I have access to the folder, I need to list the subfolders and files in it.
Im trying with the jcifs.smb.SmbFile class and jcifs.smb.NtlmPasswordAuthentication for the authentication.
My code is the following:
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("domainName", "userName", "password");
SmbFile smbFile = new SmbFile("smb://servername/someFolder", auth);
for (String fileName : smbFile.list()) {
System.out.println(fileName);
}
I would be able to connect to the server with these credentials, but I'm getting this error:
Exception in thread "main" jcifs.smb.SmbException: Failed to connect: servername/IP_ADDR
jcifs.util.transport.TransportException
java.net.SocketException: Connection reset
at java.base/sun.nio.ch.NioSocketImpl.implRead(NioSocketImpl.java:323)
at java.base/sun.nio.ch.NioSocketImpl.read(NioSocketImpl.java:350)
at java.base/sun.nio.ch.NioSocketImpl$1.read(NioSocketImpl.java:803)
at java.base/java.net.Socket$SocketInputStream.read(Socket.java:981)
...
Anyone has any idea why am I unable to connect?
SmbFile - https://www.jcifs.org/src/docs/api/jcifs/smb/SmbFile.html
NtlmPasswordAuthentication - https://javadoc.io/static/eu.agno3.jcifs/jcifs-ng/2.1.3/jcifs/smb/NtlmPasswordAuthentication.html
I found the solution!
Because of my OS (Windows 10) I needed to use SMB2 not SMB1 (this is the default).
Solution:
Open powershell as an administrator
You need to set a property: Set -SmbServerConfiguration -EnableSMB2Protocol $true
Optional: I think it isn't necessarry but I turned off the SMB1 protocol wit the
Set -SmbServerConfiguration -EnableSMB1Protocol $false command.
Then you can check the properties with: Get -SmbServerConfiguration command, and be sure about that all of the properties have the right value.
Import the proper dependency to the pom.xml:
<dependency>
<groupId>eu.agno3.jcifs</groupId>
<artifactId>jcifs-ng</artifactId>
<version>2.1.6</version>
</dependency>
https://github.com/AgNO3/jcifs-ng
Finally the code:
public static void sendRequest() throws Exception {
CIFSContext base = SingletonContext.getInstance();
CIFSContext authed1 = base.withCredentials(new NtlmPasswordAuthentication(base, "domainName",
"userName", "password"));
try (SmbFile f = new SmbFile("smb:\\serverName\folder", authed1)) {
if (f.exists()) {
for (SmbFile file : f.listFiles()) {
System.out.println(file.getName());
}
}
}
}
Want to download torrents via bt-cli. I tryed to launch bt-cli as module of bt parent and launch bt-cli-demo as project, but I aslways get java.nio.file.AccessDeniedException to temp files.
I tryed through Intellij Idea and through shell, all time the same problem.
Also tryed to dowload with -f and with -m keys (link to the project and keys description https://github.com/atomashpolskiy/bt)
I always get the same exception:
exception screen
Maybe I have to change folder for temp files, but I don't know to do it
Code:
public static void main(String[] args) throws IOException {
Options options;
try {
options = Options.parse(args);
} catch (OptionException e) {
Options.printHelp(System.out);
return;
}
configureLogging(options.getLogLevel());
configureSecurity();
registerLog4jShutdownHook();
CliClient client = new CliClient(options);
client.start();
}
all params I get from shell, api parses it and create configureation according income params. Then it starts.
Params for shell:
static {
parser = new OptionParser() {
{
acceptsAll(Arrays.asList("?", "h", "help")).isForHelp();
}
};
metainfoFileOptionSpec = parser.acceptsAll(Arrays.asList("f", "file"), "Torrent metainfo file")
.withRequiredArg().ofType(File.class);
magnetUriOptionSpec = parser.acceptsAll(Arrays.asList("m", "magnet"), "Magnet URI")
.withRequiredArg().ofType(String.class);
targetDirectoryOptionSpec = parser.acceptsAll(Arrays.asList("d", "dir"), "Target download location")
.withRequiredArg().ofType(File.class)
.required();
shouldSeedOptionSpec = parser.acceptsAll(Arrays.asList("s", "seed"), "Continue to seed when download is complete");
sequentialOptionSpec = parser.acceptsAll(Arrays.asList("S", "sequential"), "Download sequentially");
enforceEncryptionOptionSpec = parser.acceptsAll(Arrays.asList("e", "encrypted"), "Enforce encryption for all connections");
verboseOptionSpec = parser.acceptsAll(Arrays.asList("v", "verbose"), "Enable more verbose logging");
traceOptionSpec = parser.accepts("trace", "Enable trace logging");
inetAddressOptionSpec = parser.acceptsAll(Arrays.asList("i", "inetaddr"), "Use specific network address (possible values include IP address literal or hostname)")
.withRequiredArg().ofType(String.class);
torrentPortOptionSpec = parser.acceptsAll(Arrays.asList("p", "port"), "Listen on specific port for incoming connections")
.withRequiredArg().ofType(Integer.class);
dhtPortOptionSpec = parser.accepts("dhtport", "Listen on specific port for DHT messages")
.withRequiredArg().ofType(Integer.class);
shouldDownloadAllFiles = parser.acceptsAll(Arrays.asList("a", "all"), "Download all files (file selection will be disabled)");
}
/**
* #throws OptionException
*/
public static Options parse(String... args) {
OptionSet opts = parser.parse(args);
return new Options(
opts.valueOf(metainfoFileOptionSpec),
opts.valueOf(magnetUriOptionSpec),
opts.valueOf(targetDirectoryOptionSpec),
opts.has(shouldSeedOptionSpec),
opts.has(sequentialOptionSpec),
opts.has(enforceEncryptionOptionSpec),
opts.has(verboseOptionSpec),
opts.has(traceOptionSpec),
opts.valueOf(inetAddressOptionSpec),
opts.valueOf(torrentPortOptionSpec),
opts.valueOf(dhtPortOptionSpec),
opts.has(shouldDownloadAllFiles));
}
AccessDeniedException can mean lots of things. The documentation says: "a file system operation is denied, typically due to a file permission or other access check", and I would pay attention to the word "other".
I am trying to run the following Java code using sshj :-
public static void main(String[] args) throws IOException {
SSHClient ssh = new SSHClient();
ssh.loadKnownHosts();
ssh.connect("host", port);
try {
ssh.authPassword("user", "passwd");
ssh.useCompression();
final String src = System.getProperty("user.home") + File.separator + "test_file";
ssh.newSCPFileTransfer().upload(new FileSystemFile(src), "/tmp/");
} finally {
ssh.disconnect();
ssh.close();
}
}
But it is throwing exception -
Exception in thread "main" net.schmizz.sshj.userauth.UserAuthException: Exhausted available authentication methods
at net.schmizz.sshj.SSHClient.auth(SSHClient.java:231)
at net.schmizz.sshj.SSHClient.auth(SSHClient.java:206)
at net.schmizz.sshj.SSHClient.authPassword(SSHClient.java:292)
at net.schmizz.sshj.SSHClient.authPassword(SSHClient.java:262)
at net.schmizz.sshj.SSHClient.authPassword(SSHClient.java:246)
at sample.SCPUpload.main(SCPUpload.java:17)
I can connect the host using same credentials via Putty. I am using JDK "1.8.0_151". What is wrong here?
Typically it means that either your password is wrong, or you're not allowed to connect using the 'password' authentication method.
I am a bit new here but I need some help with my code. Here it is:
SVNRepositoryFactoryImpl.setup();
SVNURL url =
SVNURL.
parseURIEncoded("svn+ssh://xxx");
File keyFile =
new File("C://Users/xxxx/Documents/priave_key/private_key.ppk");
System.out.println("PPK file space:"+keyFile.getTotalSpace());
SVNSSHAuthentication sshCredentials =
new SVNSSHAuthentication(Settings.name, keyFile, Settings.pass, 22, false, url, false);
System.out.println(sshCredentials.getPrivateKey()==null);
System.out.println("hasPrivateKey(): "+sshCredentials.hasPrivateKey());
SVNUserNameAuthentication authorNameCredentials = new SVNUserNameAuthentication(Settings.name, false);
SVNPasswordAuthentication passwordCredentials = new SVNPasswordAuthentication(Settings.name, Settings.pass, false);
ISVNAuthenticationManager authManager =
new BasicAuthenticationManager(new SVNAuthentication[] {
sshCredentials,
authorNameCredentials,
passwordCredentials});
SVNRepository repository = SVNRepositoryFactory.create(url);
repository.setAuthenticationManager(authManager);
try {
SVNDirEntry info = repository.info("", -1);
System.out.println(info);
} finally {
repository.closeSession();
}
So after running this I get this output:
PPK file space:107374178304
false
hasPrivateKey(): true
Exception in thread "main" org.tmatesoft.svn.core.SVNAuthenticationException: svn: E170001: Authentication required for 'xxx#svn+ssh://xxx'
at org.tmatesoft.svn.core.internal.wc.SVNErrorManager.authenticationFailed(SVNErrorManager.jav a:47)
at org.tmatesoft.svn.core.internal.wc.SVNErrorManager.authenticationFailed(SVNErrorManager.jav a:41)
at
org.tmatesoft.svn.core.auth.BasicAuthenticationManager.getNextAuthentication(BasicAuthentic ationManager.java:223)
at org.tmatesoft.svn.core.internal.io.svn.SVNSSHConnector.open(SVNSSHConnector.java:141)
at org.tmatesoft.svn.core.internal.io.svn.SVNConnection.open(SVNConnection.java:77)
at org.tmatesoft.svn.core.internal.io.svn.SVNRepositoryImpl.openConnection(SVNRepositoryImpl.j ava:1252)
at org.tmatesoft.svn.core.internal.io.svn.SVNRepositoryImpl.info(SVNRepositoryImpl.java:1195)
at SVNTestNew.main(SVNTestNew.java:40)
I think this can be a problem with SVNSSHAuthentication class because it gives null for getPrivateKey() and true for hasPrivateKey(.) Or is this a wrong idea? Can anyone tell me how can this problem be solved?
According to me your public and private keys are not at proper place.
If you are using Linux machine then,
For private key you have default path :-
${USER_HOME}/.ssh/abc.rsa or abc.dsa
And you have to Store your public key at server in :
Default Path :-
${USER_HOME}/.ssh/authorized_keys
You can change your Path in /etc/sshd_config file