95

Access Git repositories with Java using SSH keys | Java Code Geeks - 2017

 6 years ago
source link: https://www.javacodegeeks.com/2017/12/access-git-repositories-java-using-ssh-keys.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Home » Software Development » Access Git repositories with Java using SSH keys

About Sebastian Daschner

b123da61671c52779a97386028a36552.jpg?ver=1664278061
Sebastian Daschner is a self-employed Java consultant and trainer. He is the author of the book 'Architecting Modern Java EE Applications'. Sebastian is a Java Champion, Oracle Developer Champion and JavaOne Rockstar.

Access Git repositories with Java using SSH keys

Posted by: Sebastian Daschner in Software Development December 4th, 2017 0 120 Views

For some use-cases you might access a git repository from a Java application. JGit offers a helpful integration with builder pattern APIs. The Git client can authenticate itself using SSH keys.

To open a Git repository call the cloneRepository() command.

File workingDir = Files.createTempDirectory("workspace").toFile();
TransportConfigCallback transportConfigCallback = new SshTransportConfigCallback();
git = Git.cloneRepository()
.setDirectory(workingDir)
.setTransportConfigCallback(transportConfigCallback)
.setURI("ssh://example.com/repo.git")
.call();

Our own implementation of the transport config callback configures the SSH communication for accessing the repository. We want to tell JGit to use SSH communication and to ignore the host key checking.

private static class SshTransportConfigCallback implements TransportConfigCallback {
private final SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() {
@Override
protected void configure(OpenSshConfig.Host hc, Session session) {
session.setConfig("StrictHostKeyChecking", "no");
}
};
@Override
public void configure(Transport transport) {
SshTransport sshTransport = (SshTransport) transport;
sshTransport.setSshSessionFactory(sshSessionFactory);
}
}

Per default this will take the id_rsa key file, available under the ~/.ssh. If you want to specify another file location or configure a key secured by a passphrase, change the creation of the Java Secure Channel as well.

private static class SshTransportConfigCallback implements TransportConfigCallback {
private final SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() {
@Override
protected void configure(OpenSshConfig.Host hc, Session session) {
session.setConfig("StrictHostKeyChecking", "no");
}
@Override
protected JSch createDefaultJSch(FS fs) throws JSchException {
JSch jSch = super.createDefaultJSch(fs);
jSch.addIdentity("/path/to/key", "super-secret-passphrase".getBytes());
return jSch;
}
};
@Override
public void configure(Transport transport) {
SshTransport sshTransport = (SshTransport) transport;
sshTransport.setSshSessionFactory(sshSessionFactory);
}
}

Now you can issue commands on your Git handle. For a full example on how to access Git repositories, see my AsciiBlog application.

Published on Java Code Geeks with permission by Sebastian Daschner, partner at our JCG program. See the original article here: Access Git repositories with Java using SSH keys

Opinions expressed by Java Code Geeks contributors are their own.

Tagged with: Git

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

and many more ....

Email address:

Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions

Leave this field empty if you're human:

Like This Article? Read More From Java Code Geeks

Subscribe
guest
Label

Sign me up for the newsletter!

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments

Join Us

Join Us
With 1,240,600 monthly unique visitors and over 500 authors we are placed among the top Java related sites around. Constantly being on the lookout for partners; we encourage you to join us. So If you have a blog with unique and interesting content then you should check out our JCG partners program. You can also be a guest writer for Java Code Geeks and hone your writing skills!

Newsletter

119,968 insiders are already enjoying weekly updates and complimentary whitepapers!

Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

Email address:

Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions

Leave this field empty if you're human:

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK