В некоторых случаях вы можете получить доступ к репозиторию git из приложения Java. JGit предлагает полезную интеграцию с API шаблонов построителя. Клиент Git может аутентифицировать себя, используя ключи SSH.
Чтобы открыть Git-репозиторий, вызовите команду cloneRepository()
.
1
2
3
4
5
6
7
8
9
|
<span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >File workingDir = Files.createTempDirectory( "workspace" ).toFile();</span> File workingDir = Files.createTempDirectory ( "рабочая область" ). ToFile ();</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >TransportConfigCallback transportConfigCallback = new SshTransportConfigCallback();</span> TransportConfigCallback transportConfigCallback = new SshTransportConfigCallback ();</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >git = Git.cloneRepository()</span> git = Git.cloneRepository ()</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >.setDirectory(workingDir)</span> .setDirectory (WorkingDir)</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >.setTransportConfigCallback(transportConfigCallback)</span> .setTransportConfigCallback (transportConfigCallback)</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >.setURI( "ssh://example.com/repo.git" )</span> .setURI ( "SSH: //example.com/repo.git" )</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >.call();</span> .вызов();</span> |
Наша собственная реализация обратного вызова конфигурации транспорта настраивает связь SSH для доступа к хранилищу. Мы хотим указать JGit использовать связь по SSH и игнорировать проверку ключа хоста.
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
|
<span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > private static class SshTransportConfigCallback implements TransportConfigCallback {</span> закрытый статический класс SshTransportConfigCallback реализует TransportConfigCallback {</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > private final SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() {</span> private final SshSessionFactory sshSessionFactory = new JschConfigSessionFactory () {</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > @Override </span> @Override </span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > protected void configure(OpenSshConfig.Host hc, Session session) {</span> защищенный void configure (OpenSshConfig.Host hc, Session session) {</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >session.setConfig( "StrictHostKeyChecking" , "no" );</span> session.setConfig ( "StrictHostKeyChecking" , "no" );</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >}</span> }</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >};</span> };</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > @Override </span> @Override </span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > public void configure(Transport transport) {</span> public void configure (Транспортный транспорт) {</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >SshTransport sshTransport = (SshTransport) transport;</span> SshTransport sshTransport = (SshTransport) транспорт;</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >sshTransport.setSshSessionFactory(sshSessionFactory);</span> sshTransport.setSshSessionFactory (sshSessionFactory);</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >}</span> }</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >}</span> }</span> |
По умолчанию это будет id_rsa
ключа id_rsa
, доступный в ~/.ssh
. Если вы хотите указать другое расположение файла или настроить ключ, защищенный парольной фразой, измените также создание безопасного канала Java.
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > private static class SshTransportConfigCallback implements TransportConfigCallback {</span> закрытый статический класс SshTransportConfigCallback реализует TransportConfigCallback {</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > private final SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() {</span> private final SshSessionFactory sshSessionFactory = new JschConfigSessionFactory () {</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > @Override </span> @Override </span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > protected void configure(OpenSshConfig.Host hc, Session session) {</span> защищенный void configure (OpenSshConfig.Host hc, Session session) {</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >session.setConfig( "StrictHostKeyChecking" , "no" );</span> session.setConfig ( "StrictHostKeyChecking" , "no" );</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >}</span> }</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > @Override </span> @Override </span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > protected JSch createDefaultJSch(FS fs) throws JSchException {</span> защищенный JSch createDefaultJSch (FS FS) выдает JSchException {</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >JSch jSch = super .createDefaultJSch(fs);</span> JSch jSch = super .createDefaultJSch (fs);</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >jSch.addIdentity( "/path/to/key" , "super-secret-passphrase" .getBytes());</span> jSch.addIdentity ( "/ path / to / key" , "super-secret-passphrase" .getBytes ());</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > return jSch;</span> вернуть jSch;</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >}</span> }</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >};</span> };</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > @Override </span> @Override </span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > public void configure(Transport transport) {</span> public void configure (Транспортный транспорт) {</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >SshTransport sshTransport = (SshTransport) transport;</span> SshTransport sshTransport = (SshTransport) транспорт;</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >sshTransport.setSshSessionFactory(sshSessionFactory);</span> sshTransport.setSshSessionFactory (sshSessionFactory);</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >}</span> }</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >}</span> }</span> |
Теперь вы можете выполнять команды на вашем дескрипторе Git
. Полный пример доступа к репозиториям Git см. В моем приложении AsciiBlog .
Опубликовано на Java Code Geeks с разрешения Себастьяна Дашнера, партнера нашей программы JCG. Смотрите оригинальную статью здесь: Доступ к Git-репозиториям с помощью Java с использованием ключей SSH
Мнения, высказанные участниками Java Code Geeks, являются их собственными. |