Spring boot remote shell 自定义命令 应用篇

关于官方文档上对于使用的基本配置已经有所描述,案例中使用 Groovy 创建自定义命令交互;

在这里将主要使用 Java 再实现一次,并结合 maven 使用,可供参考。

本篇前提默认大家对 spring boot 有了基本了解,如何引入基础依赖,启动服务等就不详细介绍。

基础配置

pom 文件中添加以下:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-remote-shell</artifactId>
</dependency>

以下配置用于不编译 commands 目录下 java 文件,并将 java 文件 copy 至 classpath*:/commands 目录下

<build>
        <resources>
           <resource>
               <directory>src/main/java/commands</directory>
               <targetPath>commands</targetPath>
               <filtering>false</filtering>
           </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <excludes>
                        <exclude>/commands/*</exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
</build>

application.properties 添加以下配置内容:

shell.auth.simple.user.name=test
shell.auth.simple.user.password=test

自定义命令

创建 TestCommand.java

package commands;

import org.crsh.cli.Command;
import org.crsh.cli.Usage;
import org.crsh.command.BaseCommand;

@Usage("Test Command")
public class TestCommand extends BaseCommand{

    @Command
    @Usage("Useage main Command")
    public String main (){

        return "main command";
    }

}

启动 springboot 服务:

 mvn spring-boot:run

看一下 log

现在可以尝试登录上去:

ssh -p 2000 test@localhost

执行 help:

执行命令 TestCommand

可以看到 main command 返回,证明调用成功

单纯这种调用相信大家都觉得并没有什么太大意思,往往希望通过这种后门能够帮助应用做一些补救措施;

例如直接操作 spring context ,获取到具体的 bean ,并处理执行相关业务逻辑;

往下看,在这里会使用到 InvocationContext 接口来获取上下文,修改一下 TestCommand 文件,如下:

package commands;

import org.crsh.cli.Command;
import org.crsh.cli.Usage;
import org.crsh.command.BaseCommand;
import org.crsh.command.InvocationContext;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;

@Usage("Test Command")
public class TestCommand extends BaseCommand {

    @Command
    @Usage("Useage main Command")
    public String main(InvocationContext context) {

        DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) context.getAttributes().get("spring.beanfactory");

        for (String name : defaultListableBeanFactory.getBeanDefinitionNames()) {
            System.out.println(name);
            context.getWriter().write(name);
            context.getWriter().write("\n");
        }

        return "main command";
    }
}

再次启动服务,执行 TestCommand 命令,会有如下返回:

就这样可以方便使用自定义的命令,调用 spring context 中方法;

当然看到这里,再往前思考一步,肯定还有问题引出,shell 可以调用 spring context 中相关 bean 的方法了,那方法参数传递怎么搞定?

后面的续篇中,再和大家分享,把搬运工作进行到底!

spring boot remote shell 是集成 CRsSH

如果迫不及待的朋友可以先阅读 CRaSH Reference Guide

希望能给大家一定帮助

参考:

49. Monitoring and management using a remote shell

CRaSH Reference Guide

java - Spring boot additional Crash Command

<think>好的,用户想了解如何使用spring-boot-starter-remote-shell进行远程shell操作。首先,我需要回忆一下这个组件的相关信息。根据提供的引用,特别是引用[1]和[4],提到spring-boot-starter-remote-shell曾经提供了远程SSH支持,但后来可能被弃用了。用户需要注意版本兼容性问题,可能在某些Spring Boot版本中已经不可用了。 接下来,我应该分步骤指导用户如何尝试使用这个库。首先需要确认Spring Boot的版本,因为根据引用[4],如果版本不支持,添加依赖也没用。所以第一步是检查用户使用的Spring Boot版本是否兼容。通常,这个组件在较旧的版本中可能存在,比如1.x版本,而Spring Boot 2.x可能已经移除了该功能。 然后,添加依赖的部分。用户需要在pom.xml中添加对应的starter依赖,但要注意版本匹配。这里可能需要引用具体的版本号,比如如果用户在用Spring Boot 1.5.x,可能需要指定相应的starter版本。同时,提醒用户如果遇到问题,应该考虑替代方案,比如引用[4]中提到的功能不可用的情况,可能需要使用Spring Boot Actuator或其他远程管理工具。 接下来是配置部分。需要指导用户在application.properties中设置端口、认证信息等。例如,设置shell的端口为2000,启用认证方式,配置用户和密码。这里需要注意安全风险,提醒用户使用安全连接和强密码。 然后,启动应用后,用户可以通过SSH客户端连接。给出示例命令,比如ssh user@localhost -p 2000,并输入密码。如果连接成功,就能执行内置命令如metrics或health。 但根据引用[4],如果用户已经添加了依赖但功能不可用,可能需要检查版本,或者考虑替代方案。这时候需要建议用户升级到Spring Boot 2.x,并使用Actuator的HTTP端点,或者Spring Cloud的组件如Spring Cloud Config和Spring Cloud Bus来进行远程管理。 最后,生成相关问题,帮助用户深入理解可能遇到的障碍和替代方案。例如,询问为什么依赖不可用,替代方案有哪些,如何保证远程访问的安全性等。 整个过程需要确保回答结构清晰,步骤明确,同时提醒用户注意版本兼容性和安全问题,确保回答的可靠性和实用性。</think>关于使用$spring-boot-starter-remote-shell$进行远程shell操作,由于该组件在较新版本中已被废弃[^4],以下是分步骤说明和注意事项: ### 一、历史版本使用步骤(仅限Spring Boot 1.x) 1. **添加依赖**(仅旧版本有效) ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-remote-shell</artifactId> <version>1.5.x.RELEASE</version> <!-- 需与Spring Boot主版本一致 --> </dependency> ``` 2. **配置安全认证** 在$application.properties$中设置: ```properties shell.auth=simple # 简单认证模式 shell.auth.simple.user.name=admin shell.auth.simple.user.password=secret shell.ssh.port=2000 # SSH监听端口 ``` 3. **启动后连接** 通过SSH客户端连接: ```bash ssh admin@localhost -p 2000 # 输入密码后进入交互式shell ``` ### 二、已知限制与替代方案 1. **废弃说明** 该模块自Spring Boot 2.0起被移除,官方建议改用Actuator端点实现远程管理[^1] 2. **现代替代方案** - 使用$spring-boot-starter-actuator$的HTTP端点: ```properties management.endpoints.web.exposure.include=* management.endpoint.health.show-details=always ``` - 通过JMX或$jconsole$进行监控 - 集成Spring Cloud的分布式配置管理[^3]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值