Skip to content

Commit 400ca29

Browse files
committed
Add sample to showcase secure input
1 parent 1ce48ff commit 400ca29

File tree

7 files changed

+118
-0
lines changed

7 files changed

+118
-0
lines changed

README.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ You can find complete tutorials and sample applications in the `spring-shell-sam
7373
- https://github.com/spring-projects/spring-shell/tree/main/spring-shell-samples/spring-shell-sample-spring-boot[Hello World Spring Shell application with Spring Boot]
7474
- https://github.com/spring-projects/spring-shell/tree/main/spring-shell-samples/spring-shell-sample-petclinic[Spring Pet Clinic CLI application]
7575
- https://github.com/spring-projects/spring-shell/tree/main/spring-shell-samples/spring-shell-sample-non-interactive[Non-interactive Spring Shell application]
76+
- https://github.com/spring-projects/spring-shell/tree/main/spring-shell-samples/spring-shell-sample-secure-input[Spring Shell application to demonstrate secure input]
7677

7778
== Getting Help
7879
Are you having trouble with Spring Shell? We want to help!

spring-shell-samples/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<module>spring-shell-sample-petclinic</module>
1919
<module>spring-shell-sample-spring-boot</module>
2020
<module>spring-shell-sample-non-interactive</module>
21+
<module>spring-shell-sample-secure-input</module>
2122
</modules>
2223

2324
<build>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
### About
2+
3+
This is a sample project demonstrating the use of Spring Shell commands to read secure input from the user, such as passwords.
4+
5+
### Building the Project
6+
7+
To build the project, navigate to the project's root directory and run the following command:
8+
9+
```bash
10+
./mvnw clean install
11+
```
12+
13+
### Running the Application
14+
15+
To run the application, use the following command:
16+
17+
```bash
18+
./mvnw -pl org.springframework.shell:spring-shell-sample-secure-input spring-boot:run
19+
```
20+
21+
You should see a prompt where you can use the `change-password` command to securely input and change a password.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<parent>
6+
<groupId>org.springframework.shell</groupId>
7+
<artifactId>spring-shell-samples</artifactId>
8+
<version>4.0.0-SNAPSHOT</version>
9+
</parent>
10+
11+
<artifactId>spring-shell-sample-secure-input</artifactId>
12+
<name>Spring Shell Samples - Secure input sample application </name>
13+
<packaging>jar</packaging>
14+
<description>Spring Shell application to show how to read secure input</description>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>org.springframework.shell</groupId>
19+
<artifactId>spring-shell-starter-jansi</artifactId>
20+
<version>${project.parent.version}</version>
21+
</dependency>
22+
<dependency>
23+
<groupId>ch.qos.logback</groupId>
24+
<artifactId>logback-classic</artifactId>
25+
<version>${logback.version}</version>
26+
</dependency>
27+
<dependency>
28+
<groupId>org.hibernate.validator</groupId>
29+
<artifactId>hibernate-validator</artifactId>
30+
<version>${hibernate-validator.version}</version>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.glassfish</groupId>
34+
<artifactId>jakarta.el</artifactId>
35+
<version>${jakarta.el.version}</version>
36+
</dependency>
37+
</dependencies>
38+
39+
<build>
40+
<plugins>
41+
<plugin>
42+
<groupId>org.springframework.boot</groupId>
43+
<artifactId>spring-boot-maven-plugin</artifactId>
44+
<version>${spring-boot.version}</version>
45+
<configuration>
46+
<mainClass>org.springframework.shell.samples.secure.input.SpringShellApplication</mainClass>
47+
</configuration>
48+
</plugin>
49+
</plugins>
50+
</build>
51+
52+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.springframework.shell.samples.secure.input;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.shell.core.command.CommandContext;
6+
import org.springframework.shell.core.command.annotation.Command;
7+
8+
@SpringBootApplication
9+
public class SpringShellApplication {
10+
11+
public static void main(String[] args) {
12+
SpringApplication.run(SpringShellApplication.class, args);
13+
}
14+
15+
@Command(name = "change-password", description = "Change password", group = "User Management",
16+
help = "A command that changes the user password by securely reading it from the standard input.")
17+
public String changePassword(CommandContext commandContext) {
18+
try {
19+
char[] chars = commandContext.inputReader().readPassword("Enter new password: ");
20+
// In a real application, you would update the password securely here
21+
return "Password successfully updated.";
22+
}
23+
catch (Exception e) {
24+
commandContext.outputWriter().println("Error reading password: " + e.getMessage());
25+
return "Failed to set password.";
26+
}
27+
}
28+
29+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
spring.application.name=spring-shell-secure-input
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<configuration>
2+
3+
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
4+
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
5+
<pattern>%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n</pattern>
6+
</encoder>
7+
</appender>
8+
9+
<root level="info">
10+
<appender-ref ref="CONSOLE"/>
11+
</root>
12+
13+
</configuration>

0 commit comments

Comments
 (0)