Skip to content

Commit 03f5135

Browse files
committed
Doing work
1 parent 639386a commit 03f5135

File tree

3 files changed

+46
-12
lines changed

3 files changed

+46
-12
lines changed

README.md

+24
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,30 @@ Follow the steps below to run the example application:
1212
1. Run the following command to start the `hello-service`:
1313

1414
./gradlew :hello-service:bootRun
15+
16+
2. In a new terminal, run the following command to send a request to the unsecured `hello` endpoint:
17+
18+
./gradlew :hello-client:bootRun --args="hello Bob"
19+
20+
If successful, you will see the following response:
21+
22+
2019-12-18 11:02:55.038 INFO 3540 --- [ main] e.client.hello.HelloClientApplication : Sending message without Basic Auth metadata...
23+
2019-12-18 11:02:55.065 INFO 3540 --- [ main] e.client.hello.HelloClientApplication : Response: Hello, Bob, from unsecured method
24+
25+
3. Next, run the following command to send a request to the secure `hello.secure` endpoint:
26+
27+
./gradlew :hello-client:bootRun --args="hello.secure Bob"
28+
29+
You will receive an `io.rsocket.exceptions.ApplicationErrorException: Access Denied` because you have not supplied valid credentials.
30+
31+
4. Next, run the following command to send a request to the secure `hello.secure` endpoint, but this time supplying valid credentials:
32+
33+
./gradlew :hello-client:bootRun --args="--username=admin --password=password hello.secure Bob"
34+
35+
If successful, you will see the following response:
36+
37+
2019-12-18 11:07:12.171 INFO 3575 --- [ main] e.client.hello.HelloClientApplication : Sending message with Basic Auth metadata...
38+
2019-12-18 11:07:12.284 INFO 3575 --- [ main] e.client.hello.HelloClientApplication : Response: Hello, Bob, from secured method
1539

1640
## License
1741
MIT License

hello-client/src/main/java/example/client/hello/HelloClientApplication.java

+10-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.slf4j.Logger;
44
import org.slf4j.LoggerFactory;
55
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.beans.factory.annotation.Qualifier;
67
import org.springframework.boot.CommandLineRunner;
78
import org.springframework.boot.SpringApplication;
89
import org.springframework.boot.autoconfigure.SpringBootApplication;
@@ -29,8 +30,13 @@ public static void main(String... args) {
2930
@Component
3031
public class Runner implements CommandLineRunner {
3132

33+
@Qualifier("insecureRSocketRequester")
3234
@Autowired
33-
private RSocketRequester helloServiceRequester;
35+
private RSocketRequester insecureRSocketRequester;
36+
37+
@Qualifier("secureRSocketRequester")
38+
@Autowired
39+
private RSocketRequester secureRSocketRequester;
3440

3541
@Override
3642
public void run(String... args) throws Exception {
@@ -45,7 +51,7 @@ public void run(String... args) throws Exception {
4551
LOG.info("Sending message with Basic Auth metadata...");
4652

4753
// Sending request to the hello-service
48-
String message = helloServiceRequester.route(params.method)
54+
String message = secureRSocketRequester.route(params.method)
4955
.metadata(new UsernamePasswordMetadata(params.username, params.password), UsernamePasswordMetadata.BASIC_AUTHENTICATION_MIME_TYPE)
5056
.data(params.name)
5157
.retrieveMono(String.class)
@@ -59,7 +65,7 @@ public void run(String... args) throws Exception {
5965
LOG.info("Sending message without Basic Auth metadata...");
6066

6167
// Sending request to the hello-service
62-
String message = helloServiceRequester.route(params.method)
68+
String message = insecureRSocketRequester.route(params.method)
6369
.data(params.name)
6470
.retrieveMono(String.class)
6571
.doOnError(throwable -> {
@@ -86,7 +92,7 @@ public static class ClientArguments {
8692
/**
8793
* Basic auth password
8894
*/
89-
@Option(names = "--password", defaultValue = "basic auth password")
95+
@Option(names = "--password", description = "basic auth password")
9096
public String password;
9197

9298
/**

hello-client/src/main/java/example/client/hello/config/RSocketConfiguration.java

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package example.client.hello.config;
22

3+
import org.springframework.beans.factory.annotation.Qualifier;
34
import org.springframework.beans.factory.annotation.Value;
45
import org.springframework.context.annotation.Bean;
56
import org.springframework.context.annotation.Configuration;
@@ -17,17 +18,20 @@ public class RSocketConfiguration {
1718
@Value("${example.service.hello.port}")
1819
private int helloServicePort;
1920

20-
@Bean
21-
public RSocketStrategies rSocketStrategies() {
22-
return RSocketStrategies.builder()
23-
.encoder(new BasicAuthenticationEncoder())
24-
.build();
21+
@Bean("insecureRSocketRequester")
22+
public RSocketRequester insecureRSocketRequester() {
23+
return RSocketRequester.builder()
24+
.dataMimeType(MimeTypeUtils.TEXT_PLAIN)
25+
.connectTcp(helloServiceHostname, helloServicePort)
26+
.block();
2527
}
2628

27-
@Bean
28-
public RSocketRequester helloServiceRequester(RSocketStrategies rSocketStrategies) {
29+
@Bean(name = "secureRSocketRequester")
30+
public RSocketRequester helloServiceRequester() {
2931
return RSocketRequester.builder()
30-
.rsocketStrategies(rSocketStrategies)
32+
.rsocketStrategies(builder -> {
33+
builder.encoder(new BasicAuthenticationEncoder());
34+
})
3135
.dataMimeType(MimeTypeUtils.TEXT_PLAIN)
3236
.connectTcp(helloServiceHostname, helloServicePort)
3337
.block();

0 commit comments

Comments
 (0)