Skip to content

Commit

Permalink
🚀 适配v1.0.4版本的ssh-agent,支持Apache mina sshd
Browse files Browse the repository at this point in the history
  • Loading branch information
hengboy committed May 27, 2024
1 parent d624106 commit d4aa8db
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
1 change: 1 addition & 0 deletions api-boot-project/api-boot-autoconfigure/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@
<dependency>
<groupId>org.minbox.framework</groupId>
<artifactId>ssh-agent</artifactId>
<version>${ssh-agent.version}</version>
<optional>true</optional>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.minbox.framework.api.boot.autoconfigure.ssh;

import lombok.Data;
import org.minbox.framework.ssh.agent.AgentSupport;
import org.minbox.framework.ssh.agent.config.AgentConfig;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;
Expand Down Expand Up @@ -30,4 +31,8 @@ public class SshAgentProperties {
*/
@NestedConfigurationProperty
private List<AgentConfig> configs;
/**
* 使用的Ssh代理
*/
private AgentSupport agentSupport = AgentSupport.mina;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import lombok.extern.slf4j.Slf4j;
import org.minbox.framework.ssh.agent.AgentConnection;
import org.minbox.framework.ssh.agent.DefaultAgentConnection;
import org.minbox.framework.ssh.agent.AgentSupport;
import org.minbox.framework.ssh.agent.apache.ApacheMinaSshdAgentConnection;
import org.minbox.framework.ssh.agent.config.AgentConfig;
import org.minbox.framework.ssh.agent.jsch.JSchAgentConnection;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ObjectUtils;

Expand All @@ -28,11 +30,11 @@ public class SshAgentServletContextListener implements ServletContextListener {
/**
* The ssh-agent auto config properties
*/
private SshAgentProperties sshAgentProperties;
private final SshAgentProperties sshAgentProperties;
/**
* Cache a list of AgentConnection objects
*/
private List<AgentConnection> connections = new ArrayList<>();
private final List<AgentConnection> connections = new ArrayList<>();

public SshAgentServletContextListener(SshAgentProperties sshAgentProperties) {
this.sshAgentProperties = sshAgentProperties;
Expand All @@ -44,7 +46,8 @@ public SshAgentServletContextListener(SshAgentProperties sshAgentProperties) {
* Create an {@link AgentConnection} instance according to each {@link AgentConfig} and perform port forwarding connection
*
* @param sce The {@link ServletContextEvent} instance
* @see DefaultAgentConnection
* @see JSchAgentConnection
* @see ApacheMinaSshdAgentConnection
*/
@Override
public void contextInitialized(ServletContextEvent sce) {
Expand All @@ -53,9 +56,10 @@ public void contextInitialized(ServletContextEvent sce) {
log.warn("ssh-agent agent does not take effect, reason: agent information is not configured.");
return;
}
configs.stream().forEach(config -> {
configs.forEach(config -> {
try {
AgentConnection connection = new DefaultAgentConnection(config);
AgentSupport agentSupport = sshAgentProperties.getAgentSupport();
AgentConnection connection = (AgentConnection) Class.forName(agentSupport.getClassName()).getDeclaredConstructor(AgentConfig.class).newInstance(config);
this.connections.add(connection);
connection.connect();
} catch (Exception e) {
Expand All @@ -73,6 +77,6 @@ public void contextInitialized(ServletContextEvent sce) {
*/
@Override
public void contextDestroyed(ServletContextEvent sce) {
connections.stream().forEach(connection -> connection.disconnect());
connections.forEach(AgentConnection::disconnect);
}
}
1 change: 1 addition & 0 deletions api-boot-project/api-boot-dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<build-helper-maven-plugin.version>3.2.0</build-helper-maven-plugin.version>
<xml-maven-plugin.version>1.0.2</xml-maven-plugin.version>
<minbox-datasource-switch.version>1.0.4</minbox-datasource-switch.version>
<ssh-agent.version>1.0.4</ssh-agent.version>
</properties>
<dependencyManagement>
<dependencies>
Expand Down

0 comments on commit d4aa8db

Please sign in to comment.