Skip to content

Commit 93d2757

Browse files
committed
🎉 集成ssh-agent代理组件
1 parent 9869f8e commit 93d2757

File tree

12 files changed

+260
-2
lines changed

12 files changed

+260
-2
lines changed

api-boot-project/api-boot-autoconfigure/pom.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,11 @@
176176
<artifactId>message-pipe-spring-context</artifactId>
177177
<optional>true</optional>
178178
</dependency>
179-
179+
<dependency>
180+
<groupId>org.minbox.framework</groupId>
181+
<artifactId>ssh-agent</artifactId>
182+
<optional>true</optional>
183+
</dependency>
180184

181185
<!--Others-->
182186
<dependency>
@@ -260,6 +264,7 @@
260264
<artifactId>guava</artifactId>
261265
<optional>true</optional>
262266
</dependency>
267+
263268
</dependencies>
264269
<build>
265270
<resources>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.minbox.framework.api.boot.autoconfigure.ssh;
2+
3+
import org.minbox.framework.ssh.agent.AgentConnection;
4+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
5+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
6+
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.context.annotation.Import;
8+
9+
/**
10+
* The ssh agent configuration
11+
*
12+
* @author 恒宇少年
13+
* @see SshAgentServletContextListener
14+
*/
15+
@Configuration
16+
@ConditionalOnClass(AgentConnection.class)
17+
@EnableConfigurationProperties(SshAgentProperties.class)
18+
@Import(SshAgentServletContextListener.class)
19+
public class SshAgentAutoConfiguration {
20+
// ...
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.minbox.framework.api.boot.autoconfigure.ssh;
2+
3+
import lombok.Data;
4+
import org.minbox.framework.ssh.agent.config.AgentConfig;
5+
import org.springframework.boot.context.properties.ConfigurationProperties;
6+
import org.springframework.context.annotation.Configuration;
7+
8+
import java.util.List;
9+
10+
import static org.minbox.framework.api.boot.autoconfigure.ssh.SshAgentProperties.SSH_AGENT_PREFIX;
11+
12+
/**
13+
* Ssh agent config properties
14+
*
15+
* @author 恒宇少年
16+
*/
17+
@Data
18+
@Configuration
19+
@ConfigurationProperties(prefix = SSH_AGENT_PREFIX)
20+
public class SshAgentProperties {
21+
/**
22+
* The config prefix of ssh-agent
23+
*/
24+
public static final String SSH_AGENT_PREFIX = "api.boot.ssh-agent";
25+
/**
26+
* The config collection of {@link AgentConfig}
27+
* <p>
28+
* Use this parameter to configure proxy multiple remote server port forwarding information
29+
*/
30+
private List<AgentConfig> configs;
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package org.minbox.framework.api.boot.autoconfigure.ssh;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.minbox.framework.ssh.agent.AgentConnection;
5+
import org.minbox.framework.ssh.agent.DefaultAgentConnection;
6+
import org.minbox.framework.ssh.agent.config.AgentConfig;
7+
import org.springframework.context.annotation.Configuration;
8+
import org.springframework.util.ObjectUtils;
9+
10+
import javax.servlet.ServletContext;
11+
import javax.servlet.ServletContextEvent;
12+
import javax.servlet.ServletContextListener;
13+
import javax.servlet.annotation.WebListener;
14+
import java.util.ArrayList;
15+
import java.util.List;
16+
17+
/**
18+
* The ssh-agent web listener
19+
* <p>
20+
* Establish a proxy forwarding channel after the servlet context is created when the project starts
21+
*
22+
* @author 恒宇少年
23+
*/
24+
@Configuration
25+
@Slf4j
26+
@WebListener
27+
public class SshAgentServletContextListener implements ServletContextListener {
28+
/**
29+
* The ssh-agent auto config properties
30+
*/
31+
private SshAgentProperties sshAgentProperties;
32+
/**
33+
* Cache a list of AgentConnection objects
34+
*/
35+
private List<AgentConnection> connections = new ArrayList<>();
36+
37+
public SshAgentServletContextListener(SshAgentProperties sshAgentProperties) {
38+
this.sshAgentProperties = sshAgentProperties;
39+
}
40+
41+
/**
42+
* {@link ServletContext} initialized method
43+
* <p>
44+
* Create an {@link AgentConnection} instance according to each {@link AgentConfig} and perform port forwarding connection
45+
*
46+
* @param sce The {@link ServletContextEvent} instance
47+
* @see DefaultAgentConnection
48+
*/
49+
@Override
50+
public void contextInitialized(ServletContextEvent sce) {
51+
List<AgentConfig> configs = sshAgentProperties.getConfigs();
52+
if (ObjectUtils.isEmpty(configs)) {
53+
log.warn("ssh-agent agent does not take effect, reason: agent information is not configured.");
54+
return;
55+
}
56+
configs.stream().forEach(config -> {
57+
AgentConnection connection = new DefaultAgentConnection(config);
58+
this.connections.add(connection);
59+
connection.connect();
60+
});
61+
}
62+
63+
/**
64+
* {@link ServletContext} destroy method
65+
* <p>
66+
* Disconnect all connections in the AgentConnection cache list
67+
*
68+
* @param sce The {@link ServletContextEvent} instance
69+
*/
70+
@Override
71+
public void contextDestroyed(ServletContextEvent sce) {
72+
connections.stream().forEach(connection -> connection.disconnect());
73+
}
74+
}

api-boot-project/api-boot-autoconfigure/src/main/resources/META-INF/spring.factories

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
2424
org.minbox.framework.api.boot.autoconfigure.mongo.ApiBootMongoClientSettingsAutoConfiguration,\
2525
org.minbox.framework.api.boot.autoconfigure.tools.ApiBootToolsAutoConfiguration,\
2626
org.minbox.framework.api.boot.autoconfigure.message.pipe.client.MessagePipeClientAutoConfiguration,\
27-
org.minbox.framework.api.boot.autoconfigure.message.pipe.server.MessagePipeServerAutoConfiguration
27+
org.minbox.framework.api.boot.autoconfigure.message.pipe.server.MessagePipeServerAutoConfiguration,\
28+
org.minbox.framework.api.boot.autoconfigure.ssh.SshAgentAutoConfiguration

api-boot-project/api-boot-dependencies/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,11 @@
147147
<artifactId>api-boot-starter-message-pipe-server</artifactId>
148148
<version>${project.version}</version>
149149
</dependency>
150+
<dependency>
151+
<groupId>org.minbox.framework</groupId>
152+
<artifactId>api-boot-starter-ssh-agent</artifactId>
153+
<version>${project.version}</version>
154+
</dependency>
150155
</dependencies>
151156
</dependencyManagement>
152157

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>api-boot-starters</artifactId>
7+
<groupId>org.minbox.framework</groupId>
8+
<version>${revision}</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
<artifactId>api-boot-starter-ssh-agent</artifactId>
12+
<properties>
13+
<main.basedir>${basedir}/../../..</main.basedir>
14+
</properties>
15+
<developers>
16+
<developer>
17+
<name>恒宇少年</name>
18+
<email>jnyuqy@gmail.com</email>
19+
<organization>minbox-projects</organization>
20+
<organizationUrl>https://gitee.com/minbox-projects</organizationUrl>
21+
</developer>
22+
</developers>
23+
<scm>
24+
<connection>scm:git:https://gitee.com/minbox-projects/api-boot.git</connection>
25+
<developerConnection>scm:git:https://gitee.com/minbox-projects/api-boot.git</developerConnection>
26+
<url>https://gitee.com/minbox-projects/api-boot</url>
27+
<tag>HEAD</tag>
28+
</scm>
29+
<dependencies>
30+
<dependency>
31+
<groupId>org.minbox.framework</groupId>
32+
<artifactId>api-boot-starter</artifactId>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.minbox.framework</groupId>
36+
<artifactId>ssh-agent</artifactId>
37+
</dependency>
38+
</dependencies>
39+
</project>

api-boot-project/api-boot-starters/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
<module>api-boot-starter-mongo-client-settings</module>
3636
<module>api-boot-starter-message-pipe-client</module>
3737
<module>api-boot-starter-message-pipe-server</module>
38+
<module>api-boot-starter-ssh-agent</module>
3839
</modules>
3940

4041
</project>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>api-boot-samples</artifactId>
7+
<groupId>org.minbox.framework</groupId>
8+
<version>${revision}</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>api-boot-sample-ssh-agent</artifactId>
13+
<dependencies>
14+
<dependency>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-web</artifactId>
17+
</dependency>
18+
<dependency>
19+
<groupId>org.springframework.boot</groupId>
20+
<artifactId>spring-boot-starter-test</artifactId>
21+
</dependency>
22+
<dependency>
23+
<groupId>org.minbox.framework</groupId>
24+
<artifactId>api-boot-starter-ssh-agent</artifactId>
25+
</dependency>
26+
</dependencies>
27+
<dependencyManagement>
28+
<dependencies>
29+
<dependency>
30+
<groupId>org.minbox.framework</groupId>
31+
<artifactId>api-boot-dependencies</artifactId>
32+
<version>${api-boot.version}</version>
33+
<type>pom</type>
34+
<scope>import</scope>
35+
</dependency>
36+
</dependencies>
37+
</dependencyManagement>
38+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.minbox.framework.sample.ssh.agent;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.springframework.boot.SpringApplication;
6+
import org.springframework.boot.autoconfigure.SpringBootApplication;
7+
8+
/**
9+
* ssh agent 示例入口
10+
*
11+
* @author 恒宇少年
12+
*/
13+
@SpringBootApplication
14+
public class SshAgentSampleApplication {
15+
/**
16+
* logger instance
17+
*/
18+
static Logger logger = LoggerFactory.getLogger(SshAgentSampleApplication.class);
19+
20+
public static void main(String[] args) {
21+
SpringApplication.run(SshAgentSampleApplication.class, args);
22+
logger.info("ssh agent服务启动成功.");
23+
}
24+
}

0 commit comments

Comments
 (0)